-
Notifications
You must be signed in to change notification settings - Fork 1
/
Keyword analysis library.py
69 lines (56 loc) · 2.62 KB
/
Keyword analysis library.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import re
from collections import Counter
class KeywordAnalyzer:
def __init__(self, text):
self.text = text
self.words = self._preprocess_text(text)
def _preprocess_text(self, text):
# حذف علائم نگارشی
text = re.sub(r'[^\w\s]', '', text)
# تبدیل حروف بزرگ به حروف کوچک
text = text.lower()
# تبدیل متن به لیست کلمات
words = text.split()
return words
def get_most_frequent_words(self, n=10):
"""
برگرداندن n کلمه با بیشترین فرکانس
"""
# شمارش تعداد دفعات تکرار هر کلمه
word_counts = Counter(self.words)
# مرتب سازی کلمات بر اساس تعداد دفعات تکرار
most_frequent_words = word_counts.most_common(n)
return most_frequent_words
def get_keywords_by_tfidf(self):
"""
برگرداندن کلمات کلیدی با استفاده از TF-IDF
"""
# محاسبه TF برای هر کلمه
term_frequencies = {}
for word in self.words:
term_frequencies[word] = self.words.count(word) / len(self.words)
# محاسبه IDF برای هر کلمه
document_count = 1 # در این مثال فقط یک سند داریم
inverse_document_frequencies = {}
for word in self.words:
inverse_document_frequencies[word] = math.log(document_count / (1 + len([doc for doc in self.text if word in doc])))
# محاسبه TF-IDF برای هر کلمه
tfidf_scores = {}
for word in self.words:
tfidf_scores[word] = term_frequencies[word] * inverse_document_frequencies[word]
# مرتب سازی کلمات بر اساس TF-IDF
keywords_by_tfidf = sorted(tfidf_scores.items(), key=lambda x: x[1], reverse=True)
return keywords_by_tfidf
# مثال استفاده از کتابخانه
text = """
این یک متن نمونه برای تحلیل کلمات کلیدی است.
در این متن کلماتی مانند "کلمه کلیدی" و "تحلیل"
چندین بار تکرار شدهاند.
"""
analyzer = KeywordAnalyzer(text)
# دریافت 10 کلمه با بیشترین فرکانس
most_frequent_words = analyzer.get_most_frequent_words()
print("10 کلمه با بیشترین فرکانس:", most_frequent_words)
# دریافت کلمات کلیدی با استفاده از TF-IDF
keywords_by_tfidf = analyzer.get_keywords_by_tfidf()
print("کلمات کلیدی با استفاده از TF-IDF:", keywords_by_tfidf)