Example usage for edu.stanford.nlp.util Triple setSecond

List of usage examples for edu.stanford.nlp.util Triple setSecond

Introduction

In this page you can find the example usage for edu.stanford.nlp.util Triple setSecond.

Prototype

public void setSecond(T2 o) 

Source Link

Usage

From source file:LVCoref.Document.java

License:Open Source License

private static List<Triple<Integer, Integer, String>> getLabelledSpans(Sentence sent, int fieldIndex,
        String defaultMarker, boolean checkEndLabel) {
    List<Triple<Integer, Integer, String>> spans = new ArrayList<Triple<Integer, Integer, String>>();
    Stack<Triple<Integer, Integer, String>> openSpans = new Stack<Triple<Integer, Integer, String>>();
    for (int wordPos = 0; wordPos < sent.getSize(); wordPos++) {
        String val = sent.getNode(wordPos).getField(fieldIndex);
        if (!defaultMarker.equals(val)) {
            int openParenIndex = -1;
            int lastDelimiterIndex = -1;
            for (int j = 0; j < val.length(); j++) {
                char c = val.charAt(j);
                boolean isDelimiter = false;
                if (c == '(' || c == ')' || c == '|') {
                    if (openParenIndex >= 0) {
                        String s = val.substring(openParenIndex + 1, j);
                        //                     if (removeStar) {
                        //                        s = starPattern.matcher(s).replaceAll("");
                        //                     }
                        openSpans.push(new Triple<Integer, Integer, String>(wordPos, -1, s));
                        openParenIndex = -1;
                    }/*from  w w w. j  a  v  a 2s.  c o m*/
                    isDelimiter = true;
                }
                if (c == '(') {
                    openParenIndex = j;
                } else if (c == ')') {
                    Triple<Integer, Integer, String> t = openSpans.pop();
                    if (checkEndLabel) {
                        // NOTE: end parens may cross (usually because mention either start or end on the same token
                        // and it is just an artifact of the ordering
                        String s = val.substring(lastDelimiterIndex + 1, j);
                        if (!s.equals(t.third())) {
                            Stack<Triple<Integer, Integer, String>> saved = new Stack<Triple<Integer, Integer, String>>();
                            while (!s.equals(t.third())) {
                                // find correct match
                                saved.push(t);
                                if (openSpans.isEmpty()) {
                                    throw new RuntimeException("Cannot find matching labelled span for " + s);
                                }
                                t = openSpans.pop();
                            }
                            while (!saved.isEmpty()) {
                                openSpans.push(saved.pop());
                            }
                            assert (s.equals(t.third()));
                        }
                    }
                    t.setSecond(wordPos);
                    spans.add(t);
                }
                if (isDelimiter) {
                    lastDelimiterIndex = j;
                }
            }
            if (openParenIndex >= 0) {
                String s = val.substring(openParenIndex + 1, val.length());
                //               if (removeStar) {
                //                  s = starPattern.matcher(s).replaceAll("");
                //               }
                openSpans.push(new Triple<Integer, Integer, String>(wordPos, -1, s));
            }
        }
    }
    if (openSpans.size() != 0) {
        throw new RuntimeException("Error extracting labelled spans for column " + fieldIndex + ": " + sent);
    }
    return spans;
}