[プロジェクト スタック : Java、Opennlp、Elasticsearch (データストア)、twitter からデータを読み取る twitter4j]
maxent classifier を使用してツイートを分類するつもりです。最初のステップはモデルのトレーニングであることを理解しています。ドキュメントから、モデルをトレーニングするための GISTrainer ベースの train メソッドがあることがわかりました。opennlp の maxent 分類子を使用してモデルをトレーニングし、結果を予測する単純なコードをまとめることができました。
モデルをトレーニングするために、2 つのファイル postive.txt と negative.txt を使用しました。
positive.txt の内容
positive This is good
positive This is the best
positive This is fantastic
positive This is super
positive This is fine
positive This is nice
ネガティブ.txtの内容
negative This is bad
negative This is ugly
negative This is the worst
negative This is worse
negative This sucks
そして、以下の Java メソッドが結果を生成します。
@Override
public void trainDataset(String source, String destination) throws Exception {
File[] inputFiles = FileUtil.buildFileList(new File(source)); // trains both positive and negative.txt
File modelFile = new File(destination);
Tokenizer tokenizer = SimpleTokenizer.INSTANCE;
CategoryDataStream ds = new CategoryDataStream(inputFiles, tokenizer);
int cutoff = 5;
int iterations = 100;
BagOfWordsFeatureGenerator bowfg = new BagOfWordsFeatureGenerator();
DoccatModel model = DocumentCategorizerME.train("en", ds, cutoff,iterations, bowfg);
model.serialize(new FileOutputStream(modelFile));
}
@Override
public void predict(String text, String modelFile) {
InputStream modelStream = null;
try{
Tokenizer tokenizer = SimpleTokenizer.INSTANCE;
String[] tokens = tokenizer.tokenize(text);
modelStream = new FileInputStream(modelFile);
DoccatModel model = new DoccatModel(modelStream);
BagOfWordsFeatureGenerator bowfg = new BagOfWordsFeatureGenerator();
DocumentCategorizer categorizer = new DocumentCategorizerME(model, bowfg);
double[] probs = categorizer.categorize(tokens);
if(null!=probs && probs.length>0){
for(int i=0;i<probs.length;i++){
System.out.println("double[] probs index " + i + " value " + probs[i]);
}
}
String label = categorizer.getBestCategory(probs);
System.out.println("label " + label);
int bestIndex = categorizer.getIndex(label);
System.out.println("bestIndex " + bestIndex);
double score = probs[bestIndex];
System.out.println("score " + score);
}
catch(Exception e){
e.printStackTrace();
}
finally{
if(null!=modelStream){
try {
modelStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
try {
String outputModelPath = "/home/**/sd-sentiment-analysis/models/trainPostive";
String source = "/home/**/sd-sentiment-analysis/sd-core/src/main/resources/datasets/";
MaximunEntropyClassifier me = new MaximunEntropyClassifier();
me.trainDataset(source, outputModelPath);
me.predict("This is bad", outputModelPath);
} catch (Exception e) {
e.printStackTrace();
}
}
次の質問があります。
1) モデルを反復的にトレーニングするにはどうすればよいですか? また、モデルに新しい文/単語を追加するにはどうすればよいですか? データファイルに特定のフォーマットはありますか? ファイルには、タブで区切られた 2 つ以上の単語が必要であることがわかりました。私の理解は有効ですか?2) モデルのトレーニングに使用できる、公開されているデータ セットはありますか? 映画のレビューの情報源をいくつか見つけました。私が取り組んでいるプロジェクトには、映画のレビューだけでなく、製品のレビュー、ブランドの感情なども含まれます。 3) これはある程度役に立ちます。どこかで公開されている実用的な例はありますか? maxent のドキュメントが見つかりませんでした。
私を助けてください。私はこれでちょっとブロックされています。