为什么在apache opennlp 1.8而不是String中对String []进行分类?

时间:2017-10-05 09:13:56

标签: java document opennlp

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

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

 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 :( ");
    }
}

1 个答案:

答案 0 :(得分:1)

回到OpenNLP 1.5的时代,DocumentCatagorizer所做的第一件事就是将你的字符串标记为单词。首先,这似乎很容易,但是,您可能更喜欢使用最大熵标记器而不是默认的WhitespaceTokenizer。令牌化程序可能对分类产生很大影响。更改API以允许用户选择他/她选择的标记生成器可以缓解问题。只需添加

DISPLAY_WIDTH * DISPLAY_HEIGHT

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