LoginSignup
19
19

More than 5 years have passed since last update.

PythonでN-Gram

Posted at

python-ngramを使った

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ngram

text = u'あいうえお'
index = ngram.NGram(N=2)
for term in index.ngrams(index.pad(text)):
    print term

結果

$ python sample.py 
$あ
あい
いう
うえ
えお
お$

他にもたくさん機能があるので、上記リンク先のチュートリアルなどを読むべし
検索もできるようだ

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ngram

G = ngram.NGram(['joe','joseph','jon','john','sally'])
print G.search("joseph")
$ python sample.py
[('joseph', 1.0), ('joe', 0.18181818181818182), ('jon', 0.18181818181818182), ('john', 0.16666666666666666)]
19
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
19