2

[プロジェクト スタック : 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 のドキュメントが見つかりませんでした。

私を助けてください。私はこれでちょっとブロックされています。

4

2 に答える 2

0

私の知る限り、新しいトレーニング サンプルを追加する場合は、MaxEnt モデルを完全に再トレーニングする必要があります。オンラインで段階的に行うことはできません

opennlp maxent のデフォルトの入力形式は、各行が 1 つのサンプルを表すテキスト ファイルです。サンプルは、空白で区切られたトークン (機能) で構成されます。トレーニング中、最初のトークンは結果を表します。

ここで私の最小限の作業例を見てください: openNLP maxent を使用したモデルのトレーニング

于 2016-04-05T23:22:19.477 に答える
0

1) サンプルをデータベースに保存できます。これにはaccumuloを1回使用しました。その後、一定の間隔でモデルを再構築し、データを再処理します。2) 形式は次のとおりです。カテゴリ名スペース サンプル改行。タブなし 3) 一般的な感情をトピックまたはエンティティと組み合わせたいようです。名前ファインダーまたは単に正規表現を使用してエンティティを検索するか、製品名などを含むdoccatのクラスラベルにエンティティを追加すると、サンプルは非常に具体的である必要があります

于 2014-04-15T12:32:31.297 に答える