本文整理匯總了Java中edu.stanford.nlp.ling.CoreLabel.beginPosition方法的典型用法代碼示例。如果您正苦於以下問題:Java CoreLabel.beginPosition方法的具體用法?Java CoreLabel.beginPosition怎麽用?Java CoreLabel.beginPosition使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類edu.stanford.nlp.ling.CoreLabel
的用法示例。
在下文中一共展示了CoreLabel.beginPosition方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: tokenizeBySentence
import edu.stanford.nlp.ling.CoreLabel; //導入方法依賴的package包/類
public LinkedList<LinkedList<String>> tokenizeBySentence(String text, LinkedList<int[]> boundaries) {
LinkedList<LinkedList<String>> res = new LinkedList<>();
if (text != null) {
Annotation qaTokens = new Annotation(text);
pipelineTokens.annotate(qaTokens);
List<CoreMap> qssTokens = qaTokens.get(CoreAnnotations.SentencesAnnotation.class);
for (CoreMap sentenceTokens : qssTokens) {
ArrayList<CoreLabel> tokens = (ArrayList<CoreLabel>) sentenceTokens.get(CoreAnnotations.TokensAnnotation.class);
LinkedList<String> sentence=new LinkedList<>();
boolean first=true;
int[] bounds=new int[2];
CoreLabel last=null;
for (CoreLabel t : tokens) {
if (first) {
bounds[0]=t.beginPosition();
first=false;
}
last=t;
String lemma = t.lemma();
String pos = t.tag();
if (!stopwords.contains(lemma)) {
String rep = representativeProvider.getRepresentative(lemma, pos);
if (!stopwords.contains(rep)) {
sentence.add(rep);
}
}
}
bounds[1]=last.endPosition();
if (sentence.size()>0) {
res.add(sentence);
boundaries.add(bounds);
}
}
}
return res;
}
示例2: addWord
import edu.stanford.nlp.ling.CoreLabel; //導入方法依賴的package包/類
public void addWord(CoreLabel token) {
token.set(ReadabilityAnnotations.ContentWord.class, false);
token.set(ReadabilityAnnotations.LiteralWord.class, false);
String pos = token.get(CoreAnnotations.PartOfSpeechAnnotation.class);
// String lemma = token.get(CoreAnnotations.LemmaAnnotation.class);
String word = token.word();
addingToken(token);
if (isWordPos(pos)) {
addingWord(token);
wordCount++;
docLenLettersOnly += token.endPosition() - token.beginPosition();
word = flattenToAscii(word);
Hyphenation hyphenation = hyphenator.hyphenate(word);
boolean done = false;
if (hyphenation != null) {
try {
String h = hyphenation.toString();
incrementHyphenCount(hyphenation.length() + 1);
token.set(ReadabilityAnnotations.HyphenationAnnotation.class, h);
done = true;
hyphenWordCount++;
} catch (Exception e) {
// ignored
}
}
if (!done && word.length() < 5) {
incrementHyphenCount(1);
hyphenWordCount++;
}
if (isContentPos(pos)) {
contentWordSize++;
addingContentWord(token);
}
if (isEasyPos(pos)) {
contentEasyWordSize++;
addingEasyWord(token);
}
}
if (token.get(ReadabilityAnnotations.HyphenationAnnotation.class) == null) {
token.set(ReadabilityAnnotations.HyphenationAnnotation.class, token.originalText());
}
String genericPos = getGenericPos(pos);
posStats.add(pos);
genericPosStats.add(genericPos);
}
示例3: annote
import edu.stanford.nlp.ling.CoreLabel; //導入方法依賴的package包/類
@Override
void annote(Annotation annotation, List<CoreMap> sentences) {
List<CoreLabel> labels;
CoreLabel label;
CoreMap sentence;
SemanticGraph graph;
String tokens[][], tags[][], lemmas[][], words[];
DependencyTree[] dtree;
Iterator<Lexel> lexels;
List<Integer> indexes;
Lexel lexel = null;
String id;
int size, length, position;
size = sentences.size();
tokens = new String[size][];
tags = new String[size][];
lemmas = new String[size][];
dtree = new DependencyTree[size];
lexels = annotation.iterator();
id = annotation.getID();
indexes = mIndex.get(id);
for (int i = 0; i < size; i++) {
sentence = sentences.get(i);
labels = sentence.get(TokensAnnotation.class);
length = labels.size();
words = new String[length];
tags[i] = new String[length];
lemmas[i] = new String[length];
for (int j = 0; j < length; j++) {
label = labels.get(j);
words[j] = label.get(TextAnnotation.class);
tags[i][j] = label.get(PartOfSpeechAnnotation.class);
lemmas[i][j] = label.get(LemmaAnnotation.class);
position = label.beginPosition();
if (!indexes.isEmpty() && position > indexes.get(0)) {
lexel.setOffset(j - lexel.getTokenIndex() - 1);
lexel = null;
indexes.remove(0);
}
if (!indexes.isEmpty() && position == indexes.get(0)) {
lexel = lexels.next();
lexel.setIndexes(i, j);
indexes.remove(0);
}
}
if (lexel != null)
lexel.setOffset(length - lexel.getTokenIndex() - 1);
tokens[i] = words;
graph = sentence.get(BasicDependenciesAnnotation.class);
if (graph != null)
dtree[i] = getDependencyTree(graph);
}
annotation.annote(tokens, mPOS ? tags : null, mLemma ? lemmas : null, mDepParse ? dtree : null);
mIndex.remove(id);
}
示例4: parse
import edu.stanford.nlp.ling.CoreLabel; //導入方法依賴的package包/類
@Override
public ParseResult parse(String text, Set<Pair<Integer,Integer>> entities) {
ParseResult result = new ParseResult();
Annotation document = new Annotation(text);
pipeline.annotate(document);
int i = 0;
List<CoreMap> annotations = document.get(SentencesAnnotation.class);
for (CoreMap s: annotations) {
i++;
// Sentence string
result.addSentence(i,s.toString());
// Tokens and POS tags
for (CoreLabel token: s.get(TokensAnnotation.class)) {
result.addToken(i,token.index(),token.originalText());
result.addPOS(i,token.index(),token.getString(PartOfSpeechAnnotation.class));
// Mark named entities
if (entities == null) {
// Use Stanford NEs
if (!token.get(NamedEntityTagAnnotation.class).equals("O")) {
result.addPOS(i,token.index(),"NE");
}
} else {
// Use NEs provided in input
for (Pair<Integer,Integer> entity : entities) {
if (entity.getLeft() <= token.beginPosition()
&& entity.getRight() >= token.endPosition())
result.addPOS(i,token.index(),"NE");
}
}}
// Dependency parse
SemanticGraph dependencies = s.get(BasicDependenciesAnnotation.class);
result.addParse(i,dependencies.toList().trim());
}
return result;
}