Search in sources :

Example 31 with Pair

use of edu.illinois.cs.cogcomp.core.datastructures.Pair in project cogcomp-nlp by CogComp.

the class PredicateArgumentEvaluator method getArgumentMap.

/**
     * This is an annoying function to write. It is probably VERY inefficient too...
     */
private Map<IntPair, Record> getArgumentMap(PredicateArgumentView view, Constituent predicate) {
    Set<IntPair> spans = new HashSet<>();
    List<Pair<String, Constituent>> output = new ArrayList<>();
    for (Relation r : view.getArguments(predicate)) {
        Constituent target = r.getTarget();
        output.add(new Pair<>(r.getRelationName(), target));
        if (spans.contains(target.getSpan()))
            logger.error("Error! Overlapping spans in " + view.getViewName() + "\n" + view.getTextAnnotation() + "\n" + view);
        spans.add(target.getSpan());
    }
    Collections.sort(output, new Comparator<Pair<String, Constituent>>() {

        public int compare(Pair<String, Constituent> arg0, Pair<String, Constituent> arg1) {
            return TextAnnotationUtilities.constituentStartComparator.compare(arg0.getSecond(), arg1.getSecond());
        }
    });
    List<Record> records = new ArrayList<>();
    // add a label for the verb first
    Record vRecord = new Record(predicate.getStartSpan(), predicate.getEndSpan(), "V");
    records.add(vRecord);
    Map<String, Record> recordsSoFar = new HashMap<>();
    recordsSoFar.put("V", vRecord);
    for (Pair<String, Constituent> pair : output) {
        Constituent c = pair.getSecond();
        String label = pair.getFirst().replaceAll("Support", "SUP");
        if (label.startsWith("C-")) {
            String baseLabel = label.replaceAll("C-", "");
            if (recordsSoFar.containsKey(baseLabel)) {
                Record record = recordsSoFar.get(baseLabel);
                record.start = Math.min(c.getStartSpan(), record.start);
                record.end = Math.max(c.getEndSpan(), record.end);
                assert record.baseLabel.equals(baseLabel);
                record.components.put(c.getSpan(), label);
            } else {
                // a dangling C-arg. This should never happen, but one never knows.
                // Simply treat this C-arg as arg.
                Record record = new Record(c.getStartSpan(), c.getEndSpan(), baseLabel);
                recordsSoFar.put(baseLabel, record);
                records.add(record);
            }
        } else {
            Record record = new Record(c.getStartSpan(), c.getEndSpan(), label);
            recordsSoFar.put(label, record);
            records.add(record);
        }
    }
    Map<IntPair, Record> map = new HashMap<>();
    for (Record rec : records) {
        map.put(new IntPair(rec.start, rec.end), rec);
    }
    return map;
}
Also used : IntPair(edu.illinois.cs.cogcomp.core.datastructures.IntPair) IntPair(edu.illinois.cs.cogcomp.core.datastructures.IntPair) Pair(edu.illinois.cs.cogcomp.core.datastructures.Pair)

Example 32 with Pair

use of edu.illinois.cs.cogcomp.core.datastructures.Pair in project cogcomp-nlp by CogComp.

the class ClassificationTester method getConfusionTable.

public Pair<Table, List<String>> getConfusionTable() {
    Table table = new Table();
    Set<String> set = new HashSet<>();
    for (String item : counter.items()) {
        String[] split = item.split("\\.");
        set.add(split[0]);
        set.add(split[1]);
    }
    List<String> sortSet = new ArrayList<>(set);
    Collections.sort(sortSet);
    table.addColumn("Gold");
    for (int i = 0; i < sortSet.size(); i++) {
        table.addColumn(i + "");
    }
    int id = 0;
    for (String label : sortSet) {
        List<String> s = new ArrayList<>();
        s.add(id + "");
        id++;
        for (String pLabel : sortSet) {
            int count = (int) counter.getCount(label + "." + pLabel);
            s.add(Integer.toString(count));
        }
        table.addRow(s.toArray(new String[s.size()]));
    }
    return new Pair<>(table, sortSet);
}
Also used : Table(edu.illinois.cs.cogcomp.core.utilities.Table) Pair(edu.illinois.cs.cogcomp.core.datastructures.Pair)

Example 33 with Pair

use of edu.illinois.cs.cogcomp.core.datastructures.Pair in project cogcomp-nlp by CogComp.

the class TreeView method setScoredParseTree.

/**
     * Set the parse tree of the {@code sentenceId}th sentence.
     */
public void setScoredParseTree(int sentenceId, Tree<String> tree, Tree<Double> scores) {
    safeInitializeTrees();
    if (!this.getViewName().startsWith("PARSE")) {
        throw new IllegalStateException("Cannot set a Tree<String> object " + "as the dependency tree." + " Need a Tree<String, Integer> " + "to recover dependency token information. ");
    }
    this.trees.set(sentenceId, tree);
    Tree<Pair<String, IntPair>> spanLabeledTree = ParseUtils.getSpanLabeledTree(tree);
    int sentenceStart = getSentenceStart(sentenceId);
    Pair<String, IntPair> rootLabel = spanLabeledTree.getLabel();
    IntPair rootSpan = rootLabel.getSecond();
    int rootStart = rootSpan.getFirst() + sentenceStart;
    int rootEnd = rootSpan.getSecond() + sentenceStart;
    Constituent root = createNewConstituent(rootStart, rootEnd, rootLabel.getFirst(), scores.getLabel());
    this.addConstituent(root);
    addScoredParseTree(spanLabeledTree, scores, root, this.getTextAnnotation().getSentence(sentenceId).getStartSpan());
}
Also used : IntPair(edu.illinois.cs.cogcomp.core.datastructures.IntPair) IntPair(edu.illinois.cs.cogcomp.core.datastructures.IntPair) Pair(edu.illinois.cs.cogcomp.core.datastructures.Pair)

Example 34 with Pair

use of edu.illinois.cs.cogcomp.core.datastructures.Pair in project cogcomp-nlp by CogComp.

the class StringTransformation method transformString.

/**
     * Modify the current version of the transformed text (as returned by getTransformedText()) by replacing the
     *    string between character offsets textStart and textEnd with newStr.
     * @param textStart character offset start of edit in transformed text
     * @param textEnd character offset end of edit in transformed text
     * @param newStr string to replace specified character span
     * @return the offsets in the current, internally transformed text corresponding to textStart and textEnd
     */
public IntPair transformString(int textStart, int textEnd, String newStr) {
    int start = textStart;
    int end = textEnd;
    // need updated offsets for return value -- e.g. to use as key for transform attributes
    if (isModified) {
        start = computeCurrentOffset(textStart);
        end = computeCurrentOffset(textEnd);
        if (start < 0 || end < 0) {
            throw new IllegalStateException("ERROR: edit affects deleted span (offsets are negative). Reorder " + "edits or filter overlapping edits.");
        }
    }
    // compute the net change in offset: negative for deletion/reduction, positive for insertion,
    //   zero for same-length substitution; store with indexes in current transformed text
    int newLen = newStr.length();
    int origLen = textEnd - textStart;
    int netDiff = newLen - origLen;
    EditType editType = EditType.SUBST;
    if (netDiff != 0) {
        // else just replaced, no offset changes needed.
        // for insertion, add the modifier at the end of the original span
        int putIndex = textStart + origLen;
        if (netDiff < 0) {
            // involves deleting chars: after new str, modify the offsets
            putIndex = textStart + newLen;
            editType = (newLen == 0) ? EditType.DELETE : EditType.REDUCE;
        } else
            // expanding or inserting
            editType = (origLen == 0) ? EditType.INSERT : EditType.EXPAND;
        // account for any previous modifications at this index
        if (currentOffsetModifications.containsKey(putIndex))
            netDiff += currentOffsetModifications.get(putIndex).getFirst();
        currentOffsetModifications.put(putIndex, new Pair(new Integer(netDiff), editType));
    }
    IntPair transformOffsets = new IntPair(start, end);
    String origStr = transformedText.substring(textStart, textEnd);
    // edit offsets encode affected substring allowing for previous edits in current pass
    edits.add(new Edit(transformOffsets, origStr, newStr, editType));
    isModified = true;
    return transformOffsets;
}
Also used : IntPair(edu.illinois.cs.cogcomp.core.datastructures.IntPair) IntPair(edu.illinois.cs.cogcomp.core.datastructures.IntPair) Pair(edu.illinois.cs.cogcomp.core.datastructures.Pair)

Example 35 with Pair

use of edu.illinois.cs.cogcomp.core.datastructures.Pair in project cogcomp-nlp by CogComp.

the class CollinsHeadDependencyParser method makeDepTree.

private Tree<Pair<String, Integer>> makeDepTree(Constituent parseTreeRoot) {
    if (TreeView.isLeaf(parseTreeRoot)) {
        int position = parseTreeRoot.getStartSpan();
        return new Tree<>(new Pair<>(parseTreeRoot.getLabel(), position));
    }
    Constituent headChild = headFinder.getHeadChild(parseTreeRoot);
    Tree<Pair<String, Integer>> rootTree = null;
    List<Tree<Pair<String, Integer>>> dependentTrees = new ArrayList<>();
    List<Pair<String, Integer>> edgeLabels = new ArrayList<>();
    int conjunction = -1;
    for (Relation childEdge : parseTreeRoot.getOutgoingRelations()) {
        Constituent child = childEdge.getTarget();
        if (child == headChild) {
            rootTree = makeDepTree(child);
        } else {
            dependentTrees.add(makeDepTree(child));
            edgeLabels.add(getEdgeLabel(parseTreeRoot, headChild.getLabel(), child));
            if (child.getLabel().equals("CC")) {
                conjunction = dependentTrees.size() - 1;
            }
        }
    }
    if (conjunction >= 0) {
        return doConjunctionHack(parseTreeRoot, headChild, rootTree, dependentTrees, edgeLabels, conjunction);
    } else {
        for (int i = 0; i < dependentTrees.size(); i++) {
            rootTree.addSubtree(dependentTrees.get(i), edgeLabels.get(i));
        }
        return rootTree;
    }
}
Also used : Relation(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Relation) ArrayList(java.util.ArrayList) Tree(edu.illinois.cs.cogcomp.core.datastructures.trees.Tree) Constituent(edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent) Pair(edu.illinois.cs.cogcomp.core.datastructures.Pair)

Aggregations

Pair (edu.illinois.cs.cogcomp.core.datastructures.Pair)59 IntPair (edu.illinois.cs.cogcomp.core.datastructures.IntPair)35 ArrayList (java.util.ArrayList)17 Constituent (edu.illinois.cs.cogcomp.core.datastructures.textannotation.Constituent)10 Tree (edu.illinois.cs.cogcomp.core.datastructures.trees.Tree)10 TextAnnotation (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TextAnnotation)7 Matcher (java.util.regex.Matcher)7 Paragraph (edu.illinois.cs.cogcomp.nlp.corpusreaders.aceReader.Paragraph)6 HashMap (java.util.HashMap)6 Pattern (java.util.regex.Pattern)6 TreeView (edu.illinois.cs.cogcomp.core.datastructures.textannotation.TreeView)3 SenseInstance (edu.illinois.cs.cogcomp.verbsense.jlis.SenseInstance)3 SenseStructure (edu.illinois.cs.cogcomp.verbsense.jlis.SenseStructure)3 JsonObject (com.google.gson.JsonObject)2 AnnotatorException (edu.illinois.cs.cogcomp.annotation.AnnotatorException)2 ITransformer (edu.illinois.cs.cogcomp.core.transformers.ITransformer)2 IndexedWord (edu.stanford.nlp.ling.IndexedWord)2 Annotation (edu.stanford.nlp.pipeline.Annotation)2 CoreMap (edu.stanford.nlp.util.CoreMap)2 LinkedHashSet (java.util.LinkedHashSet)2