Веса для ключевых фраз в тексте
Есть статья, мне в ней нужно выделить ключевые фразы по паттерну и определить их веса. Ключевые фразы я выделил.Привожу код
import spacy
from spacy.matcher import Matcher
nlp = spacy.load('ru_core_news_lg')
text = '''Тут текст статьи'''
doc = nlp(text)
matcher = Matcher(nlp.vocab)
pattern = [{"POS": "ADJ"}, {"POS": "NOUN"}]
matcher.add("AdjNoun", [pattern])
matches = matcher(doc)
for match_id, start, end in matches:
span = doc[start:end]
print(span)
Ключевые фразы получил. Как теперь определить их веса, подскажите пожалуйста
Ответы (1 шт):
Автор решения: CrazyElf
→ Ссылка
Используйте TfidfVectorizer. Пример из документации:
>>> from sklearn.feature_extraction.text import TfidfVectorizer
>>> corpus = [
... 'This is the first document.',
... 'This document is the second document.',
... 'And this is the third one.',
... 'Is this the first document?',
... ]
>>> vectorizer = TfidfVectorizer()
>>> X = vectorizer.fit_transform(corpus)
>>> print(vectorizer.get_feature_names())
['and', 'document', 'first', 'is', 'one', 'second', 'the', 'third', 'this']
>>> print(X.shape)
(4, 9)