0
爲什麼輸入myCategorizer.categorize();必須是apache opennlp 1.8中的String [],而不是像apache OpenNLP 1.5版中的字符串? 因爲我想檢查單獨的字符串而不是數組。文檔分類程序openNLP - 分類方法
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 :(");
}
}