在ngrams上训练朴素贝叶斯分类器

bgc*_*ode 11 ruby python nlp classification machine-learning

我一直在使用Ruby Classifier库分类隐私策略.我得出结论,这个库中内置的简单的词袋方法是不够的.为了提高我的分类准确度,我想在n-gram上训练分类器以及单个单词.

我想知道是否有一个库用于预处理文档以获得相关的n-gram(并正确处理标点符号).一个想法是我可以预处理文档并将伪ngram输入Ruby分类器,如:

wordone_wordtwo_wordthree

或许有更好的方法可以做到这一点,例如一个从getgo内置了基于ngram的Naive Bayes Classification的库.如果他们完成工作,我愿意使用Ruby以外的语言(如果需要,Python似乎是一个很好的候选人).

Nol*_*lty 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')])
Run Code Online (Sandbox Code Playgroud)

你甚至有一个方法 nltk.NaiveBayesClassifier

  • 与Ruby提供的相比,NLTK在很多方面都显得惊人.Python赢了,谢谢! (3认同)