2015-02-08 6 views
5

Ich habe die Frage von here mit meinen Änderungen. Ich habe folgenden Code:Drucken Sie 10 am häufigsten vorkommende Wörter eines Textes, einschließlich und ohne Stoppwörter

from nltk.corpus import stopwords 
>>> def content_text(text): 
    stopwords = nltk.corpus.stopwords.words('english') 
    content = [w for w in text if w.lower() in stopwords] 
    return content 

Wie kann ich die 10 am häufigsten vorkommenden Wörter eines Textes, dass 1) einschließlich und 2) ohne Stoppwörter drucken ?

+0

möglich Duplikat [Wie kann ich zählen das Vorkommen eines Listenelements in Python?] (http://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item-in-python) –

Antwort

4

Nicht sicher auf der is stopwords in der Funktion, stelle ich mir es in sein muss, aber Sie können einen Counterdict mit most_common(10) verwenden, um den 10 häufigsten zu erhalten:

from collections import Counter 
from string import punctuation 


def content_text(text): 
    stopwords = set(nltk.corpus.stopwords.words('english')) # 0(1) lookups 
    with_stp = Counter() 
    without_stp = Counter() 
    with open(text) as f: 
     for line in f: 
      spl = line.split() 
      # update count off all words in the line that are in stopwrods 
      with_stp.update(w.lower().rstrip(punctuation) for w in spl if w.lower() in stopwords) 
       # update count off all words in the line that are not in stopwords 
      without_stp.update(w.lower().rstrip(punctuation) for w in spl if w not in stopwords) 
    # return a list with top ten most common words from each 
    return [x for x in with_stp.most_common(10)],[y for y in without_stp.most_common(10)] 
wth_stop, wthout_stop = content_text(...) 

Wenn Sie in einer nltk Datei sind vorbei Objekt iterieren nur darueber:

def content_text(text): 
    stopwords = set(nltk.corpus.stopwords.words('english')) 
    with_stp = Counter() 
    without_stp = Counter() 
    for word in text: 
     # update count off all words in the line that are in stopwords 
     word = word.lower() 
     if word in stopwords: 
      with_stp.update([word]) 
     else: 
      # update count off all words in the line that are not in stopwords 
      without_stp.update([word]) 
    # return a list with top ten most common words from each 
    return [k for k,_ in with_stp.most_common(10)],[y for y,_ in without_stp.most_common(10)] 

print(content_text(nltk.corpus.inaugural.words('2009-Obama.txt'))) 

Die nltk Methode Interpunktion enthält, so dass nicht sein kann, was Sie wollen.

+0

@ user2064809, sollte jetzt –

+0

arbeiten, wenn ich schreibe 'wth_stop, wthout_stop = content_text (nltk.corpus.inaugural.words ('2009-Obama.txt'))' Ich bekomme Fehler. – user2064809

+1

@ user2064809, ich habe es getestet und es funktioniert gut für mich, welchen Fehler bekommen Sie? –

7

Es gibt eine FreqDist Funktion in nltk

import nltk 
allWords = nltk.tokenize.word_tokenize(text) 
allWordDist = nltk.FreqDist(w.lower() for w in allWords) 

stopwords = nltk.corpus.stopwords.words('english') 
allWordExceptStopDist = nltk.FreqDist(w.lower() for w in allWords if w not in stopwords)  

10 am häufigsten zu extrahieren:

mostCommon= allWordDist.most_common(10).keys() 
+0

Ich bekomme diesen Fehler: AttributeError: 'FreqDist' Objekt hat kein Attribut 'Most_common' – user2064809

+0

Können Sie bitte vollständige Liste zur Verfügung stellen? – igorushi

+0

Sie sollten Stoppwörter mit Zeichenfolgen in Kleinbuchstaben eingeben. Von: 'allWordExceptStopDist = nltk.FreqDist (w.lower() für w in Allwords wenn w nicht in Stoppwörter)' An: 'allWordExceptStopDist = nltk.FreqDist (w.lower() für w in Allwords wenn w.lower() nicht in Stoppwörtern) ' – abevieiramota

1

du versuchen:

for word, frequency in allWordsDist.most_common(10): 
    print('%s;%d' % (word, frequency)).encode('utf-8') 
Verwandte Themen