2012-04-09 14 views
10

私はRuby Classifier libraryclassify privacy policiesを使っています。私は、この図書館に組み込まれた簡単な語彙のアプローチが十分ではないという結論に達しました。私の分類精度を上げるために、個々の単語に加えてnグラムで分類子を訓練したいと思っています。niveのNaive Bayesクラシファイア

関連するnグラムを取得するためのドキュメントを前処理するためのライブラリがあるかどうか疑問に思っていました(句読点を正しく扱います)。

wordone_wordtwo_wordthree

それとも、これを行うことがより良い方法は、持っているライブラリとして、があります:一つの考えは、私が前処理文書可能性があり、同様にRubyの分類子に擬似ngramsを養うということでしたngramベースのNaive Bayes分類は、getgoからそれに組み込まれています。私はRuby以外の言語を使っています(もしPythonが必要ならば良い候補のように思えます)。

答えて

11

もしあなたが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

+0

偉大な答え+1 – Yavar

+3

NLTKは、Rubyが提供するものに比べて、多くの点で驚くようです。 Pythonが勝つ、ありがとう! – babonk

+0

@babonk私の喜び。私はnltkが使用する喜びであり、信じられないほど強力であることを発見しました。D –

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のenumerablesが列挙からn個の連続項目のすべてのを返しますenum_consというメソッドを持っている方法を持っています。この方法では、ngramsを生成するのは簡単なライナーです。

+0

Thx。 'enum_cons'の代わりに' each_cons'を使う必要がありました。 – Dru

+0

Dru:enum_consのように思われます。それを私の答えのeach_consに置き換えました。ありがとう! –

関連する問題