11

プライバシーポリシーを分類するためにRubyClassifierライブラリを使用しています。私は、このライブラリに組み込まれている単純な一言一句のアプローチでは不十分であるという結論に達しました。分類の精度を上げるために、個々の単語に加えて、n-gramで分類器をトレーニングしたいと思います。

関連するn-gramを取得する(そして句読点を適切に処理する)ためにドキュメントを前処理するためのライブラリがそこにあるかどうか疑問に思いました。1つの考えは、ドキュメントを前処理して、次のように疑似ngramをRuby分類子にフィードできるというものでした。

wordone_wordtwo_wordthree

あるいは、これを行うためのより良い方法があるかもしれません。たとえば、getgoからngramベースの単純ベイズ分類が組み込まれているライブラリなどです。ここでは、Ruby以外の言語を使用しても問題ありません(必要に応じてPythonが適しているようです)。

4

2 に答える 2

12

Python に問題がなければ、nltkがぴったりだと思います。

例えば:

>>> import nltk
>>> s = "This is some sample data.  Nltk will use the words in this string to make ngrams.  I hope that this is useful.".split()
>>> model = nltk.NgramModel(2, s)
>>> model._ngrams
set([('to', 'make'), ('sample', 'data.'), ('the', 'words'), ('will', 'use'), ('some', 'sample'), ('', 'This'), ('use', 'the'), ('make', 'ngrams.'), ('ngrams.', 'I'), ('hope', 'that'
), ('is', 'some'), ('is', 'useful.'), ('I', 'hope'), ('this', 'string'), ('Nltk', 'will'), ('words', 'in'), ('this', 'is'), ('data.', 'Nltk'), ('that', 'this'), ('string', 'to'), ('
in', 'this'), ('This', 'is')])

あなたも方法を持っていますnltk.NaiveBayesClassifier

于 2012-04-09T20:21:11.283 に答える
3
>> s = "She sells sea shells by the sea shore"
=> "She sells sea shells by the sea shore"
>> s.split(/ /).each_cons(2).to_a.map {|x,y| x + ' ' +  y}
=> ["She sells", "sells sea", "sea shells", "shells by", "by the", "the sea", "sea shore"]

Ruby enumerable には enum_cons と呼ばれるメソッドがあり、enum_cons から n 個の連続する項目をすべて返します。その方法を使用すると、ngram を生成するのは単純なワンライナーです。

于 2012-04-10T04:24:06.040 に答える