本文整理汇总了Java中opennlp.tools.postag.POSModel类的典型用法代码示例。如果您正苦于以下问题:Java POSModel类的具体用法?Java POSModel怎么用?Java POSModel使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
POSModel类属于opennlp.tools.postag包,在下文中一共展示了POSModel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import opennlp.tools.postag.POSModel; //导入依赖的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());
}
}
示例2: testLemmatizing
import opennlp.tools.postag.POSModel; //导入依赖的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));
}
}
示例3: testPOSTaggerWithCustomFactory
import opennlp.tools.postag.POSModel; //导入依赖的package包/类
@Test
public void testPOSTaggerWithCustomFactory() throws Exception {
Path dictionary = POSDictionayBuilderTest.createMorfologikDictionary();
POSTaggerFactory inFactory = new MorfologikPOSTaggerFactory();
TagDictionary inDict = inFactory.createTagDictionary(dictionary.toFile());
inFactory.setTagDictionary(inDict);
POSModel posModel = trainPOSModel(ModelType.MAXENT, inFactory);
POSTaggerFactory factory = posModel.getFactory();
assertTrue(factory.getTagDictionary() instanceof MorfologikTagDictionary);
factory = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
posModel.serialize(out);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
POSModel fromSerialized = new POSModel(in);
factory = fromSerialized.getFactory();
assertTrue(factory.getTagDictionary() instanceof MorfologikTagDictionary);
assertEquals(2, factory.getTagDictionary().getTags("casa").length);
}
示例4: doInitialize
import opennlp.tools.postag.POSModel; //导入依赖的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);
}
}
示例5: scoreStructure
import opennlp.tools.postag.POSModel; //导入依赖的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;
}
示例6: startStage
import opennlp.tools.postag.POSModel; //导入依赖的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();
}
}
示例7: KeyPhraseChunkExtractor
import opennlp.tools.postag.POSModel; //导入依赖的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);
}
示例8: train
import opennlp.tools.postag.POSModel; //导入依赖的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;
}
示例9: POSExample
import opennlp.tools.postag.POSModel; //导入依赖的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
示例10: readPOSModel
import opennlp.tools.postag.POSModel; //导入依赖的package包/类
private POSModel readPOSModel(Language language) {
logger.debug(marker, "Reading POS model for {}", language);
File modelFile = new File(modelsDir, String.format("%s-pos-maxent.bin", language.name().toLowerCase()));
try (InputStream in = new FileInputStream(modelFile)) {
return new POSModel(in);
} catch (IOException e) {
throw new StarGraphException("Can't read '" + modelFile + "'", e);
}
}
示例11: init
import opennlp.tools.postag.POSModel; //导入依赖的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);
}
}
}
示例12: main
import opennlp.tools.postag.POSModel; //导入依赖的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"/>
}
示例13: main
import opennlp.tools.postag.POSModel; //导入依赖的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));
}
}
}
示例14: getFocusNoun
import opennlp.tools.postag.POSModel; //导入依赖的package包/类
public String[] getFocusNoun(String question) throws IOException
{
String wordnetDir = System.getProperty("wordnet.dir");
wordnetDir="WordNet-3.0/dict/";
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);
String[] focus=new String[2];
int p=0;
for(int i=0;i<context.length;i++)
{
if(context[i].startsWith("hw=") || context[i].startsWith("mw="))
{
//System.out.println(context[i].substring(3));
focus[p++]=context[i].substring(3);
}
}
return focus;
}
示例15: initPosTagger
import opennlp.tools.postag.POSModel; //导入依赖的package包/类
private void initPosTagger() {
try (InputStream modelIn = NlpVerticle.class.getResourceAsStream("/nlp/en-pos-maxent.bin")) {
POSModel model = new POSModel(modelIn);
posTagger = new POSTaggerME(model);
} catch (IOException e) {
throw new RuntimeException(e);
}
}