public Map<String, Object> calculateObjects(Map<String, Object> input) { Map<String, Object> result = new HashMap<String, Object>(); Matrix source = MathUtil.getMatrix(input.get(SOURCE)); Matrix target = Matrix.Factory.zeros(source.getRowCount(), 1); long cols = source.getColumnCount(); long rows = source.getRowCount(); for (int k = 0; k < rows; k++) { int tp = 0; int fn = 0; for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { int count = source.getAsInt(r, c); boolean expected = r == k; boolean predicted = c == k; if (expected && predicted) { tp += count; } else if (expected && (!predicted)) { fn += count; } } } target.setAsDouble(MathUtil.sensitivity(tp, fn), k, 0); } result.put(TARGET, target); return result; }
private Matrix calculate() { wordMapping = new DefaultMapMatrix<String, Long>(); Matrix m = getSource(); for (long[] c : m.availableCoordinates()) { String s = m.getAsString(c); if (s != null) { String[] words = s.split("\\s+"); for (String w : words) { if (w.length() == 0) { continue; } Long i = wordMapping.get(w); if (i == null) { wordMapping.put(w, wordMapping.getRowCount()); } } } } result = new DefaultSparseLongMatrix(m.getRowCount(), wordMapping.getRowCount()); long rowCount = m.getRowCount(); long colCount = m.getColumnCount(); for (long row = 0; row < rowCount; row++) { for (long col = 0; col < colCount; col++) { String string = m.getAsString(row, col); if (string != null && string.length() > 0) { String[] words = string.split("[\\s]+"); for (String w : words) { if (w.length() == 0) { continue; } long i = wordMapping.get(w); int count = result.getAsInt(row, i); result.setAsInt(++count, row, i); } } } } return result; }