java - 为什么在 apache opennlp 1.8 中分类是 String[ ] 而不是 String?

标签 java document opennlp

为什么在 apache opennlp 1.8 中 myCategorizer.categorize(); 的输入必须是 String[] 而不是 String就像 apache OpenNLP 1.5 版本一样?

因为我想检查单独的字符串而不是数组?

 public void trainModel() 
    {
        InputStream dataIn = null;
        try 
        {;
            dataIn = new FileInputStream("D:/training.txt");
            ObjectStream lineStream = new PlainTextByLineStream(dataIn, "UTF-8");
            ObjectStream sampleStream = new DocumentSampleStream(lineStream);
            // Specifies the minimum number of times a feature must be seen
            int cutoff = 2;
            int trainingIterations = 30;
            model = DocumentCategorizerME.train("NL", sampleStream, cutoff,trainingIterations);


        } 

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

        finally 
        {
            if (dataIn != null) 
            {
                try 
                {
                    dataIn.close();
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
            }
        }
    }


public void classifyNewTweet(String tweet) 
{
    DocumentCategorizerME myCategorizer = new DocumentCategorizerME(model);
    double[] outcomes = myCategorizer.categorize(tweet);
    String category = myCategorizer.getBestCategory(outcomes);

    if (category.equalsIgnoreCase("1")) 
    {
        System.out.println("The tweet is positive :) ");
    } 
    else 
    {
        System.out.println("The tweet is negative :( ");
    }
}

最佳答案

早在 OpenNLP 1.5 时代,DocumentCatagorizer 做的第一件事就是将字符串标记为单词。乍一看,这可能看起来很简单,但是,您可能更喜欢使用最大熵标记生成器而不是默认的 WhitespaceTokenizer。分词器对分类有很大的影响。更改 API 以允许用户选择他/她选择的标记器可以缓解这个问题。只需添加

Tokenizer tokenizer = WhitespaceTokenizer.INSTANCE;
...
String[] tokens = tokenizer.tokenize(tweet);
double[] outcomes = myCategorizer.categorize(tweet);
...

这应该可以解决您的问题。您还可以使用统计分词器(请参阅 TokenizerME)或 SimpleTokenizer。

关于java - 为什么在 apache opennlp 1.8 中分类是 String[ ] 而不是 String?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46581873/

相关文章:

java - 在 JTextArea 中的特定位置插入字符串

css - 需要更改 Sharepoint 文档库的布局

OpenNLP 和同义词数据库

java - 是否可以使用 ProcessBuilder 运行外部 .class 文件?

java - Java 应用程序的调试/标准构建

document - Microsoft Document Explorer 2008有什么用途?

nlp - 提取子句形成句子

java - 测试 OpenNLP 分类器模型

java - 打开附件面板的操作

Netbeans 上的 JavaFX