0
我想初始化一次stanfordNLP pipelince,並多次使用它,而不需要再次初始化,以提高執行時間。如何初始化stanfordNLP管道一次並多次使用而不需要再次初始化?
可能嗎?
我有代碼:
public static boolean isHeaderMatched(String string) {
// creates a StanfordCoreNLP object.
Properties props = new Properties();
props.put("annotators", "tokenize, ssplit, pos, lemma, ner");
RedwoodConfiguration.current().clear().apply();
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
Env env = TokenSequencePattern.getNewEnv();
env.setDefaultStringMatchFlags(NodePattern.CASE_INSENSITIVE);
env.setDefaultStringPatternFlags(Pattern.CASE_INSENSITIVE);
Annotation document = new Annotation(string);
// use the pipeline to annotate the document we created
pipeline.annotate(document);
List<CoreMap> sentences = document.get(SentencesAnnotation.class);
CoreMapExpressionExtractor extractor = CoreMapExpressionExtractor.createExtractorFromFiles(env, "./app/utils/Summarizer/mapping/career_objective.rule", "./app/utils/Summarizer/mapping/personal_info.rule", "./app/utils/Summarizer/mapping/education.rule", "./app/utils/Summarizer/mapping/work_experience.rule", "./app/utils/Summarizer/mapping/certification.rule", "./app/utils/Summarizer/mapping/publication.rule", "./app/utils/Summarizer/mapping/award_achievement.rule", "./app/utils/Summarizer/mapping/hobbies_interest.rule", "./app/utils/Summarizer/mapping/lang_known.rule", "./app/utils/Summarizer/mapping/project_details.rule", "./app/utils/Summarizer/mapping/skill-set.rule", "./app/utils/Summarizer/mapping/misc_header.rule");
boolean flag = false;
for (CoreMap sentence : sentences) {
List<MatchedExpression> matched = extractor.extractExpressions(sentence);
//System.out.println("Probable Header is : " + matched);
Set<String> uniqueMatchedKeyWordSet = DocumentParserUtil.removeDuplicate(matched);
System.out.println("Matched: " + uniqueMatchedKeyWordSet + " and Size of MatchedSet: " + uniqueMatchedKeyWordSet.size());
//checked if the more than half the no. of word in header(string) is matched
if ((matched.size() >= uniqueMatchedKeyWordSet.size()) && !matched.isEmpty() && matched.size() >= Math.floorDiv(string.split("\\s").length, 2)) {
//System.out.println("This is sure a header!");
flag = true;
} else {
flag = false;
}
/*for(MatchedExpression phrase: matched){
System.out.println("matched header type: " + phrase.getValue().get());
}*/
}
return flag;
}
我要執行這部分代碼僅在上述方法中加載模型的第一呼叫被執行。
// creates a StanfordCoreNLP object.
Properties props = new Properties();
props.put("annotators", "tokenize, ssplit, pos, lemma, ner");
RedwoodConfiguration.current().clear().apply();
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
Env env = TokenSequencePattern.getNewEnv();
env.setDefaultStringMatchFlags(NodePattern.CASE_INSENSITIVE);
env.setDefaultStringPatternFlags(Pattern.CASE_INSENSITIVE);
在此先感謝。
您可以將'StanfordCoreNLP'變量從函數的作用域移動到類的作用域,並將要執行的代碼放入'static {}'塊中。 –
如果我將最後提到的代碼塊放在'static {}'塊中,那麼在靜態方法中沒有檢測到兩個變量'pipeline'和'env'。 –