0

reddit praw api を使用して、reddit の投稿タイトルで最も人気のある単語を見つけるためのコードをいくつか書きました。

import nltk
import praw

picksub = raw_input('\nWhich subreddit do you want to analyze? r/')
many = input('\nHow many of the top words would you like to see? \n\t> ')

print 'Getting the top %d most common words from r/%s:' % (many,picksub)
r = praw.Reddit(user_agent='get the most common words from chosen subreddit')
submissions = r.get_subreddit(picksub).get_top_from_all(limit=200)

hey = []

for x in submissions:
    hey.extend(str(x).split(' '))   

fdist = nltk.FreqDist(hey) # creates a frequency distribution for words in 'hey'
top_words = fdist.keys()

common_words = ['its','am', 'ago','took', 'got', 'will', 'been', 'get', 'such','your','don\'t', 'if', 'why', 'do', 'does', 'or', 'any', 'but', 'they', 'all', 'now','than','into','can', 'i\'m','not','so','just', 'out','about','have','when', 'would' ,'where', 'what', 'who' 'I\'m','says' 'not', '', 'over', '_', '-','after', 'an','for', 'who', 'by', 'from', 'it', 'how', 'you', 'about' 'for', 'on', 'as', 'be', 'has', 'that', 'was', 'there', 'with','what', 'we', '::', 'to', 'the', 'of', ':', '...', 'a', 'at', 'is', 'my', 'in' , 'i', 'this', 'and', 'are', 'he', 'she', 'is', 'his', 'hers']
already = []
counter = 0
number = 1

print '-----------------------'
for word in top_words:  
    if word.lower() not in common_words and word.lower() not in already:
        print str(number) + ". '" + word + "'"
        counter +=1
    number +=1
    already.append(word.lower())
if counter == many:
    break
print '-----------------------\n'

そのため、subreddit 'python' を入力して 10 件の投稿を取得すると、次のように返されます。


  1. 「パイソン」
  2. 「ピピ」
  3. 'コード'
  4. '使用する'
  5. '136'
  6. '181'
  7. 「だ...」
  8. 「アイパイソン」
  9. '133'

    10.「158」

このスクリプトが数字や「d...」などのエラー ワードを返さないようにするにはどうすればよいですか? 最初の 4 つの結果は受け入れられますが、この残りを意味のある言葉に置き換えたいと思います。リストを common_words にするのは不合理であり、これらのエラーをフィルター処理しません。私はコードを書くことに比較的慣れていないので、助けていただければ幸いです。

4

1 に答える 1

0

同意しません。一般的な単語のリストを作成することは正しいことです。for、I、am などを除外する簡単な方法はありません。ただし、単語ではない結果を除外するために common_words リストを使用するのは合理的ではありません。不要なすべての非単語を含める必要があります。非単語は別の方法で除外する必要があります。

いくつかの提案:
1) common_words は である必要がありset()ます。リストが長いため、これにより速度が向上します。セットのin操作は O(1) ですが、リストの場合は O(n) です。

2) すべての数値文字列を取り除くのは簡単です。あなたがそれを行うことができる1つの方法は次のとおりです。

all([w.isdigit() for w in word])

これが True を返す場合、単語は単なる一連の数字です。

3) d... を取り除くのはもう少しトリッキーです。非単語をどのように定義するかによって異なります。これ:

tf = [ c.isalpha() for c in word ]

True/False 値のリストを返します (char が文字でない場合は False です)。次に、次のような値をカウントできます。

t = tf.count(True)
f = tf.count(False)

次に、非単語を、文字よりも多くの非文字文字を含むもの、非文字文字をまったく含まないものなどとして定義できます。たとえば、次のようになります。

def check_wordiness(word):
    # This returns true only if a word is all letters
    return all([ c.isalpha() for c in word ])

4)for word in top_words:ブロック内で、カウンターと数字を混同していませんか? また、カウンターと数値はかなり冗長なので、最後のビットを次のように書き換えることができます。

for word in top_words:
    # Since you are calling .lower() so much, 
    # you probably want to define it up here
    w = word.lower() 
    if w not in common_words and w not in already:
        # String formatting is preferred over +'s
        print "%i. '%s'" % (number, word)
        number +=1
    # This could go under the if statement. You only want to add
    # words that could be added again.  Why add words that are being
    # filtered out anyways?
    already.append(w)

    # this wasn't indented correctly before
    if number == many:
        break

それが役立つことを願っています。

于 2013-08-14T22:08:07.480 に答える