/
Implementation of SEO algorithms2.py
62 lines (44 loc) · 1.66 KB
/
Implementation of SEO algorithms2.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
import re
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
class SEOLibrary:
def __init__(self, text):
self.text = text
def _preprocess_text(self):
text = self.text.lower()
text = text.replace('.', ' ')
text = text.replace(',', ' ')
text = text.replace(';', ' ')
text = text.replace('!', ' ')
text = text.replace('?', ' ')
text = text.replace('(', ' ')
text = text.replace(')', ' ')
text = text.replace('"', ' ')
text = text.replace("'", ' ')
return text
def get_keyword_density(self, keyword):
preprocessed_text = self._preprocess_text()
tokens = word_tokenize(preprocessed_text)
keyword_count = 0
for token in tokens:
if token == keyword:
keyword_count += 1
return keyword_count / len(tokens)
def get_title_tag(self):
# ...
def get_meta_description(self):
# ...
def get_heading_tags(self):
# ...
def get_image_alt_tags(self):
# ...
def get_internal_links(self):
# ...
def get_external_links(self):
# ...
# مثال استفاده از کتابخانه
text = "این یک متن نمونه برای پیادهسازی الگوریتمهای سئو است. در این متن سعی شده است از کلمات کلیدی مختلفی استفاده شود تا عملکرد کتابخانه به طور کامل بررسی شود."
seo_library = SEOLibrary(text)
keyword_density = seo_library.get_keyword_density("سئو")
print("چگالی کلمه کلیدی 'سئو':", keyword_density)
# ...