public void process(Context c, Sentence s) { // we assume markers occur only at the beginning of clauses // TODO is this heuristic language-specific? Intervals markerInts = new Intervals(); for (Interval clause : s.get(Features.CLAUSES)) for (Marker m : markers) if (testMarker(s, clause, m)) { markerInts.add(clause.low(), clause.low() + m.length()).set(Features.MARKER_TYPE, m.type); break; } s.set(Features.DISCOURSE_MARKERS, markerInts); }
/** * Create an {@link ArrayImgFactory} if an image of the requested <code>targetSize</code> could be * held in an {@link ArrayImg}. Otherwise return a {@link CellImgFactory} with as large as * possible cell size. * * @param targetSize size of image that the factory should be able to create. * @param type type of the factory. * @return an {@link ArrayImgFactory} or a {@link CellImgFactory}. */ public static <T extends NativeType<T>> ImgFactory<T> getArrayOrCellImgFactory( final Dimensions targetSize, final T type) { if (Intervals.numElements(targetSize) <= Integer.MAX_VALUE) return new ArrayImgFactory<T>(); final int cellSize = (int) Math.pow( Integer.MAX_VALUE / type.getEntitiesPerPixel().getRatio(), 1.0 / targetSize.numDimensions()); return new CellImgFactory<T>(cellSize); }
private static String replaceVars(String a, String[] have) { String ret = a; if (a.contains("var")) { String[] aFields = a.split(":="); Pattern p = Pattern.compile("var[0-9]+"); Matcher m = p.matcher(aFields[1]); while (m.find()) { aFields[1] = aFields[1].replaceAll(m.group(), Intervals.getVariableInterval(have, m.group())); } ret = aFields[0].replaceAll("var[0-9]+", "var") + ":=" + aFields[1]; } return ret; }
private void maybeAdd(Intervals out, Information info, Interval i) { if (i != null) { i.set(Features.INFORMATION, info); out.add(i); } }
// Join a set of previous 'know' clauses with the effects of an action, taking the input into // account // for example, if you know that "N>=2" and you run an action with effect "L[idx1]<L[idx2]" and // with input 1:1 and 2:2, the results is "N>=2" and "L[1]<L[2]" public static String[] joinKnows(String[] prev, Effect effect, String[] input, String[] have) { ArrayList<String> knowledge = new ArrayList(); knowledge.addAll(Arrays.asList(prev)); // split intervals into single value stuff // check if haves affect current knowns // this method is fairly naive, because a have of L[1:N] should replace a know for L[1] or L[2] // plus we are assuming 2 inputs with names idx1 and idx2 // add new knowns // were assuming 2 inputs named idx1 and idx2! // for each effect String[][] possibleInputs = Intervals.getInputList(input, have); for (String[] inputArr : possibleInputs) { // System.out.println("Joining knows with input (" + inputArr[0] + " ; " + inputArr[1] + ")"); ArrayList<String> effectsHave = new ArrayList(); ArrayList<String> effectsKnowTemp = new ArrayList(); ArrayList<String> effectsKnow = new ArrayList(); for (String eff : effect.effects) { if (eff.startsWith("have")) { effectsHave.add(eff); } else if (eff.contains("'")) { effectsKnowTemp.add(eff); } else { effectsKnow.add(eff); } } for (String eff : effectsHave) { // System.out.println("Oba: " + eff.replaceAll("idx1", inputArr[0]).replaceAll("idx2", // inputArr[1])); String[] actualEffSplit = eff.split(";"); String[] actualEff = actualEffSplit[1].split(":="); // ignore self attributions if (actualEff[0] .replaceAll("idx1", inputArr[0]) .replaceAll("idx2", inputArr[1]) .equals(actualEff[1].replaceAll("idx1", inputArr[0]).replaceAll("idx2", inputArr[1]))) { continue; } // replace idx's for actual indexes (for example, idx1 for 1, 2, ..., N-1 or N) String oldValue = actualEff[0].replaceAll("idx1", inputArr[0]).replaceAll("idx2", inputArr[1]); // find knowledge about cases and check whether the knowledge is still valid // in other words, we remove 'knows' that have been affected for (int j = 0; j < knowledge.size(); j++) { if (knowledge.get(j).contains(oldValue)) { // check rest of action knowledge (the ones with ') // if for example, current 'have' says that (maybe) L[1]:=L[N] // and we previously had knowledge of L[1]<L[2] // by checking that L[1]<=L[1]', we know L[1]<L[2] is still valid boolean isCurrentKnowOK = false; for (String tempKnow : effectsKnowTemp) { tempKnow = tempKnow .split(";")[1] .replaceAll("idx1", inputArr[0]) .replaceAll("idx2", inputArr[1]); // skip information not relevant if (!tempKnow.contains(oldValue)) { continue; } // System.out.println("\t@" + knowledge.get(j) + "\t\tRelevant TempKnow: " + // tempKnow); String[] tempParts = tempKnow.split("[<=>]+"); tempParts[1] = tempParts[1].replaceAll("'", ""); // this is the old part String tempOperator = Logic.getOperator(tempKnow); String[] parts = knowledge.get(j).split("[<=>]+"); String operator = Logic.getOperator(knowledge.get(j)); // this assumes that all temporary knows are related to themselves! // for example, L[idx1]<=L[idx1]' if (!tempParts[0].equals(tempParts[1])) { // if something like L[idx1]<=L[idx2]' came around, we'd have to check the 2nd // variable // and infer knowledge from it by reading the knowledge list! // basically, we have to reach this same state from the L[idx2]' // assume we know L[1]<L[2], L[3]<L[2], and we now will replace L[1]:=L[3]-1 // then, tempKnow is that L[1]<L[3]'. When analysing L[1]<L[2], we find that // 'L[1]<L[3]' and, since we know 'L[3]<L[2]', then we infer L[1]<L[2], and the // know is valid! System.err.println( "TempKnowledge " + tempKnow + " is not for the same variable " + tempParts[0] + "!=" + tempParts[1]); } else { if (parts[0].equals(tempParts[0])) { isCurrentKnowOK = Logic.matchOperators(operator, tempOperator); } else if (parts[1].equals(tempParts[0])) { isCurrentKnowOK = Logic.matchOperators(Logic.mirrorOperator(operator), tempOperator); } else { System.err.println( tempKnow + " somehow contains " + oldValue + ", but its not found after split"); } } } // if current 'know' is valid, lets move on if (isCurrentKnowOK) { continue; } // otherwise, check what we know about value that will replace actualEff[0] String newValue = actualEff[1].replaceAll("idx1", inputArr[0]).replaceAll("idx2", inputArr[1]); // System.out.println("\tChecking prev Knowledge for att " + oldValue + ":=" + // newValue); for (String know : knowledge) { if (know.contains(newValue)) { while (know.contains(newValue)) { know = know.replace(newValue, oldValue); } // System.out.println("\tChecking prev Knowledge! " + knowledge.get(j) + " VS " + // know); if (know.equals(knowledge.get(j))) { // System.out.println("\tChecking prev Knowledge! " + knowledge.get(j) + " is // equal to processed " + know); isCurrentKnowOK = true; break; } } } if (!isCurrentKnowOK) { // System.out.println("REMOVING knowledge " + knowledge.get(j)); knowledge.remove(j); j--; } } } } for (String eff : effectsKnow) { String actualEff = eff.split(";")[1]; // for all values in the interval (for example, 1:N affects 1, 2, ..., N-1 and N String temp = actualEff.replaceAll("idx1", inputArr[0]).replaceAll("idx2", inputArr[1]); // avoid repeated knowledge if (!knowledge.contains(temp)) { knowledge.add(temp); } } } // create return array String[] knowledgeArr = new String[knowledge.size()]; for (int i = 0; i < knowledge.size(); i++) { knowledgeArr[i] = knowledge.get(i); } return knowledgeArr; }
void strassenMult( final int mf, final int ml, final int nf, final int nl, final int pf, final int pl, final Matrix A, final Matrix B, final Matrix C) { if ((ml - mf) * (nl - nf) * (pl - pf) < GRAIN) { matmultleaf(mf, ml, nf, nl, pf, pl, A, B, C); } else { final int m2 = (ml - mf) / 2; final int n2 = (nl - nf) / 2; final int p2 = (pl - pf) / 2; final Matrix M1 = new Matrix(m2, n2); final Matrix M2 = new Matrix(m2, n2); final Matrix M3 = new Matrix(m2, n2); final Matrix M4 = new Matrix(m2, n2); final Matrix M5 = new Matrix(m2, n2); final Matrix M6 = new Matrix(m2, n2); final Matrix M7 = new Matrix(m2, n2); final Matrix A11 = A.submatrix(mf, pf); final Matrix A12 = A.submatrix(mf, p2); final Matrix A21 = A.submatrix(m2, pf); final Matrix A22 = A.submatrix(m2, p2); final Matrix B11 = B.submatrix(pf, nf); final Matrix B12 = B.submatrix(pf, n2); final Matrix B21 = B.submatrix(p2, nf); final Matrix B22 = B.submatrix(p2, n2); final Matrix C11 = C.submatrix(mf, nf); final Matrix C12 = C.submatrix(mf, n2); final Matrix C21 = C.submatrix(m2, nf); final Matrix C22 = C.submatrix(m2, n2); Intervals.inline( new VoidInlineTask() { @Override public void run(Interval subinterval) { // M1 = (A11 + A22)*(B11 + B22) new Interval(Intervals.child()) { @Override public void run() { Matrix tAM1 = new Matrix(m2, p2); addMatrixBlocks(tAM1, m2, p2, A11, A22); Matrix tBM1 = new Matrix(p2, n2); addMatrixBlocks(tBM1, p2, n2, B11, B22); strassenMult(0, m2, 0, n2, 0, p2, tAM1, tBM1, M1); } }; // M2 = (A21 + A22)*B11 new Interval(Intervals.child()) { @Override public void run() { Matrix tAM2 = new Matrix(m2, p2); addMatrixBlocks(tAM2, m2, p2, A21, A22); strassenMult(0, m2, 0, n2, 0, p2, tAM2, B11, M2); } }; // M3 = A11*(B12 - B22) new Interval(Intervals.child()) { @Override public void run() { Matrix tBM3 = new Matrix(p2, n2); subMatrixBlocks(tBM3, p2, n2, B12, B22); strassenMult(0, m2, 0, n2, 0, p2, A11, tBM3, M3); } }; // M4 = A22*(B21 - B11) new Interval(Intervals.child()) { @Override public void run() { Matrix tBM4 = new Matrix(p2, n2); subMatrixBlocks(tBM4, p2, n2, B21, B11); strassenMult(0, m2, 0, n2, 0, p2, A22, tBM4, M4); } }; // M5 = (A11 + A12)*B22 new Interval(Intervals.child()) { @Override public void run() { Matrix tAM5 = new Matrix(m2, p2); addMatrixBlocks(tAM5, m2, p2, A11, A12); strassenMult(0, m2, 0, n2, 0, p2, tAM5, B22, M5); } }; // M6 = (A21 - A11)*(B11 + B12) new Interval(Intervals.child()) { @Override public void run() { Matrix tAM6 = new Matrix(m2, p2); Matrix tBM6 = new Matrix(p2, n2); subMatrixBlocks(tAM6, m2, p2, A21, A11); addMatrixBlocks(tBM6, p2, n2, B11, B12); strassenMult(0, m2, 0, n2, 0, p2, tAM6, tBM6, M6); } }; // M7 = (A12 - A22)*(B21 + B22) new Interval(Intervals.child()) { @Override public void run() { Matrix tAM7 = new Matrix(m2, p2); Matrix tBM7 = new Matrix(p2, n2); subMatrixBlocks(tAM7, m2, p2, A12, A22); addMatrixBlocks(tBM7, p2, n2, B21, B22); strassenMult(0, m2, 0, n2, 0, p2, tAM7, tBM7, M7); } }; } }); for (int i = 0; i < m2; i++) for (int j = 0; j < n2; j++) { C11.set(i, j, M1.get(i, j) + M4.get(i, j) - M5.get(i, j) + M7.get(i, j)); C12.set(i, j, M3.get(i, j) + M5.get(i, j)); C21.set(i, j, M2.get(i, j) + M4.get(i, j)); C22.set(i, j, M1.get(i, j) - M2.get(i, j) + M3.get(i, j) + M6.get(i, j)); } } }