本文整理汇总了Java中edu.stanford.nlp.util.ArrayCoreMap类的典型用法代码示例。如果您正苦于以下问题:Java ArrayCoreMap类的具体用法?Java ArrayCoreMap怎么用?Java ArrayCoreMap使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ArrayCoreMap类属于edu.stanford.nlp.util包,在下文中一共展示了ArrayCoreMap类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: tokenizeFromFile
import edu.stanford.nlp.util.ArrayCoreMap; //导入依赖的package包/类
/**
* Reads stdin through a buffer and runs only the tokenize annotator to the
* StanfordCoreNLP pipeline. Tokens are printed to stdout one per line.
*
* @param filePath
* The file to run the tagger on
*/
private static void tokenizeFromFile(String filePath) {
Properties properties = new Properties();
properties.setProperty("annotators", "tokenize");
StanfordCoreNLP pipeline = new StanfordCoreNLP(properties);
PrintWriter out = new PrintWriter(System.out);
File dataFile = new File(filePath);
Annotation annotation = null;
try {
annotation = new Annotation(IOUtils.slurpFile(dataFile));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pipeline.annotate(annotation);
for (CoreMap token : annotation
.get(CoreAnnotations.TokensAnnotation.class)) {
ArrayCoreMap aToken = (ArrayCoreMap) token;
out.println(aToken.toShortString(' ', "Text"));
out.flush();
}
}
示例2: concreteSectionToCoreMapList
import edu.stanford.nlp.util.ArrayCoreMap; //导入依赖的package包/类
public static List<CoreMap> concreteSectionToCoreMapList(final Section sect, final String commText) {
List<CoreMap> toRet = new ArrayList<>();
List<Sentence> sentList = sect.getSentenceList();
int tokOffset = 0;
for (int i = 0; i < sentList.size(); i++) {
Sentence st = sentList.get(i);
CoreMap cm = new ArrayCoreMap();
cm.set(SentenceIndexAnnotation.class, i);
final TextSpan sts = st.getTextSpan();
final int sentCharStart = sts.getStart();
final int sentCharEnd = sts.getEnding();
LOGGER.debug("Setting stanford sentence BeginChar = {}", sentCharStart);
cm.set(CharacterOffsetBeginAnnotation.class, sentCharStart);
LOGGER.debug("Setting stanford sentence EndChar = {}", sentCharEnd);
cm.set(CharacterOffsetEndAnnotation.class, sentCharEnd);
String sectText = commText.substring(sentCharStart, sentCharEnd);
LOGGER.debug("Setting text: {}", sectText);
cm.set(TextAnnotation.class, sectText);
Tokenization tkz = st.getTokenization();
List<CoreLabel> clList = tokenizationToCoreLabelList(tkz, i, sentCharStart);
final int maxIdx = clList.size();
LOGGER.debug("Setting stanford sentence token begin: {}", tokOffset);
cm.set(TokenBeginAnnotation.class, tokOffset);
final int tokEnd = tokOffset + maxIdx;
LOGGER.debug("Setting stanford sentence token end: {}", tokEnd);
cm.set(TokenEndAnnotation.class, tokEnd);
cm.set(TokensAnnotation.class, clList);
tokOffset = tokEnd;
toRet.add(cm);
}
return toRet;
}
示例3: processParses
import edu.stanford.nlp.util.ArrayCoreMap; //导入依赖的package包/类
/**
* Start from parsed trees, and run the coref.
*/
public List<EntityMention> processParses(Collection<Tree> trees) {
CoreLabelTokenFactory tokenfactory = new CoreLabelTokenFactory();
List<EntityMention> entities = null;
// Create an empty Annotation
Annotation document = new Annotation("");
try {
// Setup the sentences using CoreMaps and CoreLabels.
List<CoreMap> sentences = new ArrayList<CoreMap>();
for( Tree tree : trees ) {
List<CoreLabel> sentence = new ArrayList<CoreLabel>();
CoreMap sent = new ArrayCoreMap(1);
sent.set(TokensAnnotation.class,sentence);
sentences.add(sent);
// Now add the leaves from the trees as separate tokens.
List<String> strs = TreeOperator.stringLeavesFromTree(tree);
List<String> pos = TreeOperator.posTagsFromTree(tree);
int start = 0, index = 0;
for( String str : strs ) {
CoreLabel label = tokenfactory.makeToken(str, start, start+str.length());
start += str.length() + 1;
label.set(PartOfSpeechAnnotation.class, pos.get(index++));
sentence.add(label);
}
// Now add the parse tree.
sent.set(TreeAnnotation.class, tree);
}
// Add all sentences as an annotation to the document.
document.set(CoreAnnotations.SentencesAnnotation.class, sentences);
// for( CoreMap sen : sentences ) {
// System.out.println(sen);
// }
// NOTE: You can see each annotator get created in the StanfordCoreNLP.java class.
// Look at its function getDefaultAnnotatorPool()
pipeline.annotate(document);
// System.out.println("AFTER");
// for( CoreMap sen : sentences )
// System.out.println(sen);
// This is the coreference link graph
// Each chain stores a set of mentions that link to each other,
// along with a method for getting the most representative mention
// Both sentence and token offsets start at 1!
Map<Integer, CorefChain> graph = document.get(CorefChainAnnotation.class);
// for( Integer id : graph.keySet() ) System.out.println(id + "\t" + graph.get(id));
entities = extractEntities(graph);
} catch( Exception ex ) {
System.out.println("--STANFORD COREF EXCEPTION-- Parses skipped...");
ex.printStackTrace();
}
return entities;
}