71

I have a series of text items- raw HTML from a MySQL database. I want to find the most common phrases in these entries (not the single most common phrase, and ideally, not enforcing word-for-word matching).

My example is any review on Yelp.com, that shows 3 snippets from hundreds of reviews of a given restaurant, in the format:

"Try the hamburger" (in 44 reviews)

e.g., the "Review Highlights" section of this page:

http://www.yelp.com/biz/sushi-gen-los-angeles/

I have NLTK installed and I've played around with it a bit, but am honestly overwhelmed by the options. This seems like a rather common problem and I haven't been able to find a straightforward solution by searching here.

4

4 に答える 4

100

最も一般的なフレーズだけではなく、最も興味深いコロケーションが必要なのではないかと思います。そうしないと、一般的な単語で構成されたフレーズが過剰に表現され、興味深く有益なフレーズが少なくなる可能性があります。

これを行うには、基本的にデータから n グラムを抽出し、点ごとの相互情報量(PMI)が最も高いものを見つける必要があります。つまり、偶然に予想されるよりもはるかに多くの単語が一緒に出現することを見つけたいと考えています。

NLTK コロケーションのハウツーでは、約 7 行のコードでこれを行う方法を説明しています。

import nltk
from nltk.collocations import *
bigram_measures = nltk.collocations.BigramAssocMeasures()
trigram_measures = nltk.collocations.TrigramAssocMeasures()

# change this to read in your data
finder = BigramCollocationFinder.from_words(
    nltk.corpus.genesis.words('english-web.txt'))

# only bigrams that appear 3+ times
finder.apply_freq_filter(3)

# return the 10 n-grams with the highest PMI
finder.nbest(bigram_measures.pmi, 10)
于 2010-03-16T09:35:52.420 に答える
5

あなたが探しているのはchunkingだと思います。NLTK 本の第 7 章、またはチャンク抽出に関する自分の記事を読むことをお勧めします。これらは両方とも、品詞のタグ付けに関する知識を前提としています。これについては、第 5 章で説明します。

于 2010-04-15T02:37:02.160 に答える
4

3 つ以上の ngram を取得したい場合は、これを試すことができます。htmlなどのジャンクをすべて取り除いたと思います。

import nltk
ngramlist=[]
raw=<yourtextfile here>

x=1
ngramlimit=6
tokens=nltk.word_tokenize(raw)

while x <= ngramlimit:
  ngramlist.extend(nltk.ngrams(tokens, x))
  x+=1

私はこれを自分で1か月ほどしか行っていないので、おそらくあまりpythonicではありませんが、役立つかもしれません!

于 2010-03-28T21:12:33.690 に答える
0

まず最初に、おそらくすべての HTML タグを削除する必要があります (「<[^>]*>」を検索し、「」に置き換えます)。その後、2 つのテキスト項目ごとに共通する最長の部分文字列を探す単純な方法を試すこともできますが、あまり良い結果が得られるとは思えません。最初に単語を正規化 (基本形に戻し、すべてのアクセントを削除し、すべてを小文字または大文字に設定) してから分析すると、より良い結果が得られる場合があります。繰り返しますが、何を達成したいかによっては、語順の柔軟性を考慮すれば、テキスト項目をより適切にクラスター化できる場合があります。つまり、テキスト項目を正規化された単語のバッグとして扱い、バッグの内容の類似性を測定します。

ここで、同様の (同一ではありませんが) トピックについてコメントしました。

于 2010-03-16T09:21:44.273 に答える