如何通过使用Apache OpenNLP解决IllegalStateException和ParserConfigurationException以在android studio中查找位置

时间:2018-11-12 06:20:53

标签: android opennlp bin

 package com.example.dell.apacheopennlp;
    import android.os.StrictMode;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;
    import java.io.IOException;
    import java.io.InputStream;
    import opennlp.tools.namefind.NameFinderME;
    import opennlp.tools.namefind.TokenNameFinderModel;
    import opennlp.tools.tokenize.TokenizerME;
    import opennlp.tools.tokenize.TokenizerModel;
    import opennlp.tools.util.Span;
    public class apacheOpenNLP extends AppCompatActivity {
         TokenNameFinderModel locationModel = null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate (savedInstanceState);
            setContentView (R.layout.activity_apache_open_nlp);
            final TextView txt = (TextView) findViewById (R.id.txtView);
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
             StrictMode.setThreadPolicy(policy);

            InputStream inputStream = null;
            TokenizerModel tokenModel = null;
            try {
                inputStream = getAssets ( ).open ("en-token.bin");
                tokenModel = new TokenizerModel (inputStream);
                inputStream.close ( );
            } catch (IOException e) {
                e.printStackTrace ( );
                txt.setText (e.toString ( ) + " inside catch of token");
            }

            if (tokenModel != null) {
                TokenizerME tokenizer = new TokenizerME (tokenModel);
                String paragraph = "Tutorialspoint is located in Hyderabad";
                String tokens[] = tokenizer.tokenize (paragraph);
                InputStream locationInputStream = null;

                try {
                    locationInputStream = getAssets ( ).open ("en-ner-location.bin");
                    locationModel = new TokenNameFinderModel (locationInputStream);

                } catch (IOException e) {
                    e.printStackTrace ( );
                    txt.setText (e.toString ( ) + " inside catch of location");
                }
                Thread thread = new Thread(new Runnable() {

                    @Override
                    public void run() {
                        try  {
                            NameFinderME nameFinder = null;
                            if (locationModel != null) {
                                txt.setText ("Location model is not empty");
                                nameFinder = new NameFinderME (locationModel);

                                //after this line..its goes in the catch and the app crashes 
                                Span nameSpans[] = nameFinder.find (tokens);
                                String result = null;
                                for (Span s : nameSpans)
                                   result += s.toString ( );
                                txt.setText (result);
                                // txt.setText ("Location model is not empty");*/
                            }
                            else{
                                txt.setText ("Location model is empty");
                            }
                    } catch (Exception e) {
                        txt.setText(e.toString ()+"++++++");
                        e.printStackTrace();
                    }
                }
            });

            thread.start();

        }

        }

    }

我正在使用apache openNLP在句子中查找位置,当它在nameFinder = new NameFinderME(locationModel)处传递时出现错误。 我在使用NameFinderME时尝试了其他示例,但它也无法正常工作。有人可以找出问题吗?

W/System.err: java.lang.IllegalStateException: javax.xml.parsers.ParserConfigurationException: http://javax.xml.XMLConstants/feature/secure-processing
                  at opennlp.tools.util.XmlUtil.createDocumentBuilder(XmlUtil.java:42)
                  at opennlp.tools.util.featuregen.GeneratorFactory.createDOM(GeneratorFactory.java:557)
                  at opennlp.tools.util.featuregen.GeneratorFactory.create(GeneratorFactory.java:590)
                  at opennlp.tools.namefind.TokenNameFinderFactory.createFeatureGenerators(TokenNameFinderFactory.java:189)
                  at opennlp.tools.namefind.TokenNameFinderFactory.createContextGenerator(TokenNameFinderFactory.java:150)
                  at opennlp.tools.namefind.NameFinderME.<init>(NameFinderME.java:83)
                  at com.example.dell.apacheopennlp.apacheOpenNLP$1.run(apacheOpenNLP.java:60)
                  at java.lang.Thread.run(Thread.java:764)

W/System.err: Caused by: javax.xml.parsers.ParserConfigurationException: http://javax.xml.XMLConstants/feature/secure-processing
                  at org.apache.harmony.xml.parsers.DocumentBuilderFactoryImpl.setFeature(DocumentBuilderFactoryImpl.java:101)

2 个答案:

答案 0 :(得分:0)

如果您使用的是Apache OpenNLP的最新版本,则可以直接将getResource返回的URL传递给TokenizerModel构造函数。

TokenizerModel tokenModel = 
    new TokenizerModel(apacheOpenNLP.class.getResource("en-token.bin"));  

您不应在每次调用令牌生成器时加载模型,而应将模型重用于多次调用。

答案 1 :(得分:0)

您能否尝试将nameFinder更改为声明为 TokenNameFinder 的接口类型?

只需尝试更换

NameFinderME nameFinder = null

TokenNameFinder nameFinder = null

所有其他都应该相同。