2

(方法) OpenNLP Document Classifier で Bigram 機能を使用できますか?

非常に短いドキュメント (タイトル、フレーズ、文) のコレクションがあり、ツール LibShortText で使用される種類のバイグラム機能を追加したいと考えています。

http://www.csie.ntu.edu.tw/~cjlin/libshorttext/

これは可能ですか?

ドキュメントでは、Name Finder を使用してこれを行う方法のみを説明しています。

BigramNameFeatureGenerator()

ドキュメント分類子ではありません

4

2 に答える 2

2

トレーナーと分類子はメソッドでカスタム機能ジェネレーターを許可していると思いますが、それらは FeatureGenerator の実装である必要があり、BigramFeatureGenerator はその実装ではありません。そこで、以下の内部クラスとして簡単な impl を作成しました。機会があれば、この (テストされていない) コードを試してください。

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.List;
    import opennlp.tools.doccat.DoccatModel;
    import opennlp.tools.doccat.DocumentCategorizerME;
    import opennlp.tools.doccat.DocumentSample;
    import opennlp.tools.doccat.DocumentSampleStream;
    import opennlp.tools.doccat.FeatureGenerator;
    import opennlp.tools.util.ObjectStream;
    import opennlp.tools.util.PlainTextByLineStream;



    public class DoccatUsingBigram {

      public static void main(String[] args) throws IOException {
        InputStream dataIn = new FileInputStream(args[0]);
        try {


          ObjectStream<String> lineStream =
                  new PlainTextByLineStream(dataIn, "UTF-8");
//here you can use it as part of building the model
          ObjectStream<DocumentSample> sampleStream = new DocumentSampleStream(lineStream);
          DoccatModel model = DocumentCategorizerME.train("en", sampleStream, 10, 100, new MyBigramFeatureGenerator());


          ///now you would use it like this

          DocumentCategorizerME classifier = new DocumentCategorizerME(model);
          String[] someData = "whatever you are trying to classify".split(" ");
          Collection<String> bigrams = new MyBigramFeatureGenerator().extractFeatures(someData);
          double[] categorize = classifier.categorize(bigrams.toArray(new String[bigrams.size()]));


        } catch (IOException e) {
          // Failed to read or parse training data, training failed
          e.printStackTrace();
        }

      }

      public static class MyBigramFeatureGenerator implements FeatureGenerator {

        @Override
        public Collection<String> extractFeatures(String[] text) {
          return generate(Arrays.asList(text), 2, "");
        }

        private  List<String> generate(List<String> input, int n, String separator) {

          List<String> outGrams = new ArrayList<String>();
          for (int i = 0; i < input.size() - (n - 2); i++) {
            String gram = "";
            if ((i + n) <= input.size()) {
              for (int x = i; x < (n + i); x++) {
                gram += input.get(x) + separator;
              }
              gram = gram.substring(0, gram.lastIndexOf(separator));
              outGrams.add(gram);
            }
          }
          return outGrams;
        }
      }
    }

お役に立てれば...

于 2014-01-18T21:52:54.990 に答える
0

ユースケースには、OpenNLP[1] の NGramFeatureGenerator.java クラスを使用できます。

[1] https://github.com/apache/opennlp

ありがとう、マダワ

于 2016-04-21T07:23:46.657 に答える