@SuppressWarnings("unchecked") public ResidentConverter(List<ObjectConverter.ColumnInfo> allColumns) throws java.io.IOException { Optional<ObjectConverter.ColumnInfo> column; final java.util.List<ObjectConverter.ColumnInfo> columns = allColumns .stream() .filter( it -> "mixinReference".equals(it.typeSchema) && "Resident_entity".equals(it.typeName)) .collect(Collectors.toList()); columnCount = columns.size(); readers = new ObjectConverter.Reader[columnCount]; for (int i = 0; i < readers.length; i++) { readers[i] = (instance, rdr, ctx) -> StringConverter.skip(rdr, ctx); } final java.util.List<ObjectConverter.ColumnInfo> columnsExtended = allColumns .stream() .filter( it -> "mixinReference".equals(it.typeSchema) && "-ngs_Resident_type-".equals(it.typeName)) .collect(Collectors.toList()); columnCountExtended = columnsExtended.size(); readersExtended = new ObjectConverter.Reader[columnCountExtended]; for (int i = 0; i < readersExtended.length; i++) { readersExtended[i] = (instance, rdr, ctx) -> StringConverter.skip(rdr, ctx); } column = columns.stream().filter(it -> "id".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'id' column in mixinReference Resident_entity. Check if DB is in sync"); __index___id = (int) column.get().order - 1; column = columnsExtended.stream().filter(it -> "id".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'id' column in mixinReference Resident. Check if DB is in sync"); __index__extended_id = (int) column.get().order - 1; column = columns.stream().filter(it -> "birth".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'birth' column in mixinReference Resident_entity. Check if DB is in sync"); __index___birth = (int) column.get().order - 1; column = columnsExtended.stream().filter(it -> "birth".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'birth' column in mixinReference Resident. Check if DB is in sync"); __index__extended_birth = (int) column.get().order - 1; }
/** * Search the tree for the first instance of `pattern`, giving preference to the left child. * * <p>`pattern` is a tree, representing a pattern of nodes. `NilNode`s should be considered * wildcards. * * <p>Example patterns ---------------- `Tree()` Matches any tree. * * <p>`Tree(3)` Matches any tree where the root node has a value of `3`. * * <p>`Tree(3, Tree(2), Tree())` Matches any tree where the root node has a value of `3`, and a * left sub-tree with a root value of `2`, and any (or no) right sub-tree. */ static Optional<Tree> find(Tree target, Tree pattern) { if (pattern.isEmpty()) return Optional.of(target); if (target.isEmpty()) return Optional.empty(); if (prefixMatches(target, pattern)) return Optional.of(target); Optional<Tree> leftMatch = find(target.left(), pattern); if (leftMatch.isPresent()) return leftMatch; return find(target.right(), pattern); }
@Override public Object apply(List<Object> args, Context context) throws ParseException { File outFile = null; String editor = getEditor(); try { outFile = File.createTempFile("stellar_shell", "out"); if (args.size() > 0) { String arg = (String) args.get(0); try (PrintWriter pw = new PrintWriter(outFile)) { IOUtils.write(arg, pw); } } } catch (IOException e) { String message = "Unable to create temp file: " + e.getMessage(); LOG.error(message, e); throw new IllegalStateException(message, e); } Optional<Object> console = context.getCapability(CONSOLE, false); try { PausableInput.INSTANCE.pause(); // shut down the IO for the console ProcessBuilder processBuilder = new ProcessBuilder(editor, outFile.getAbsolutePath()); processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT); processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT); processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); try { Process p = processBuilder.start(); // wait for termination. p.waitFor(); try (BufferedReader br = new BufferedReader(new FileReader(outFile))) { String ret = IOUtils.toString(br).trim(); return ret; } } catch (Exception e) { String message = "Unable to read output: " + e.getMessage(); LOG.error(message, e); return null; } } finally { try { PausableInput.INSTANCE.unpause(); if (console.isPresent()) { ((Console) console.get()).pushToInputStream("\b\n"); } } catch (IOException e) { LOG.error("Unable to unpause: " + e.getMessage(), e); } if (outFile.exists()) { outFile.delete(); } } }
/** * The core implementation of the search. * * @param root The root word to search from. Traditionally, this is the root of the sentence. * @param candidateFragments The callback for the resulting sentence fragments. This is a * predicate of a triple of values. The return value of the predicate determines whether we * should continue searching. The triple is a triple of * <ol> * <li>The log probability of the sentence fragment, according to the featurizer and the * weights * <li>The features along the path to this fragment. The last element of this is the * features from the most recent step. * <li>The sentence fragment. Because it is relatively expensive to compute the resulting * tree, this is returned as a lazy {@link Supplier}. * </ol> * * @param classifier The classifier for whether an arc should be on the path to a clause split, a * clause split itself, or neither. * @param featurizer The featurizer to use. Make sure this matches the weights! * @param actionSpace The action space we are allowed to take. Each action defines a means of * splitting a clause on a dependency boundary. */ protected void search( // The root to search from IndexedWord root, // The output specs final Predicate<Triple<Double, List<Counter<String>>, Supplier<SentenceFragment>>> candidateFragments, // The learning specs final Classifier<ClauseSplitter.ClauseClassifierLabel, String> classifier, Map<String, ? extends List<String>> hardCodedSplits, final Function<Triple<State, Action, State>, Counter<String>> featurizer, final Collection<Action> actionSpace, final int maxTicks) { // (the fringe) PriorityQueue<Pair<State, List<Counter<String>>>> fringe = new FixedPrioritiesPriorityQueue<>(); // (avoid duplicate work) Set<IndexedWord> seenWords = new HashSet<>(); State firstState = new State(null, null, -9000, null, x -> {}, true); // First state is implicitly "done" fringe.add(Pair.makePair(firstState, new ArrayList<>(0)), -0.0); int ticks = 0; while (!fringe.isEmpty()) { if (++ticks > maxTicks) { // System.err.println("WARNING! Timed out on search with " + ticks + " ticks"); return; } // Useful variables double logProbSoFar = fringe.getPriority(); assert logProbSoFar <= 0.0; Pair<State, List<Counter<String>>> lastStatePair = fringe.removeFirst(); State lastState = lastStatePair.first; List<Counter<String>> featuresSoFar = lastStatePair.second; IndexedWord rootWord = lastState.edge == null ? root : lastState.edge.getDependent(); // Register thunk if (lastState.isDone) { if (!candidateFragments.test( Triple.makeTriple( logProbSoFar, featuresSoFar, () -> { SemanticGraph copy = new SemanticGraph(tree); lastState .thunk .andThen( x -> { // Add the extra edges back in, if they don't break the tree-ness of the // extraction for (IndexedWord newTreeRoot : x.getRoots()) { if (newTreeRoot != null) { // what a strange thing to have happen... for (SemanticGraphEdge extraEdge : extraEdgesByGovernor.get(newTreeRoot)) { assert Util.isTree(x); //noinspection unchecked addSubtree( x, newTreeRoot, extraEdge.getRelation().toString(), tree, extraEdge.getDependent(), tree.getIncomingEdgesSorted(newTreeRoot)); assert Util.isTree(x); } } } }) .accept(copy); return new SentenceFragment(copy, assumedTruth, false); }))) { break; } } // Find relevant auxilliary terms SemanticGraphEdge subjOrNull = null; SemanticGraphEdge objOrNull = null; for (SemanticGraphEdge auxEdge : tree.outgoingEdgeIterable(rootWord)) { String relString = auxEdge.getRelation().toString(); if (relString.contains("obj")) { objOrNull = auxEdge; } else if (relString.contains("subj")) { subjOrNull = auxEdge; } } // Iterate over children // For each outgoing edge... for (SemanticGraphEdge outgoingEdge : tree.outgoingEdgeIterable(rootWord)) { // Prohibit indirect speech verbs from splitting off clauses // (e.g., 'said', 'think') // This fires if the governor is an indirect speech verb, and the outgoing edge is a ccomp if (outgoingEdge.getRelation().toString().equals("ccomp") && ((outgoingEdge.getGovernor().lemma() != null && INDIRECT_SPEECH_LEMMAS.contains(outgoingEdge.getGovernor().lemma())) || INDIRECT_SPEECH_LEMMAS.contains(outgoingEdge.getGovernor().word()))) { continue; } // Get some variables String outgoingEdgeRelation = outgoingEdge.getRelation().toString(); List<String> forcedArcOrder = hardCodedSplits.get(outgoingEdgeRelation); if (forcedArcOrder == null && outgoingEdgeRelation.contains(":")) { forcedArcOrder = hardCodedSplits.get( outgoingEdgeRelation.substring(0, outgoingEdgeRelation.indexOf(":")) + ":*"); } boolean doneForcedArc = false; // For each action... for (Action action : (forcedArcOrder == null ? actionSpace : orderActions(actionSpace, forcedArcOrder))) { // Check the prerequisite if (!action.prerequisitesMet(tree, outgoingEdge)) { continue; } if (forcedArcOrder != null && doneForcedArc) { break; } // 1. Compute the child state Optional<State> candidate = action.applyTo(tree, lastState, outgoingEdge, subjOrNull, objOrNull); if (candidate.isPresent()) { double logProbability; ClauseClassifierLabel bestLabel; Counter<String> features = featurizer.apply(Triple.makeTriple(lastState, action, candidate.get())); if (forcedArcOrder != null && !doneForcedArc) { logProbability = 0.0; bestLabel = ClauseClassifierLabel.CLAUSE_SPLIT; doneForcedArc = true; } else if (features.containsKey("__undocumented_junit_no_classifier")) { logProbability = Double.NEGATIVE_INFINITY; bestLabel = ClauseClassifierLabel.CLAUSE_INTERM; } else { Counter<ClauseClassifierLabel> scores = classifier.scoresOf(new RVFDatum<>(features)); if (scores.size() > 0) { Counters.logNormalizeInPlace(scores); } String rel = outgoingEdge.getRelation().toString(); if ("nsubj".equals(rel) || "dobj".equals(rel)) { scores.remove( ClauseClassifierLabel.NOT_A_CLAUSE); // Always at least yield on nsubj and dobj } logProbability = Counters.max(scores, Double.NEGATIVE_INFINITY); bestLabel = Counters.argmax(scores, (x, y) -> 0, ClauseClassifierLabel.CLAUSE_SPLIT); } if (bestLabel != ClauseClassifierLabel.NOT_A_CLAUSE) { Pair<State, List<Counter<String>>> childState = Pair.makePair( candidate.get().withIsDone(bestLabel), new ArrayList<Counter<String>>(featuresSoFar) { { add(features); } }); // 2. Register the child state if (!seenWords.contains(childState.first.edge.getDependent())) { // System.err.println(" pushing " + action.signature() + " with " + // argmax.first.edge); fringe.add(childState, logProbability); } } } } } seenWords.add(rootWord); } // System.err.println("Search finished in " + ticks + " ticks and " + classifierEvals + " // classifier evaluations."); }
@SuppressWarnings("unchecked") public Detail2Converter(List<ObjectConverter.ColumnInfo> allColumns) throws java.io.IOException { Optional<ObjectConverter.ColumnInfo> column; final java.util.List<ObjectConverter.ColumnInfo> columns = allColumns .stream() .filter(it -> "test".equals(it.typeSchema) && "Detail2_entity".equals(it.typeName)) .collect(Collectors.toList()); columnCount = columns.size(); readers = new ObjectConverter.Reader[columnCount]; for (int i = 0; i < readers.length; i++) { readers[i] = (instance, rdr, ctx) -> { StringConverter.skip(rdr, ctx); return instance; }; } final java.util.List<ObjectConverter.ColumnInfo> columnsExtended = allColumns .stream() .filter(it -> "test".equals(it.typeSchema) && "-ngs_Detail2_type-".equals(it.typeName)) .collect(Collectors.toList()); columnCountExtended = columnsExtended.size(); readersExtended = new ObjectConverter.Reader[columnCountExtended]; for (int i = 0; i < readersExtended.length; i++) { readersExtended[i] = (instance, rdr, ctx) -> { StringConverter.skip(rdr, ctx); return instance; }; } column = columns.stream().filter(it -> "u".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'u' column in test Detail2_entity. Check if DB is in sync"); __index___u = (int) column.get().order - 1; column = columnsExtended.stream().filter(it -> "u".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'u' column in test Detail2. Check if DB is in sync"); __index__extended_u = (int) column.get().order - 1; column = columns.stream().filter(it -> "dd".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'dd' column in test Detail2_entity. Check if DB is in sync"); __index___dd = (int) column.get().order - 1; column = columnsExtended.stream().filter(it -> "dd".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'dd' column in test Detail2. Check if DB is in sync"); __index__extended_dd = (int) column.get().order - 1; column = columns.stream().filter(it -> "EntityCompositeid".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'EntityCompositeid' column in test Detail2_entity. Check if DB is in sync"); __index___EntityCompositeid = (int) column.get().order - 1; column = columnsExtended.stream().filter(it -> "EntityCompositeid".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'EntityCompositeid' column in test Detail2. Check if DB is in sync"); __index__extended_EntityCompositeid = (int) column.get().order - 1; column = columns.stream().filter(it -> "EntityIndex".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'EntityIndex' column in test Detail2_entity. Check if DB is in sync"); __index___EntityIndex = (int) column.get().order - 1; column = columnsExtended.stream().filter(it -> "EntityIndex".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'EntityIndex' column in test Detail2. Check if DB is in sync"); __index__extended_EntityIndex = (int) column.get().order - 1; column = columns.stream().filter(it -> "Index".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'Index' column in test Detail2_entity. Check if DB is in sync"); __index___Index = (int) column.get().order - 1; column = columnsExtended.stream().filter(it -> "Index".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'Index' column in test Detail2. Check if DB is in sync"); __index__extended_Index = (int) column.get().order - 1; }