10

私はいくつかのテキストを持っています:

 s="Imageclassificationmethodscan beroughlydividedinto two broad families of approaches:"

これを個々の単語に解析したいと思います。私はすぐにエンチャントとnltkを調べましたが、すぐに役立つと思われるものは何も見つかりませんでした。これに投資する時間があれば、単語が英語かどうかをチェックするエンチャントの機能を備えた動的プログラムを作成することを検討します。私はこれをオンラインで行う何かがあるだろうと思っていたでしょう、私は間違っていますか?

4

2 に答える 2

9

トライを使用した欲張りアプローチ

Biopython( )を使用してこれを試してくださいpip install biopython

from Bio import trie
import string


def get_trie(dictfile='/usr/share/dict/american-english'):
    tr = trie.trie()
    with open(dictfile) as f:
        for line in f:
            word = line.rstrip()
            try:
                word = word.encode(encoding='ascii', errors='ignore')
                tr[word] = len(word)
                assert tr.has_key(word), "Missing %s" % word
            except UnicodeDecodeError:
                pass
    return tr


def get_trie_word(tr, s):
    for end in reversed(range(len(s))):
        word = s[:end + 1]
        if tr.has_key(word): 
            return word, s[end + 1: ]
    return None, s

def main(s):
    tr = get_trie()
    while s:
        word, s = get_trie_word(tr, s)
        print word

if __name__ == '__main__':
    s = "Imageclassificationmethodscan beroughlydividedinto two broad families of approaches:"
    s = s.strip(string.punctuation)
    s = s.replace(" ", '')
    s = s.lower()
    main(s)

結果

>>> if __name__ == '__main__':
...     s = "Imageclassificationmethodscan beroughlydividedinto two broad families of approaches:"
...     s = s.strip(string.punctuation)
...     s = s.replace(" ", '')
...     s = s.lower()
...     main(s)
... 
image
classification
methods
can
be
roughly
divided
into
two
broad
families
of
approaches

警告

これが機能しない英語の退化したケースがあります。これらに対処するにはバックトラッキングを使用する必要がありますが、これで開始できます。

必須テスト

>>> main("expertsexchange")
experts
exchange
于 2013-03-12T17:00:50.377 に答える
1

これは、アジアのNLPで頻繁に発生する問題の一種です。辞書をお持ちの場合は、このhttp://code.google.com/p/mini-segmenter/を使用できます(免責事項:私が書いたので、気にしないでください)。

アルファベット英語の文字数は確かに音節中国語/日本語よりも長いため、検索スペースが非常に大きくなる可能性があることに注意してください。

于 2013-03-13T22:25:32.813 に答える