Annotation
构造
List<CoreMap>sentences函数可设置文档。
您要为每个句子创建一个
CoreMap对象,如下所示。(请注意,我还分别向每个句子和标记对象添加了一个句子和标记索引。)
int sentenceIdx = 1;List<CoreMap> sentences = new ArrayList<CoreMap>();for (parsedSentence : parsedSentences) { CoreMap sentence = new CoreLabel(); List<CoreLabel> tokens = new ArrayList<>(); for(int tokenCount = 0; tokenCount < parsedSentence.size(); tokenCount++) { ArrayList<String> parsedLine = parsedSentence.get(tokenCount); String word = parsedLine.get(1); String lemma = parsedLine.get(2); String posTag = parsedLine.get(3); String namedEntity = parsedLine.get(4); String partOfParseTree = parsedLine.get(6); CoreLabel token = new CoreLabel(); token.setWord(word); token.setLemma(lemma); token.setTag(posTag); token.setNER(namedEntity); token.setIndex(tokenCount + 1); tokens.add(token); } // set tokens annotations and id of sentence sentence.set(TokensAnnotation.class, tokens); sentence.set(SentenceIndexAnnotation.class, sentenceIdx++); // set parse tree annotations to annotation Tree stanfordParseTree = Tree.valueOf(inputParseTree); sentence.set(TreeAnnotation.class, stanfordParseTree); // add sentence to list of sentences sentences.add(sentence);}
然后,您可以
Annotation使用
sentences列表创建一个实例:
Annotation annotation = new Annotation(sentences);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)