本文整理匯總了Java中opennlp.tools.postag.POSTaggerME類的典型用法代碼示例。如果您正苦於以下問題:Java POSTaggerME類的具體用法?Java POSTaggerME怎麽用?Java POSTaggerME使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
POSTaggerME類屬於opennlp.tools.postag包,在下文中一共展示了POSTaggerME類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: doRun
import opennlp.tools.postag.POSTaggerME; //導入依賴的package包/類
@Override
public List<Word> doRun(Language language, String sentence) {
Tokenizer tokenizer = new TokenizerME(getTokenizerModel(language));
POSTaggerME tagger = new POSTaggerME(getPOSModel(language));
String[] tokens = tokenizer.tokenize(sentence);
String[] tags = tagger.tag(tokens);
PartOfSpeechSet posSet = PartOfSpeechSet.getPOSSet(language);
List<Word> words = new ArrayList<>();
for (int i = 0; i < tokens.length; i++) {
words.add(new Word(posSet.valueOf(tags[i]), tokens[i]));
}
return words;
}
示例2: main
import opennlp.tools.postag.POSTaggerME; //導入依賴的package包/類
public static void main(String[] args) throws IOException {
if (args.length == 0) {
System.err.println("Usage: AnswerTypeEventStream eventfile");
System.exit(1);
}
int ai = 0;
String eventFile = args[ai++];
String modelsDirProp = System.getProperty("models.dir", "book/src/main" + File.separator + "opennlp-models" +
File.separator + "english");
File modelsDir = new File(modelsDirProp);
File wordnetDir = new File(System.getProperty("wordnet.dir", "book/src/main" + File.separator + "WordNet-3.0" + File.separator + "dict"));
InputStream chunkerStream = new FileInputStream(
new File(modelsDir, "en-chunker.bin"));
ChunkerModel chunkerModel = new ChunkerModel(chunkerStream);
ChunkerME chunker = new ChunkerME(chunkerModel);
InputStream posStream = new FileInputStream(
new File(modelsDir, "en-pos-maxent.bin"));
POSModel posModel = new POSModel(posStream);
POSTaggerME tagger = new POSTaggerME(posModel);
Parser parser = new ChunkParser(chunker, tagger);
AnswerTypeContextGenerator actg = new AnswerTypeContextGenerator(wordnetDir);
EventStream es = new AnswerTypeEventStream(eventFile, actg, parser);
while (es.hasNext()) {
System.out.println(es.next().toString());
}
}
示例3: OpenNLP
import opennlp.tools.postag.POSTaggerME; //導入依賴的package包/類
/**
* Private constructor to allow only one {@link OpenNLP} for each Thread
*
* @throws IllegalStateException if an error occurred from {@link LoaderNLP} or {@link PropertiesManager}
*/
private OpenNLP() {
try {
detector = new SentenceDetectorME(LoaderNLP.getSentenceModel());
tokenizer = new TokenizerME(LoaderNLP.getTokenizerModel());
tagger = new POSTaggerME(LoaderNLP.getPosModel());
nameFinderOrg = new NameFinderME(LoaderNLP.getTokenNameFinderModelOrg());
nameFinderLoc = new NameFinderME(LoaderNLP.getTokenNameFinderModelLoc());
nameFinderPers = new NameFinderME(LoaderNLP.getTokenNameFinderModelPers());
InputStream inputStream = new FileInputStream(PROPERTIES_MANAGER.getProperty("nlp.dictionaries.path"));
lemmatizer = new SimpleLemmatizer(inputStream);
inputStream.close();
} catch (IllegalArgumentException | IOException e) {
LOGGER.error(e.getMessage());
throw new IllegalStateException(e);
}
}
示例4: testLemmatizing
import opennlp.tools.postag.POSTaggerME; //導入依賴的package包/類
@Test
public void testLemmatizing() throws Exception {
try (InputStream posInputStream = this.getClass().getResourceAsStream("/nlp/en-pos-maxent.bin");
InputStream lemmaInputStream = getClass().getResourceAsStream("/nlp/en-lemmatizer.dict")) {
POSModel model = new POSModel(posInputStream);
POSTaggerME tagger = new POSTaggerME(model);
String sent[] = new String[]{"Fine", "Most", "large", "cities", "in", "the", "US", "had",
"morning", "and", "afternoon", "newspapers", "."};
String tags[] = tagger.tag(sent);
DictionaryLemmatizer dictionaryLemmatizer = new DictionaryLemmatizer(lemmaInputStream);
String[] lemmatize = dictionaryLemmatizer.lemmatize(sent, tags);
logger.info("lemmas: {}", Arrays.asList(lemmatize));
}
}
示例5: doInitialize
import opennlp.tools.postag.POSTaggerME; //導入依賴的package包/類
@Override
public void doInitialize(UimaContext aContext) throws ResourceInitializationException {
try {
tokensModel.loadModel(TokenizerModel.class, getClass().getResourceAsStream("en_token.bin"));
sentencesModel.loadModel(SentenceModel.class, getClass().getResourceAsStream("en_sent.bin"));
posModel.loadModel(POSModel.class, getClass().getResourceAsStream("en_pos_maxent.bin"));
chunkModel.loadModel(ChunkerModel.class, getClass().getResourceAsStream("en_chunker.bin"));
} catch (BaleenException be) {
getMonitor().error("Unable to load OpenNLP Language Models", be);
throw new ResourceInitializationException(be);
}
try {
sentenceDetector = new SentenceDetectorME((SentenceModel) sentencesModel.getModel());
wordTokenizer = new TokenizerME((TokenizerModel) tokensModel.getModel());
posTagger = new POSTaggerME((POSModel) posModel.getModel());
phraseChunker = new ChunkerME((ChunkerModel) chunkModel.getModel());
} catch (Exception e) {
getMonitor().error("Unable to create OpenNLP taggers", e);
throw new ResourceInitializationException(e);
}
}
示例6: scoreStructure
import opennlp.tools.postag.POSTaggerME; //導入依賴的package包/類
public double scoreStructure(String ca, String q, String passage, boolean verbose) throws InvalidFormatException, IOException{
POSTaggerME parserModel = new POSTaggerME(new POSModel(new FileInputStream(new File("en-pos-model.bin"))));
Tokenizer tokenizer = new TokenizerME(new TokenizerModel(new FileInputStream(new File("en-token.bin"))));
Parser parser = ParserFactory.create(new ParserModel(new FileInputStream(new File("en-parser.bin"))));
double score = 0;
Parse[] questionParse = ParserTool.parseLine(q, parser, 1);
Parse[] passageParse = ParserTool.parseLine(q, parser, 1);
if (passage.contains(ca)) {
for (int i =0; i < questionParse.length; i++) {
score += matchChildren(questionParse[i],passageParse[i]);
}
}
return score;
}
示例7: startStage
import opennlp.tools.postag.POSTaggerME; //導入依賴的package包/類
@Override
public void startStage(StageConfiguration config) {
// parse the config to map the params properly
textField = config.getProperty("textField", textField);
peopleField = config.getProperty("peopleField", peopleField);
posTextField = config.getProperty("posTextField", posTextField);
try {
// Sentence finder
SentenceModel sentModel = new SentenceModel(new FileInputStream(sentenceModelFile));
sentenceDetector = new SentenceDetectorME(sentModel);
// tokenizer
TokenizerModel tokenModel = new TokenizerModel(new FileInputStream(tokenModelFile));
tokenizer = new TokenizerME(tokenModel);
// person name finder
TokenNameFinderModel nameModel = new TokenNameFinderModel(new FileInputStream(personModelFile));
nameFinder = new NameFinderME(nameModel);
// load the part of speech tagger.
posTagger = new POSTaggerME(new POSModel(new FileInputStream(posModelFile)));
} catch (IOException e) {
log.info("Error loading up OpenNLP Models. {}", e.getLocalizedMessage());
e.printStackTrace();
}
}
示例8: KeyPhraseChunkExtractor
import opennlp.tools.postag.POSTaggerME; //導入依賴的package包/類
public KeyPhraseChunkExtractor() throws Exception, IOException {
InputStream modelIn = getClass().getResourceAsStream(
"/nlptools/data/en-pos-maxent.bin");
posModel = new POSModel(modelIn);
tagger = new POSTaggerME(posModel);
modelIn = getClass().getResourceAsStream(
"/nlptools/data/en-chunker.bin");
chunkModel = new ChunkerModel(modelIn);
chunker = new ChunkerME(chunkModel);
modelIn = getClass().getResourceAsStream("/nlptools/data/en-token.bin");
nlTokenizerModel = new TokenizerModel(modelIn);
nlTokenizer = new TokenizerME(nlTokenizerModel);
}
示例9: initialize
import opennlp.tools.postag.POSTaggerME; //導入依賴的package包/類
public static void initialize() throws IOException {
/* normal model */
/*
model = new POSModelLoader().load(new File(RESOURCES + "pt.postagger.model"));
tModel = new TokenizerModel(new FileInputStream(RESOURCES + "pt.tokenizer.model"));
sModel = new SentenceModel(new FileInputStream(RESOURCES + "pt.sentdetect.model"));
*/
/* with VPP tag */
model = new POSModelLoader().load(new File(RESOURCES + "pt.postaggerVerbPP.model"));
tModel = new TokenizerModel(new FileInputStream(RESOURCES + "pt.tokenizerVerbPP.model"));
sModel = new SentenceModel(new FileInputStream(RESOURCES + "pt.sentDetectVerbPP.model"));
tagger = new POSTaggerME(model);
token = new TokenizerME(tModel);
sent = new SentenceDetectorME(sModel);
}
示例10: train
import opennlp.tools.postag.POSTaggerME; //導入依賴的package包/類
public final POSModel train(final TrainingParameters params) {
// features
if (getPosTaggerFactory() == null) {
throw new IllegalStateException(
"Classes derived from AbstractTrainer must "
+ " create a POSTaggerFactory features!");
}
// training model
POSModel trainedModel = null;
POSEvaluator posEvaluator = null;
try {
trainedModel = POSTaggerME.train(this.lang, this.trainSamples, params,
getPosTaggerFactory());
final POSTaggerME posTagger = new POSTaggerME(trainedModel);
posEvaluator = new POSEvaluator(posTagger);
posEvaluator.evaluate(this.testSamples);
} catch (final IOException e) {
System.err.println("IO error while loading training and test sets!");
e.printStackTrace();
System.exit(1);
}
System.out.println("Final result: " + posEvaluator.getWordAccuracy());
return trainedModel;
}
示例11: createAutomaticDictionary
import opennlp.tools.postag.POSTaggerME; //導入依賴的package包/類
/**
* Automatically create a tag dictionary from training data.
*
* @param aDictSamples
* the dictSamples created from training data
* @param aDictCutOff
* the cutoff to create the dictionary
*/
protected final void createAutomaticDictionary(
final ObjectStream<POSSample> aDictSamples, final int aDictCutOff) {
if (aDictCutOff != Flags.DEFAULT_DICT_CUTOFF) {
try {
TagDictionary dict = getPosTaggerFactory().getTagDictionary();
if (dict == null) {
dict = getPosTaggerFactory().createEmptyTagDictionary();
getPosTaggerFactory().setTagDictionary(dict);
}
if (dict instanceof MutableTagDictionary) {
POSTaggerME.populatePOSDictionary(aDictSamples,
(MutableTagDictionary) dict, aDictCutOff);
} else {
throw new IllegalArgumentException("Can't extend a POSDictionary"
+ " that does not implement MutableTagDictionary.");
}
this.dictSamples.reset();
} catch (final IOException e) {
throw new TerminateToolException(-1,
"IO error while creating/extending POS Dictionary: "
+ e.getMessage(), e);
}
}
}
示例12: POSExample
import opennlp.tools.postag.POSTaggerME; //導入依賴的package包/類
public void POSExample() {
try (InputStream input = new FileInputStream(
new File("en-pos-maxent.bin"));) {
// To lower case example
String lowerCaseVersion = sentence.toLowerCase();
out.println(lowerCaseVersion);
// Pull out tokens
List<String> list = new ArrayList<>();
Scanner scanner = new Scanner(sentence);
while (scanner.hasNext()) {
list.add(scanner.next());
}
// Convert list to an array
String[] words = new String[1];
words = list.toArray(words);
// Build model
POSModel posModel = new POSModel(input);
POSTaggerME posTagger = new POSTaggerME(posModel);
// Tag words
String[] posTags = posTagger.tag(words);
for (int i = 0; i < posTags.length; i++) {
out.println(words[i] + " - " + posTags[i]);
}
// Find top sequences
Sequence sequences[] = posTagger.topKSequences(words);
for (Sequence sequence : sequences) {
out.println(sequence);
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
開發者ID:PacktPublishing,項目名稱:Machine-Learning-End-to-Endguide-for-Java-developers,代碼行數:38,代碼來源:NLPExamples.java
示例13: init
import opennlp.tools.postag.POSTaggerME; //導入依賴的package包/類
@SuppressWarnings("rawtypes")
public void init(NamedList initArgs) {
SolrParams params = SolrParams.toSolrParams(initArgs);
String modelDirectory = params.get("modelDirectory",
System.getProperty("model.dir"));//<co id="qqpp.model"/>
String wordnetDirectory = params.get("wordnetDirectory",
System.getProperty("wordnet.dir"));//<co id="qqpp.wordnet"/>
if (modelDirectory != null) {
File modelsDir = new File(modelDirectory);
try {
InputStream chunkerStream = new FileInputStream(
new File(modelsDir,"en-chunker.bin"));
ChunkerModel chunkerModel = new ChunkerModel(chunkerStream);
chunker = new ChunkerME(chunkerModel); //<co id="qqpp.chunker"/>
InputStream posStream = new FileInputStream(
new File(modelsDir,"en-pos-maxent.bin"));
POSModel posModel = new POSModel(posStream);
tagger = new POSTaggerME(posModel); //<co id="qqpp.tagger"/>
// model = new DoccatModel(new FileInputStream( //<co id="qqpp.theModel"/>
// new File(modelDirectory,"en-answer.bin"))).getMaxentModel();
model = new SuffixSensitiveGISModelReader(new File(modelDirectory+"/qa/ans.bin")).getModel();
//GISModel m = new SuffixSensitiveGISModelReader(new File(modelFileName)).getModel();
probs = new double[model.getNumOutcomes()];
atcg = new AnswerTypeContextGenerator(
new File(wordnetDirectory, "dict"));//<co id="qqpp.context"/>
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
示例14: main
import opennlp.tools.postag.POSTaggerME; //導入依賴的package包/類
/**
* Train the answer model
* <p>
* Hint:
* <pre>
* mvn exec:java -Dexec.mainClass=com.tamingtext.qa.AnswerTypeClassifier \
* -Dexec.args="dist/data/questions-train.txt en-answer.bin" \
* -Dmodel.dir=../../opennlp-models \
* -Dwordnet.dir=../../Wordnet-3.0/dict
* </pre>
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
if (args.length < 2) {
System.err.println("Usage: AnswerTypeClassifier <trainFile> <modelFile>");
System.exit(1);
}
String trainFile = args[0];
File outFile = new File(args[1]);
String modelsDirProp = System.getProperty("model.dir");
File modelsDir = new File(modelsDirProp);
String wordnetDir = System.getProperty("wordnet.dir");
InputStream chunkerStream = new FileInputStream(
new File(modelsDir, "en-chunker.bin"));
ChunkerModel chunkerModel = new ChunkerModel(chunkerStream);
ChunkerME chunker = new ChunkerME(chunkerModel);
InputStream posStream = new FileInputStream(
new File(modelsDir, "en-pos-maxent.bin"));
POSModel posModel = new POSModel(posStream);
POSTaggerME tagger = new POSTaggerME(posModel);
Parser parser = new ChunkParser(chunker, tagger);
AnswerTypeContextGenerator actg = new AnswerTypeContextGenerator(new File(wordnetDir));
//<start id="atc.train"/>
AnswerTypeEventStream es = new AnswerTypeEventStream(trainFile,
actg, parser);
GISModel model = GIS.trainModel(100, new TwoPassDataIndexer(es, 3));//<co id="atc.train.do"/>
GISModelWriter writer = new SuffixSensitiveGISModelWriter(model, outFile);
writer.persist();
//new DoccatModel("en", model).serialize(new FileOutputStream(outFile));
/*
<calloutlist>
<callout arearefs="atc.train.do"><para>Using the event stream, which feeds us training examples, do the actual training using OpenNLP's Maxent classifier.</para></callout>
</calloutlist>
*/
//<end id="atc.train"/>
}
示例15: main
import opennlp.tools.postag.POSTaggerME; //導入依賴的package包/類
public static void main(String args[]) throws IOException
{
String wordnetDir = System.getProperty("wordnet.dir");
//wordnetDir="WordNet-3.0/dict/";
String question="Who is Abraham Lincoln?";
AnswerTypeContextGenerator atcg=new AnswerTypeContextGenerator(new File(wordnetDir));
String q=null;
String modelsDirProp = System.getProperty("model.dir");
// modelsDirProp="opennlp-models/";
File modelsDir = new File(modelsDirProp);
InputStream chunkerStream = new FileInputStream(
new File(modelsDir,"en-chunker.bin"));
ChunkerModel chunkerModel = new ChunkerModel(chunkerStream);
ChunkerME chunker = new ChunkerME(chunkerModel);
InputStream posStream = new FileInputStream(
new File(modelsDir,"en-pos-maxent.bin"));
POSModel posModel = new POSModel(posStream);
POSTaggerME tagger = new POSTaggerME(posModel);
Parser parser = new ChunkParser(chunker, tagger);
Parse query = ParserTool.parseLine(question,parser,1)[0];
String[] context=atcg.getContext(query);
for(int i=0;i<context.length;i++)
{
if(context[i].startsWith("hw=") || context[i].startsWith("mw="))
{
System.out.println(context[i].substring(3));
}
}
}