本文整理匯總了Java中edu.stanford.nlp.ling.CoreAnnotations類的典型用法代碼示例。如果您正苦於以下問題:Java CoreAnnotations類的具體用法?Java CoreAnnotations怎麽用?Java CoreAnnotations使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
CoreAnnotations類屬於edu.stanford.nlp.ling包,在下文中一共展示了CoreAnnotations類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: Token
import edu.stanford.nlp.ling.CoreAnnotations; //導入依賴的package包/類
public Token(Sentence sentence, CoreLabel coreLabel, int tokenIndex) {
this.sentence = sentence;
this.coreLabel = coreLabel;
this.index = tokenIndex;
// pure word
this.word = coreLabel.get(CoreAnnotations.TextAnnotation.class);
// this is the POS tag of the token
this.partOfSpeech = coreLabel.get(CoreAnnotations.PartOfSpeechAnnotation.class);
// this is the NE label of the token
this.namedEntity = coreLabel.get(CoreAnnotations.NamedEntityTagAnnotation.class);
// lema
this.lemma = coreLabel.get(CoreAnnotations.LemmaAnnotation.class);
this.characterOffsetBegin = coreLabel.get(CoreAnnotations.CharacterOffsetBeginAnnotation.class);
this.characterOffsetEnd = coreLabel.get(CoreAnnotations.CharacterOffsetEndAnnotation.class);
// resolve edge node in semantic graph for this label
this.typeDependency = findTypeDependency(sentence.semanticGraph(), coreLabel);
// stemmer
this.stem = new IteratedLovinsStemmer().stem(word);
}
示例2: getStanfordSentimentRate
import edu.stanford.nlp.ling.CoreAnnotations; //導入依賴的package包/類
public int getStanfordSentimentRate(String sentimentText) {
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
//StanfordCoreNLP
int totalRate = 0;
String[] linesArr = sentimentText.split("\\.");
for (int i = 0; i < linesArr.length; i++) {
if (linesArr[i] != null) {
Annotation annotation = pipeline.process(linesArr[i]);
for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
int score = RNNCoreAnnotations.getPredictedClass(tree);
totalRate = totalRate + (score - 2);
}
}
}
return totalRate;
}
示例3: tokenize
import edu.stanford.nlp.ling.CoreAnnotations; //導入依賴的package包/類
public LinkedList<String> tokenize(String text) {
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);
for (CoreLabel t : tokens) {
String lemma = t.lemma();
String pos = t.tag();
if (!stopwords.contains(lemma)) {
String rep = representativeProvider.getRepresentative(lemma, pos);
if (!stopwords.contains(rep)) {
res.add(rep);
}
}
}
}
}
return res;
}
示例4: annotate
import edu.stanford.nlp.ling.CoreAnnotations; //導入依賴的package包/類
protected void annotate(StanfordCoreNLP pipeline, Annotation ann) {
if (ann.get(CoreAnnotations.SentencesAnnotation.class) == null) {
pipeline.annotate(ann);
}
else {
if (ann.get(CoreAnnotations.SentencesAnnotation.class).size() == 1) {
CoreMap sentence = ann.get(CoreAnnotations.SentencesAnnotation.class).get(0);
for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
token.remove(NaturalLogicAnnotations.OperatorAnnotation.class);
token.remove(NaturalLogicAnnotations.PolarityAnnotation.class);
}
sentence.remove(NaturalLogicAnnotations.RelationTriplesAnnotation.class);
sentence.remove(NaturalLogicAnnotations.EntailedSentencesAnnotation.class);
sentence.remove(SemanticGraphCoreAnnotations.BasicDependenciesAnnotation.class);
sentence.remove(SemanticGraphCoreAnnotations.EnhancedDependenciesAnnotation.class);
sentence.remove(SemanticGraphCoreAnnotations.EnhancedPlusPlusDependenciesAnnotation.class);
pipeline.annotate(ann);
}
}
}
示例5: readResultInContents
import edu.stanford.nlp.ling.CoreAnnotations; //導入依賴的package包/類
private void readResultInContents(Layer targetLayer, List<CoreLabel> tokenList, AtomicInteger n) {
int start = 0;
int end = 0;
String prevLabel = "O";
for (int i = 0; i < tokenList.size(); ++i) {
CoreLabel token = tokenList.get(i);
String label = token.get(CoreAnnotations.AnswerAnnotation.class);
if (!label.equals(prevLabel)) {
createAnnotation(targetLayer, start, end, prevLabel, n);
start = token.get(CoreAnnotations.CharacterOffsetBeginAnnotation.class);
prevLabel = label;
}
end = token.get(CoreAnnotations.CharacterOffsetEndAnnotation.class);
}
createAnnotation(targetLayer, start, end, prevLabel, n);
}
示例6: readResultInSentence
import edu.stanford.nlp.ling.CoreAnnotations; //導入依賴的package包/類
private void readResultInSentence(Layer targetLayer, Layer sentence, List<CoreLabel> tokenList, AtomicInteger n) {
int start = 0;
int end = 0;
String prevLabel = "O";
for (int i = 0; i < tokenList.size(); ++i) {
Annotation w = sentence.get(i);
CoreLabel token = tokenList.get(i);
String label = token.get(CoreAnnotations.AnswerAnnotation.class);
if (!label.equals(prevLabel)) {
createAnnotation(targetLayer, start, end, prevLabel, n);
start = w.getStart();
prevLabel = label;
}
end = w.getEnd();
}
createAnnotation(targetLayer, start, end, prevLabel, n);
}
示例7: preprocess
import edu.stanford.nlp.ling.CoreAnnotations; //導入依賴的package包/類
public Concept preprocess(Concept c) {
if (this.tagger == null)
this.tagger = new MaxentTagger("ext_models/pos_tagger/english-left3words-distsim.tagger");
if (this.ner == null)
this.ner = CRFClassifier.getClassifierNoExceptions("ext_models/ner/english.all.3class.distsim.crf.ser.gz");
List<CoreLabel> words = tokFactory.getTokenizer(new StringReader(c.name)).tokenize();
tagger.tagCoreLabels(words);
words = ner.classifySentence(words);
words = this.addLemmas(words);
List<PToken> tokens = new ArrayList<PToken>();
for (CoreLabel word : words) {
PToken t = new PToken(word.originalText());
t.pos = word.tag();
t.neTag = word.get(CoreAnnotations.AnswerAnnotation.class);
t.lemma = word.get(LemmaAnnotation.class);
tokens.add(t);
}
c.tokenList = tokens;
return c;
}
示例8: prepareSUTParser
import edu.stanford.nlp.ling.CoreAnnotations; //導入依賴的package包/類
/**
* Prepares the check for a temporal expression.
*
* @param cell
* Holds the column´s cell
* @param pipeline
* Used for temporal expressions.
* @param result
* Holds the intermediate result before executing this operation.
* @return Holds the intermediate result after executing this operation.
*/
private int prepareSUTParser(String cell, AnnotationPipeline pipeline,
int result) {
if ((!cell.trim().isEmpty()) && (!cell.trim().equals("-")
&& !cell.trim().equals("--") && !cell.trim().equals("---")
&& !cell.trim().equals("n/a") && !cell.trim().equals("N/A")
&& !cell.trim().equals("(n/a)")
&& !cell.trim().equals("Unknown")
&& !cell.trim().equals("unknown") && !cell.trim().equals("?")
&& !cell.trim().equals("??") && !cell.trim().equals(".")
&& !cell.trim().equals("null") && !cell.trim().equals("NULL")
&& !cell.trim().equals("Null"))) {
Annotation annotation = new Annotation(cell);
annotation.set(CoreAnnotations.DocDateAnnotation.class,
"2013-07-14");
pipeline.annotate(annotation);
List<CoreMap> timexAnnsAll = annotation
.get(TimeAnnotations.TimexAnnotations.class);
if (timexAnnsAll != null)
if (!timexAnnsAll.isEmpty())
result++;
}
return result;
}
示例9: main
import edu.stanford.nlp.ling.CoreAnnotations; //導入依賴的package包/類
public static void main(String[] args) {
// 載入自定義的Properties文件
StanfordCoreNLP pipeline = new StanfordCoreNLP("CoreNLP-chinese.properties");
// 用一些文本來初始化一個注釋。文本是構造函數的參數。
Annotation annotation;
annotation = pipeline.process("我愛北京天安門");
// 從注釋中獲取CoreMap List,並取第0個值
List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
CoreMap sentence = sentences.get(0);
// 從CoreMap中取出CoreLabel List,逐一打印出來
List<CoreLabel> tokens = sentence.get(CoreAnnotations.TokensAnnotation.class);
System.out.println("字/詞");
System.out.println("-----------------------------");
for (CoreLabel token : tokens) {
String word = token.getString(TextAnnotation.class);
// String pos = token.getString(PartOfSpeechAnnotation.class);
// String ner = token.getString(NamedEntityTagAnnotation.class);
System.out.println(word);
}
}
示例10: findSentiment
import edu.stanford.nlp.ling.CoreAnnotations; //導入依賴的package包/類
public static int findSentiment(String tweet) {
int mainSentiment = 0;
if (tweet != null && tweet.length() > 0) {
int longest = 0;
Annotation annotation = pipeline.process(tweet);
for (CoreMap sentence : annotation
.get(CoreAnnotations.SentencesAnnotation.class)) {
Tree tree = sentence
.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
int sentiment = RNNCoreAnnotations.getPredictedClass(tree);
String partText = sentence.toString();
if (partText.length() > longest) {
mainSentiment = sentiment;
longest = partText.length();
}
}
}
return mainSentiment;
}
示例11: lemmatizer
import edu.stanford.nlp.ling.CoreAnnotations; //導入依賴的package包/類
/** @return Lemmatized string, input string can be a word, sentence or paragraph */
public static String lemmatizer(String string) {
List<String> lemmas = new ArrayList<>();
// create an empty Annotation just with the given text
Annotation annotation = new Annotation(string);
// run all Annotators on this string
pipeline.annotate(annotation);
// Iterate over all of the sentences found
List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
for (CoreMap sentence : sentences) {
// Iterate over all tokens in a sentence
for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
// Retrieve and add the lemma for each word into the list of lemmas
lemmas.add(token.get(CoreAnnotations.LemmaAnnotation.class));
}
}
return String.join(" ", lemmas);
}
示例12: runRaw
import edu.stanford.nlp.ling.CoreAnnotations; //導入依賴的package包/類
public Annotation runRaw(String text, @Nullable StanfordCoreNLP pipeline) {
load();
Annotation annotation = new Annotation(text);
LOGGER.debug("Text: {}", text);
if (documentDate == null) {
documentDate = Instant.now().toString().substring(0, 10);
}
annotation.set(CoreAnnotations.DocDateAnnotation.class, documentDate);
if (pipeline == null) {
pipeline = new StanfordCoreNLP(props);
}
pipeline.annotate(annotation);
annotation.set(TimingAnnotations.TimingAnnotation.class, pipeline.timingInformation());
return annotation;
}
示例13: requirementsSatisfied
import edu.stanford.nlp.ling.CoreAnnotations; //導入依賴的package包/類
@Override public Set<Class<? extends CoreAnnotation>> requirementsSatisfied() {
return new HashSet<>(Arrays.asList(
CoreAnnotations.TextAnnotation.class,
CoreAnnotations.TokensAnnotation.class,
CoreAnnotations.CharacterOffsetBeginAnnotation.class,
CoreAnnotations.CharacterOffsetEndAnnotation.class,
CoreAnnotations.BeforeAnnotation.class,
CoreAnnotations.AfterAnnotation.class,
CoreAnnotations.TokenBeginAnnotation.class,
CoreAnnotations.TokenEndAnnotation.class,
CoreAnnotations.PositionAnnotation.class,
CoreAnnotations.IndexAnnotation.class,
CoreAnnotations.OriginalTextAnnotation.class,
CoreAnnotations.ValueAnnotation.class,
CoreAnnotations.SentencesAnnotation.class,
CoreAnnotations.SentenceIndexAnnotation.class
));
}
示例14: addDescriptionForm
import edu.stanford.nlp.ling.CoreAnnotations; //導入依賴的package包/類
static public void addDescriptionForm(String form, HashMap<Integer, Integer> indexes, int start,
int numberOfTokens, TreeMap<Integer, DescriptionForm> forms, Annotation annotation,
HashMap<String, GlossarioEntry> glossario) {
Integer lemmaIndex = indexes.get(start);
if (lemmaIndex == null) {
return;
}
CoreLabel firstToken = annotation.get(CoreAnnotations.TokensAnnotation.class).get(lemmaIndex);
CoreLabel endToken = annotation.get(CoreAnnotations.TokensAnnotation.class)
.get(lemmaIndex + numberOfTokens - 1);
Integer beginOffset = firstToken.get(CoreAnnotations.CharacterOffsetBeginAnnotation.class);
Integer endOffset = endToken.get(CoreAnnotations.CharacterOffsetEndAnnotation.class);
GlossarioEntry glossarioEntry = glossario.get(form);
if (glossarioEntry == null) {
return;
}
DescriptionForm descriptionForm = new DescriptionForm(
beginOffset, endOffset, glossarioEntry);
forms.put(beginOffset, descriptionForm);
}
示例15: addingContentWord
import edu.stanford.nlp.ling.CoreAnnotations; //導入依賴的package包/類
@Override public void addingContentWord(CoreLabel token) {
super.addingContentWord(token);
HashMap<Integer, HashMultimap<String, String>> easyWords = model.getEasyWords();
String simplePos = getGenericPos(token.get(CoreAnnotations.PartOfSpeechAnnotation.class));
String lemma = token.get(CoreAnnotations.LemmaAnnotation.class);
token.set(ReadabilityAnnotations.DifficultyLevelAnnotation.class, 4);
if (easyWords.get(3).get(simplePos).contains(lemma)) {
level3WordSize++;
token.set(ReadabilityAnnotations.DifficultyLevelAnnotation.class, 3);
}
if (easyWords.get(2).get(simplePos).contains(lemma)) {
level2WordSize++;
token.set(ReadabilityAnnotations.DifficultyLevelAnnotation.class, 2);
}
if (easyWords.get(1).get(simplePos).contains(lemma)) {
level1WordSize++;
token.set(ReadabilityAnnotations.DifficultyLevelAnnotation.class, 1);
}
}