-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoreferenceResolutionTrueCase.java
More file actions
53 lines (46 loc) · 2.41 KB
/
Copy pathcoreferenceResolutionTrueCase.java
File metadata and controls
53 lines (46 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.io.*;
import java.util.*;
import edu.stanford.nlp.coref.CorefCoreAnnotations;
import edu.stanford.nlp.coref.data.CorefChain;
import edu.stanford.nlp.io.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.util.*;
public class coreferenceResolutionTrueCase {
public static void main(String[] args) throws Exception {
PrintWriter out = new PrintWriter("outFile.txt");
PrintWriter xmlOut = new PrintWriter("outFile.xml");
Annotation document = new Annotation("Barack Obama went to Russia. He was impressed by it.");
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
Annotation annotation = new Annotation(document);
pipeline.annotate(annotation);
pipeline.prettyPrint(annotation, out);
if (xmlOut != null) {
pipeline.xmlPrint(annotation, xmlOut);
}
List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
if (sentences != null && !sentences.isEmpty()) {
System.out.println("Coreference information");
Map<Integer, CorefChain> corefChains =
annotation.get(CorefCoreAnnotations.CorefChainAnnotation.class);
if (corefChains == null) {
return;
}
for (Map.Entry<Integer, CorefChain> entry : corefChains.entrySet()) {
System.out.println("Chain " + entry.getKey());
for (CorefChain.CorefMention m : entry.getValue().getMentionsInTextualOrder()) {
// We need to subtract one since the indices count from 1 but the Lists start from 0
List<CoreLabel> tokens = sentences.get(m.sentNum - 1).get(CoreAnnotations.TokensAnnotation.class);
// We subtract two for end: one for 0-based indexing, and one because we want last token of mention not one following.
System.out.println(" " + m + ", i.e., 0-based character offsets [" + tokens.get(m.startIndex - 1).beginPosition() +
", " + tokens.get(m.endIndex - 2).endPosition() + ")");
}
}
System.out.println();
}
IOUtils.closeIgnoringExceptions(out);
IOUtils.closeIgnoringExceptions(xmlOut);
}
}