我一直在使用Ruby分类器库来对隐私政策进行分类。我得出的结论是,这个库内置的简单词袋方法是不够的。为了提高我的分类准确性,我想在单个词之外,还要对ngram进行训练以训练分类器。
我想知道是否有专门用于预处理文档以获取相关ngram的库(并正确处理标点符号)。我有一个想法是,我可以预处理文档,并将伪ngram输入到Ruby分类器中,如下所示:
wordone_wordtwo_wordthree
或者,可能有更好的方法,比如有一个库从一开始就内置了基于ngram的朴素贝叶斯分类。如果其他语言能完成这项工作,我愿意使用(如果需要的话,Python似乎是一个不错的选择)。
回答:
如果你不介意使用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._ngramsset([('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