繁体   English   中英

使用Apache OpenNLP在句子中查找位置时如何解决问题

[英]How can I resolve the issue when finding location in a sentence using Apache OpenNLP

package com.example.dell.apacheopennlp;

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 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_apache_open_nlp);
        final TextView txt = (TextView) findViewById (R.id.txtView);

        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;
            TokenNameFinderModel locationModel = 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");
            }

            if (locationModel != null) {
                NameFinderME nameFinder = new NameFinderME (locationModel);
                Span nameSpans[] = nameFinder.find (tokens);

                String result = null;
                for (Span s : nameSpans)
                    //result=  s.toString()+"  "+tokens[s.getStart()];
                    result += s.toString ( );
                txt.setText (result);
            }
            else{
               // txt.setText ("Location model is empty");
            }
        }
    }
}

我正在使用Apache OpenNLP提取句子中的位置。 我调试了代码,发现在NameFinderME nameFinder = new NameFinderME(locationModel)之后; android应用程序崩溃,显示一条消息“该应用程序已停止工作”如何解决此问题?

您可以尝试将nameFinder更改为声明为TokenNameFinder接口类型吗?

只是尝试更换

NameFinderME nameFinder = null

TokenNameFinder nameFinder = null

休息都应该一样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM