将Stanford NER集成到Android中

时间:2018-05-07 02:56:06

标签: android stanford-nlp

我正在尝试在我的Android应用中使用Stanford NER。

所有三个模型(3个类,4个类和7个类,所有最新版本)在Intellij中运行良好,但当我把它放入我的Android Studio时,发生了一些错误。

3类和4类NER模型在Android Studio中工作,但不适用于7类。但是,只有7个类包含DATE,我真的需要它。

代码和日志如下。有人可以帮我解决一下吗?非常感谢!

代码:

    public void processText(String text, String model_path, String classifier_path){
        try {

            Reader reader = new StringReader(text);

            //for pos tag
            MaxentTagger tagger = new MaxentTagger(model_path);


            //for ner
//            String serializedClassifier = "classifiers/english.muc.7class.distsim.crf.ser.gz";

            String serializedClassifier = classifier_path;
            AbstractSequenceClassifier<CoreLabel> classifier = CRFClassifier.getClassifier(serializedClassifier);

            //for lemma
            Morphology morpha = new Morphology();

            List<List<HasWord>> sentences = MaxentTagger.tokenizeText(reader);

            List<String> sentenceList = new ArrayList<String>();

            List<List<Triple<String,Integer,Integer>>> nerTags = new ArrayList<>();

            for(int i = 0; i < sentences.size(); ++i){
                List<HasWord> sentence = sentences.get(i);

                String sentenceString = SentenceUtils.listToString(sentence);
                sentenceList.add(sentenceString);
                //pos tag
                List<TaggedWord> tSentence = tagger.tagSentence(sentence);

                //ner
                List<Triple<String,Integer,Integer>> triples = classifier.classifyToCharacterOffsets(sentenceString);
                nerTags.add(triples);

            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }

日志:

1228-1228/com.nlp.runnlponandroid W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
1228-1228/com.nlp.runnlponandroid W/System.err:     at edu.stanford.nlp.util.HashIndex.size(HashIndex.java:94)
1228-1228/com.nlp.runnlponandroid W/System.err:     at edu.stanford.nlp.ie.crf.CRFClassifier.getCliqueTree(CRFClassifier.java:1480)
1228-1228/com.nlp.runnlponandroid W/System.err:     at edu.stanford.nlp.ie.crf.CRFClassifier.getSequenceModel(CRFClassifier.java:1190)
1228-1228/com.nlp.runnlponandroid W/System.err:     at edu.stanford.nlp.ie.crf.CRFClassifier.getSequenceModel(CRFClassifier.java:1186)
1228-1228/com.nlp.runnlponandroid W/System.err:     at edu.stanford.nlp.ie.crf.CRFClassifier.classifyMaxEnt(CRFClassifier.java:1218)
1228-1228/com.nlp.runnlponandroid W/System.err:     at edu.stanford.nlp.ie.crf.CRFClassifier.classify(CRFClassifier.java:1128)
1228-1228/com.nlp.runnlponandroid W/System.err:     at edu.stanford.nlp.ie.AbstractSequenceClassifier.classifyToCharacterOffsets(AbstractSequenceClassifier.java:623)
1228-1228/com.nlp.runnlponandroid W/System.err:     at com.nlp.runnlponandroid.TestNER.processText(TestNER.java:480)

TestNER.java:480实际上是:

 List<Triple<String,Integer,Integer>> triples = classifier.classifyToCharacterOffsets(sentenceString);

我尝试将corenlp.zip中的每个jar文件都包含在我的Android Studio项目中,但它甚至没有成功编译。这是错误日志:

Exception in thread "main" java.lang.IllegalStateException: Couldn't desugar invokedynamic for edu/stanford/nlp/trees/tregex/gui/TregexGUI.actionPerformed using java/lang/invoke/LambdaMetafactory.metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; (6) with arguments [(Ljava/awt/event/ActionEvent;)V, edu/stanford/nlp/trees/tregex/gui/TregexGUI.lambda$createFileChooser$592(Ljavax/swing/JFileChooser;Ljava/awt/event/ActionEvent;)V (6), (Ljava/awt/event/ActionEvent;)V]
    at com.google.devtools.build.android.desugar.LambdaDesugaring$InvokedynamicRewriter.visitInvokeDynamicInsn(LambdaDesugaring.java:467)
    at org.objectweb.asm.ClassReader.a(Unknown Source)
    at org.objectweb.asm.ClassReader.b(Unknown Source)
    at org.objectweb.asm.ClassReader.accept(Unknown Source)
    at org.objectweb.asm.ClassReader.accept(Unknown Source)
    at com.google.devtools.build.android.desugar.Desugar.desugarClassesInInput(Desugar.java:401)
    at com.google.devtools.build.android.desugar.Desugar.desugarOneInput(Desugar.java:326)
    at com.google.devtools.build.android.desugar.Desugar.desugar(Desugar.java:280)
    at com.google.devtools.build.android.desugar.Desugar.main(Desugar.java:584)
Caused by: java.lang.IllegalAccessException: no such method: edu.stanford.nlp.trees.tregex.gui.TregexGUI.lambda$createFileChooser$592(JFileChooser,ActionEvent)void/invokeStatic
    at java.lang.invoke.MemberName.makeAccessException(MemberName.java:867)
    at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:1003)
    at java.lang.invoke.MethodHandles$Lookup.resolveOrFail(MethodHandles.java:1386)
    at java.lang.invoke.MethodHandles$Lookup.findStatic(MethodHandles.java:780)
    at com.google.devtools.build.android.desugar.LambdaDesugaring$InvokedynamicRewriter.toMethodHandle(LambdaDesugaring.java:604)
    at com.google.devtools.build.android.desugar.LambdaDesugaring$InvokedynamicRewriter.toJvmMetatype(LambdaDesugaring.java:586)
    at com.google.devtools.build.android.desugar.LambdaDesugaring$InvokedynamicRewriter.visitInvokeDynamicInsn(LambdaDesugaring.java:401)
    ... 8 more
Caused by: java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    javax/swing/JFrame.getAccessibleContext()Ljavax/accessibility/AccessibleContext; @16: putfield
  Reason:
    Type 'javax/swing/JFrame$AccessibleJFrame' (current frame, stack[1]) is not assignable to 'javax/accessibility/AccessibleContext'
  Current Frame:
    bci: @16
    flags: { }
    locals: { 'javax/swing/JFrame' }
    stack: { 'javax/swing/JFrame', 'javax/swing/JFrame$AccessibleJFrame' }
  Bytecode:
    0x0000000: 2ab4 002d c700 0f2a bb00 0f59 2ab7 0160
    0x0000010: b500 2d2a b400 2db0                    
  Stackmap Table:
    same_frame(@19)

    at java.lang.invoke.MethodHandleNatives.resolve(Native Method)
    at java.lang.invoke.MemberName$Factory.resolve(MemberName.java:975)
    at java.lang.invoke.MemberName$Factory.resolveOrFail(MemberName.java:1000)
    ... 13 more


 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithDesugarForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: Error while executing java process with main class com.google.devtools.build.android.desugar.Desugar with arguments {--input C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\23.jar --output C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\desugar\debug\20.jar --input C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\15.jar --output C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\desugar\debug\15.jar --input C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\12.jar --output C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\desugar\debug\12.jar --input C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\28.jar --output C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\desugar\debug\25.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\classes\debug --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\1.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\2.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\3.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\4.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\5.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\6.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\7.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\8.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\9.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\10.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\11.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\12.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\13.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\14.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\15.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\16.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\17.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\21.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\22.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\23.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\24.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\25.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\26.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\27.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\28.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\29.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\30.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\31.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\32.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\33.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\34.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\35.jar --classpath_entry C:\Users\eve\AndroidStudioProjects\RunNLPOnAndroid\app\build\intermediates\transforms\stackFramesFixer\debug\36.jar --bootclasspath_entry C:\Users\eve\AppData\Local\Android\Sdk\platforms\android-27\android.jar --bootclasspath_entry C:\Users\eve\AppData\Local\Android\Sdk\platforms\android-27\optional\org.apache.http.legacy.jar --bootclasspath_entry C:\Program Files\Android\Android Studio\jre\jre\lib\resources.jar --bootclasspath_entry C:\Program Files\Android\Android Studio\jre\jre\lib\rt.jar --bootclasspath_entry C:\Program Files\Android\Android Studio\jre\jre\lib\jsse.jar --bootclasspath_entry C:\Program Files\Android\Android Studio\jre\jre\lib\jce.jar --bootclasspath_entry C:\Program Files\Android\Android Studio\jre\jre\lib\charsets.jar --min_sdk_version 24 --nodesugar_try_with_resources_if_needed --desugar_try_with_resources_omit_runtime_classes}

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 18s

19 actionable tasks: 4 executed, 15 up-to-date

1 个答案:

答案 0 :(得分:0)

您应该使用管道API并查看是否仍有问题。

管道API的文档位于:https://stanfordnlp.github.io/CoreNLP/api.html

具体回顾一下这个例子:

import edu.stanford.nlp.pipeline.*;
import java.util.*;

public class BasicPipelineExample {

    public static void main(String[] args) {

        // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        // read some text in the text variable
        String text = "...";

        // create an empty Annotation just with the given text
        Annotation document = new Annotation(text);

        // run all Annotators on this text
        pipeline.annotate(document);

    }

}