-
Notifications
You must be signed in to change notification settings - Fork 0
/
textwords.py
198 lines (166 loc) · 5.93 KB
/
textwords.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
''' On Page SEO counter.
This script will parse a markdown repository using an approximation
of the GoogleBot keyword assessment. It will extract noun entities
with more than one word from each markdown topic.
1. call `get_top_ten(corpus)`
2. output a dictionary of the top ten keywords.
Matt Briggs V1.3: 6.04.2019
From Golkonda
'''
import html as HTML
import nltk
import pandas as pd
import stoplist as SP
# Score algorithm
def make_SEO_dict(textcorpus):
SEO_dict = {
"title" : '',
"heading1": '',
"description" : "",
"filename": "",
"bodytitle" : "",
"intro" : "",
"imgtext": "",
"imgfilename": ""
}
introstate = 0
textcorpus = textcorpus.replace("### ", "(H3) ")
textcorpus = textcorpus.replace("## ", "(H2) ")
textlines = textcorpus.split("\n")
for l in textlines:
if l.find("title:") > -1:
SEO_dict['title'] = l[l.find("title:")+6:].strip()
elif l.find("description:") > -1:
SEO_dict['description'] = l[l.find("description:")+12:].strip()
elif l.find("![") > -1:
rawalttext = l[l.find("![")+2:l.find("]")].strip() + " "
SEO_dict['imgtext'] += rawalttext
imagepath = l[l.find("](")+2:l.find(")")].replace("-", " ").strip() + " "
slashes = imagepath.split("/")
rawfilenameext = slashes[len(slashes)-1]
rawfilename = rawfilenameext[:rawfilenameext.find(".")] + " "
SEO_dict['imgfilename'] += rawfilename
elif l.find("# ") > -1:
if SEO_dict["heading1"] == "":
SEO_dict['heading1'] += l[l.find("# ")+1:].strip()
introstate = 1
elif l.find("(H2)") > -1:
SEO_dict['bodytitle'] += l[l.find("## ")+5:].strip() + " "
elif l.find("(H3)") > -1:
SEO_dict['bodytitle'] += l[l.find("## ")+5:].strip() + " "
elif introstate > 0:
SEO_dict['intro'] += l + " "
introstate += 1
if introstate > 4:
introstate = 0
return SEO_dict
def score_SEO(SEO_dict, instring):
score = 0
if SEO_dict["title"].find(instring) > -1:
score += 5
if SEO_dict["heading1"].find(instring) > -1:
score += 5
if SEO_dict["description"].find(instring) > -1:
score += 5
if SEO_dict["filename"].find(instring) > -1:
score += 5
if SEO_dict["bodytitle"].find(instring) > -1:
score += 3
if SEO_dict["intro"].find(instring) > -1:
score += 3
if SEO_dict["imgtext"].find(instring) > -1:
score += 2
if SEO_dict["imgfilename"].find(instring) > -1:
score += 2
return score
# Parser Functions using NLTK
def extract_chunks(sent):
'''With a parsed sentence, return sets of entities.'''
grammar = r"""
NBAR:
# Nouns and Adjectives, terminated with Nouns
{<NN.*>*<NN.*>}
NP:
{<NBAR>}
# Above, connected with in/of/etc...
{<NBAR><IN><NBAR>}
"""
chunker = nltk.RegexpParser(grammar)
ne = set()
chunk = chunker.parse(nltk.pos_tag(nltk.word_tokenize(sent)))
for tree in chunk.subtrees(filter=lambda t: t.label() == 'NP'):
ne.add(' '.join([child[0] for child in tree.leaves()]))
return ne
def parse_sentences(incorpus):
'''Take a body text and return sentences in a list.'''
sentences = nltk.sent_tokenize(incorpus)
return sentences
def only_word_pairs(inlist):
'''Takes an list with strings, splits by space, and removes single items.'''
outlist = []
for i in inlist:
j = i.strip()
h = j.split()
if len(h) > 1:
outlist.append(i)
return outlist
def remove_blank(inlist):
'''Iterate over the list to remove blank entries.'''
noblank = []
for i in inlist:
x = i.strip()
if x:
noblank.append(x)
return noblank
def apply_stoplist(inlist):
'''Iterate over the list to remove stop items.'''
stoplist = SP.stoplist.split("\n")
outlist = []
for i in inlist:
if i not in stoplist:
outlist.append(i)
return outlist
def clean_keyword(inlist):
glyphs = '[]|<>*='
outlist = []
for i in inlist:
for char in glyphs:
i = i.replace(char, "")
outlist.append(i)
return outlist
def extract_entities(bodytext):
'''Take a multisentence text and return a list of unique entities.'''
breakdown = parse_sentences(bodytext)
entities = []
for sent in breakdown:
for i in extract_chunks(sent):
entities.append(i)
step1_entities = clean_keyword(entities)
step2_entities = remove_blank(step1_entities)
step3_entities = set(step2_entities) # remove duplicates
step4_entities = only_word_pairs(list(step3_entities))
step5_entities = apply_stoplist(step4_entities)
return step5_entities
def get_top_ten(textcorpus):
'''With a path name to a markdown file return the top 10 SEO ranked keywords in the file as a dictionary.'''
try:
bodytext = textcorpus
seo_dict = make_SEO_dict(bodytext)
record_terms = extract_entities(bodytext)
pagedata = {"SEO score" : [], "Count" : [], "Keyword" : []}
for term in record_terms:
pagedata["SEO score"].append(score_SEO(seo_dict, term))
pagedata["Count"].append(bodytext.count(term))
pagedata["Keyword"].append(term)
SEO_df_full = pd.DataFrame(pagedata).sort_values(by=["SEO score", "Count"], ascending=False).reset_index()
SEO_summary = SEO_df_full.loc[0:9].to_dict()
SEO_out = []
for i in SEO_summary['index']:
SEO_out.append(HTML.escape(SEO_summary['Keyword'][i]))
except Exception as e:
SEO_out = {1: {"error": "Unable to process file.", "message": str(e)}}
return SEO_out
def main():
print("This is the script that contains the functional logic.")
if __name__ == "__main__":
main()