public static ConfusionMatrix parseFromText(String text)
            throws IllegalArgumentException
    {
        try {

            String[] lines = text.split("\n");
            String[] l = lines[0].split("\\s+");

            List<String> labels = new ArrayList<>();
            for (String aL : l) {
                if (!aL.isEmpty()) {
                    labels.add(aL);
                }
            }

            ConfusionMatrix result = new ConfusionMatrix();

            for (int i = 1; i < lines.length; i++) {
                String line = lines[i];

                String[] split = line.split("\\s+");

                List<String> row = new ArrayList<>();
                for (String aSplit : split) {
                    if (!aSplit.isEmpty()) {
                        row.add(aSplit);
                    }
                }

                String predictedLabel = row.get(0);

                for (int r = 1; r < row.size(); r++) {
                    String s = row.get(r);
                    Integer val = Integer.valueOf(s);

                    String acutalLabel = labels.get(r - 1);

                    result.increaseValue(predictedLabel, acutalLabel, val);
                }
            }

            return result;
        }
        catch (Exception e) {
            throw new IllegalArgumentException("Wrong input format", e);
        }
    }
public static ConfusionMatrix parseFromText ( String text ) throws IllegalArgumentException { try { String [ ] lines = text . split ( "\n" ) ; String [ ] l = lines [ 0 ] . split ( "\\s+" ) ; List < String > labels = new ArrayList < > ( ) ; for ( String aL : l ) { if ( ! aL . isEmpty ( ) ) { labels . add ( aL ) ; } } ConfusionMatrix result = new ConfusionMatrix ( ) ; for ( int i = 1 ; i < lines . length ; i ++ ) { String line = lines [ i ] ; String [ ] split = line . split ( "\\s+" ) ; List < String > row = new ArrayList < > ( ) ; for ( String aSplit : split ) { if ( ! aSplit . isEmpty ( ) ) { row . add ( aSplit ) ; } } String predictedLabel = row . get ( 0 ) ; for ( int r = 1 ; r < row . size ( ) ; r ++ ) { String s = row . get ( r ) ; Integer val = Integer . valueOf ( s ) ; String acutalLabel = labels . get ( r - 1 ) ; result . increaseValue ( predictedLabel , acutalLabel , val ) ; } } return result ; } catch ( Exception e ) { throw new IllegalArgumentException ( "Wrong input format" , e ) ; } }
Confusion matrix printed to text by toString can be parsed back

@param text input text
@return confusion matrix
@throws IllegalArgumentException if input is malformed