Лемматизировать имена и прозвища с SpaCy - PullRequest
0 голосов
/ 02 мая 2020

Я хотел бы иметь график слов вхождения в заданный текст. Приведенный выше код работает хорошо, но он считает «Мэтью» и «Мат» как два разных слова.

Как заставить SpaCy считать его одним и тем же словом?

def cleanup_text(docs):
    texts = []
    counter = 1
    for doc in docs:
        if counter % 100 == 0:
            print('Processed {} out of {}'.format(counter, len(docs)))
        counter += 1
        doc = nlp(doc, disable=['parser', 'ner'])
        tokens = [tok.lemma_.lower().strip() for tok in doc if tok.lemma_ != '-PRON-']
        tokens = [tok for tok in tokens if tok not in stopwords and tok not in punctuations]
        tokens = ' '.join(tokens)
        texts.append(tokens)
    return pd.Series(texts)

def make_barplot_for_author(texts):
    text_clean = cleanup_text(texts)
    text_clean = ' '.join(author_clean).split()
    text_clean = [word for word in texts_clean if word not in '\'s']
    text_counts = Counter(texts_clean)
    NUM_WORDS = 25
    text_common_words = [word[0] for word in texts_counts.most_common(NUM_WORDS)]
    text_common_counts = [word[1] for word in texts_counts.most_common(NUM_WORDS)]
    plt.figure(figsize=(15, 12))
    sns.barplot(x=text_common_counts, y=text_common_words)
    plt.title('Words that Apo use frequently', fontsize=20)
    plt.show()

1 Ответ

0 голосов
/ 03 мая 2020

Что ж, перед тем, как перейти к функции с помощью проверки NLTK, вам нужно лемматизировать здесь

...