Проблема в том, что lemma.lemmatize
ожидает string
, а вы передаете list
.Элементы filter_set
являются lists
.Вам нужно изменить строку:
lem.append(lemma.lemmatize(w))
на что-то вроде этого:
lem.append([wi for wi in map(lemma.lemmatize, w)])
Приведенный выше код применяет lemma.lemmatize к каждому токену (wi
) в w
.Полный код:
import nltk, re
import string
from collections import Counter
from string import punctuation
from nltk.tokenize import TweetTokenizer, sent_tokenize, word_tokenize
from nltk.corpus import gutenberg, stopwords
from nltk.stem import WordNetLemmatizer
def remove_punctuation(from_text):
table = str.maketrans('', '', string.punctuation)
stripped = [w.translate(table) for w in from_text]
return stripped
def preprocessing():
raw_data = (gutenberg.raw('shakespeare-hamlet.txt'))
tokens_sentences = sent_tokenize(raw_data)
tokens = [[word.lower() for word in line.split()] for line in tokens_sentences]
print(len(tokens))
stripped_tokens = [remove_punctuation(i) for i in tokens]
sw = (stopwords.words('english'))
filter_set = [[token for token in sentence if (token.lower() not in sw and token.isalnum())] for sentence in
stripped_tokens]
lemma = WordNetLemmatizer()
lem = []
for w in filter_set:
lem.append([wi for wi in map(lemma.lemmatize, w)])
return lem
result = preprocessing()
for e in result[:10]: # take the first 10 results
print(e)
Выход
['tragedie', 'hamlet', 'william', 'shakespeare', '1599', 'actus', 'primus']
['scoena', 'prima']
['enter', 'barnardo', 'francisco', 'two', 'centinels']
['barnardo']
['who']
['fran']
['nay', 'answer', 'stand', 'vnfold', 'selfe', 'bar']
['long', 'liue', 'king', 'fran']
['barnardo']
['bar']
ОБНОВЛЕНИЕ
Для получения частот вы можете использовать Counter
:
result = preprocessing()
frequencies = Counter(word for sentence in result for word in sentence)
for word, frequency in frequencies.most_common(10): # get the 10 most frequent words
print(word, frequency)
Выход
ham 337
lord 217
king 180
haue 175
come 127
let 107
shall 107
hamlet 107
thou 105
good 98