我正在尝试在UIMA管道中制作一个POSTagger(词性)。我已经下载了stanford POSTagger jar并将其附加到项目中并将模型复制到英语中但它引发了一些异常。
我的代码:
package com.gauge.ie.uimaproject;
import java.io.IOException;
import org.apache.uima.UIMAException;
import org.apache.uima.analysis_component.JCasAnnotator_ImplBase;
import org.apache.uima.analysis_engine.AnalysisEngine;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.CASException;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.fit.factory.AnalysisEngineFactory;
import org.apache.uima.fit.factory.JCasFactory;
import org.apache.uima.jcas.JCas;
import edu.stanford.nlp.tagger.maxent.MaxentTagger;
public class POSTagger extends JCasAnnotator_ImplBase
{
public static String SOFA_NAME="";
MaxentTagger tagger = new MaxentTagger("tagger/bidirectional-distsim-wsj-0-18.tagger");
@Override
public void process(JCas jcas)throws AnalysisEngineProcessException
{
try
{
String text="";
JCas newJCas=jcas.createView(SOFA_NAME);
System.out.println("getting doc text.......");
String docText = jcas.getDocumentText();
String tagged=tagger.tagString(docText);
System.out.println(tagged);
newJCas.setDocumentText(tagged);
}
catch(CASException cae)
{
System.out.println(cae);
}
}
}
异常:
Reading POS tagger model from tagger/bidirectional-distsim-wsj-0-18.tagger ... org.apache.uima.resource.ResourceInitializationException: Could not instantiate Annotator class "com.gauge.ie.uimaproject.POSTagger". Check that your annotator class is not abstract and has a zero-argument constructor. (Descriptor: <unknown>)
at org.apache.uima.analysis_engine.impl.PrimitiveAnalysisEngine_impl.initializeAnalysisComponent(PrimitiveAnalysisEngine_impl.java:250)
at org.apache.uima.analysis_engine.impl.PrimitiveAnalysisEngine_impl.initialize(PrimitiveAnalysisEngine_impl.java:170)
at org.apache.uima.impl.AnalysisEngineFactory_impl.produceResource(AnalysisEngineFactory_impl.java:94)
at org.apache.uima.impl.CompositeResourceFactory_impl.produceResource(CompositeResourceFactory_impl.java:62)
at org.apache.uima.UIMAFramework.produceResource(UIMAFramework.java:279)
at org.apache.uima.UIMAFramework.produceAnalysisEngine(UIMAFramework.java:407)
at org.apache.uima.analysis_engine.asb.impl.ASB_impl.setup(ASB_impl.java:256)
at org.apache.uima.analysis_engine.impl.AggregateAnalysisEngine_impl.initASB(AggregateAnalysisEngine_impl.java:430)
at org.apache.uima.analysis_engine.impl.AggregateAnalysisEngine_impl.initializeAggregateAnalysisEngine(AggregateAnalysisEngine_impl.java:374)
at org.apache.uima.analysis_engine.impl.AggregateAnalysisEngine_impl.initialize(AggregateAnalysisEngine_impl.java:187)
at org.apache.uima.impl.AnalysisEngineFactory_impl.produceResource(AnalysisEngineFactory_impl.java:94)
at org.apache.uima.impl.CompositeResourceFactory_impl.produceResource(CompositeResourceFactory_impl.java:62)
at org.apache.uima.UIMAFramework.produceResource(UIMAFramework.java:279)
at org.apache.uima.UIMAFramework.produceResource(UIMAFramework.java:331)
at org.apache.uima.UIMAFramework.produceAnalysisEngine(UIMAFramework.java:448)
at org.apache.uima.fit.pipeline.SimplePipeline.runPipeline(SimplePipeline.java:140)
at com.gauge.ie.uimaproject.pipeline.main(pipeline.java:27)
Caused by: edu.stanford.nlp.io.RuntimeIOException: Unrecoverable error while loading a tagger model
at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:869)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:767)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:298)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.<init>(MaxentTagger.java:263)
at com.gauge.ie.uimaproject.POSTagger.<init>(POSTagger.java:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at java.lang.Class.newInstance(Class.java:442)
at org.apache.uima.analysis_engine.impl.PrimitiveAnalysisEngine_impl.initializeAnalysisComponent(PrimitiveAnalysisEngine_impl.java:240)
... 16 more
Caused by: java.io.InvalidClassException: edu.stanford.nlp.tagger.maxent.ExtractorDistsim; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:621)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1623)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at java.util.HashMap.readObject(HashMap.java:1396)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:1017)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1896)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1993)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1918)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1801)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.readExtractors(MaxentTagger.java:595)
at edu.stanford.nlp.tagger.maxent.MaxentTagger.readModelAndInit(MaxentTagger.java:820)
... 26 more
org.apache.uima.resource.ResourceInitializationException: Could not instantiate Annotator class "com.gauge.ie.uimaproject.POSTagger". Check that your annotator class is not abstract and has a zero-argument constructor. (Descriptor: <unknown>)
答案 0 :(得分:2)
在编写自己的集成之前,我建议您查看DKPro及其与Stanford PoS标记器的集成。这可能会为您节省一些时间:
https://code.google.com/p/dkpro-core-asl/wiki/ComponentList_1_6_2#POS_Tagging
如果您真的想自己编写代码,可以查看它们的来源:
据我所知,他们使用不同的构造函数实例化标记器:
String modelFile = aUrl.toString();
MaxentTagger tagger = new MaxentTagger(modelFile,
StringUtils.argsToProperties(new String[] { "-model", modelFile }), false);
答案 1 :(得分:1)
您正在尝试加载与您正在使用的POS标记器版本不兼容的模型POS标签模型
public void insertStocuri(Integer rucsaci,Integer kituri, Integer sticle)
{
ContentValues newValues = new ContentValues();
newValues.put("Rucsaci", rucsaci);
newValues.put("Kit",kituri);
newValues.put("Sticle",sticle);
Cursor query =db.query("STOCURI", null, null, null, null, null, null);
if(query.getCount()<1)
{
//db.replace("STOCURI", null, newValues);
db.insert("STOCURI", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
else {
db.update("STOCURI",newValues, null, null);
}
}
因此,POS标记符无法反序列化模型。确保您使用的是兼容型号。