-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspellbound.py
More file actions
285 lines (269 loc) · 11.9 KB
/
spellbound.py
File metadata and controls
285 lines (269 loc) · 11.9 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import sys, string, requests, enchant,collections,data_getter,time # requests is for HTTP requests; enchant is for checking spelling
from nltk.tag import pos_tag
word_repeat_limit=3
counted_comment_words=collections.Counter()#keeps track of words counted
dictionaryUS = enchant.Dict("en_US")
dictionaryGB = enchant.Dict("en_GB")
def most_popular(amount):
# GitHub's API only allows us to return up to 100 results per page, so amount should be <= 100
if amount > 100:
raise Exception("amount must be less than or equal to 100")
req = requests.get("https://api.github.com/search/repositories?q=stars:>0&per_page=" + str(amount)+str(secret))
popular = [tuple(repo["full_name"].split('/')) for repo in req.json()["items"]]
# the format of the returned value is (owner,repo)
return popular
def get_owner_repos(owner):
#not caching...
req = requests.get("https://api.github.com/users/" + owner + "/repos")
print req.json()
repos = [tuple(repo["full_name"].split('/')) for repo in req.json()]
return repos
def get_file_type(path):
if "." in path:
return path.rsplit(".")[1]
else:
return ""
def file_paths(owner,repo,branch):
tree = requests.get("https://api.github.com/repos/" + owner + "/" + repo + "/git/trees/"+branch+"?recursive=1"+str(secret)).json()
if tree.has_key("message"):
return []
# we look for the paths of files, not directories
files = [item["path"] for item in tree["tree"] if item["type"] == "blob"]
# ignore hidden files
files = [x for x in files if x[0] !="."]
files = [x for x in files if get_file_type(x) in ["js","py","rb","php","java","rs","c","md","txt"]] # we only can detect comments in certain file formats
return files
def get_words(line):
# returns a list of words in a given line (words can only include letters)
words = filter(lambda s: s.isalpha(),line.split())
return set(words)
def get_word_types(text,file_type,adding=False): #returns the line number and text of each single-line comment in a file
code_words=set([])
comment_words=set([])
line_number=1
current_word=""
in_code=True#This indicates what character is bounding.
comment_type=""
skip_next=False
if file_type in ("js","c","rs","java"):
for i in range(len(text)):
if skip_next:
skip_next=False
continue
char=text[i]
if char=="\\":
skip_next=True
elif in_code:
if char in string.lowercase:
current_word+=char
else:
code_words.add(current_word)
current_word=""
if char in string.uppercase:
current_word+=char
elif char=="/" and text[i+1]=="/":
skip_next=True
in_code=False
comment_type="//"
elif char=="/" and text[i+1]=="*":
skip_next=True
in_code=False
comment_type="/*"
elif char=="\n":
line_number+=1
else:
if char.lower() in string.lowercase or (current_word and char in ("'", "\xe2\x80\x99") and (text[i+1] in string.lowercase)):
current_word+=char
else:
if current_word!="":
comment_words.add((current_word,line_number))
if adding: counted_comment_words[current_word]+=1
current_word=""
if char=="\n" and comment_type=='//':
in_code=True
line_number+=1
elif char=="*" and text[i+1]=="/" and comment_type=='/*':
in_code=True
skip_next=True
elif char=="\n":
line_number+=1
elif file_type=="php":
for i in range(len(text)):
if skip_next:
skip_next=False
continue
char=text[i]
if char=="\\":
skip_next=True
elif in_code:
if char in string.lowercase:
current_word+=char
else:
code_words.add(current_word)
current_word=""
if char in string.uppercase:
current_word+=char
elif char=="/" and text[i+1]=="/":
skip_next=True
in_code=False
comment_type="//"
elif char=="/" and text[i+1]=="*":
skip_next=True
in_code=False
comment_type="/*"
elif char=="#":
in_code=False
comment_type="#"
elif char=="\n":
line_number+=1
else:
if char.lower() in string.lowercase or (current_word and char in ("'", "\xe2\x80\x99") and (text[i+1] in string.lowercase)):
current_word+=char
else:
if current_word!="":
comment_words.add((current_word,line_number))
if adding: counted_comment_words[current_word]+=1
current_word=""
if char=="\n" and comment_type=='//':
in_code=True
line_number+=1
elif char=="*" and text[i+1]=="/" and comment_type=='/*':
in_code=True
skip_next=True
elif char=="\n" and comment_type=='#':
in_code=True
line_number+=1
elif char=="\n":
line_number+=1
elif file_type=="py":
for i in range(len(text)):
if skip_next:
skip_next=False
continue
char=text[i]
if char=="\\":
skip_next=True
elif in_code:
if char in string.lowercase:
current_word+=char
else:
code_words.add(current_word)
current_word=""
if char in string.uppercase:
current_word+=char
elif char=="#":
in_code=False
comment_type="#"
elif char=="\n":
line_number+=1
else:
if char.lower() in string.lowercase or (current_word and char in ("'", "\xe2\x80\x99") and (text[i+1] in string.lowercase)):
current_word+=char
else:
if current_word!="":
comment_words.add((current_word,line_number))
if adding: counted_comment_words[current_word]+=1
current_word=""
if char=="\n" and comment_type=='#':
in_code=True
line_number+=1
elif char=="\n":
line_number+=1
elif file_type in ["md","txt"]:
in_code=False
for i in range(len(text)):
char=text[i]
if char.lower() in string.lowercase or (current_word and char in ("'", "\xe2\x80\x99") and (text[i+1] in string.lowercase)):
current_word+=char
elif current_word!="":
comment_words.add((current_word,line_number))
if adding: counted_comment_words[current_word]+=1
current_word=""
for i in counted_comment_words:
if counted_comment_words[i]>=word_repeat_limit:#yes its 5 for now
code_words.add(i)
return code_words.difference([""]),comment_words
def words_in_file(text):
wordset = set([])
for line in text:
wordset = wordset.union(get_words(line))
return wordset
def edits1(word): # this function is stolen from Peter Norvig's article http://norvig.com/spell-correct.html
splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = [a + b[1:] for a, b in splits if b]
transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
replaces = [a + c + b[1:] for a, b in splits for c in string.lowercase if b]
inserts = [a + c + b for a, b in splits for c in string.lowercase]
return set(deletes + transposes + replaces + inserts)
def check_spelling(owner,repo,branch="master",using_nltk=False):
print("Getting file paths.")
start_time=time.time()
paths = filter(get_file_type, file_paths(owner,repo,branch))
print("Getting paths took %s seconds."%(str(time.time()-start_time)))
print("Setting up requests.")
start_time=time.time()
data_getter.init(secret)
for path in paths:
if get_file_type(path):
data_getter.get_text(owner,repo,branch,path)
print("Setting up took %s seconds."%(str(time.time()-start_time)))
print("Getting text.")
start_time=time.time()
data_getter.start()
print("Getting text took %s seconds."%(str(time.time()-start_time)))
print("Counting words.")
start_time=time.time()
for path in paths:
text = data_getter.data[path]
get_word_types(text,get_file_type(path),True)#Adding to counted_comment_words
print("Counting words took %s seconds."%(str(time.time()-start_time)))
print("Finding misspellings.")
start_time=time.time()
for path in paths:
text = data_getter.data[path]
code_words,comment_words = get_word_types(text,get_file_type(path))
excused_words = code_words
for item in comment_words:
word,line_number=item
if len(word)<=2: #skip really short words
continue
if word[-2:] in ("'s","\xe2\x80\x99s"):#ignore ending in 's
word=word[:-2]
if using_nltk and pos_tag([word])[0][1]=="NNP":
continue
wordl = word.lower()
if not dictionaryUS.check(wordl) and not dictionaryGB.check(wordl):
if not word in excused_words:
# to narrow down the list of candidates, we only look at words that are one edit away from real words
edits = edits1(wordl)
try:
if True in map(lambda w: dictionaryUS.check(w), edits) or True in map(lambda w: dictionaryGB.check(w), edits):
url = "https://github.com/" + owner + "/" + repo + "/blob/"+branch+"/" + path + "#L" + str(line_number)
print(word + " from " + url)
except:
print("Error on word " + word)
print("Finding misspellings took %s seconds"%(str(time.time()-start_time)))
secret=""
def main():
using_nltk=False
if sys.argv[1]=="nltk":
using_nltk=True
sys.argv=[sys.argv[0]]+sys.argv[2:]
if sys.argv[1]=="secret":
secret="&client_id="+sys.argv[2]+"&client_secret="+sys.argv[3]
sys.argv=[sys.argv[0]]+sys.argv[4:]
if len(sys.argv) == 4:
check_spelling(sys.argv[1],sys.argv[2],sys.argv[3],using_nltk=using_nltk)
elif len(sys.argv)==3:
if sys.argv[1] == "popular":
for (owner,repo) in most_popular(int(sys.argv[2])):
print("Checking " + owner + "/" + repo + "....")
check_spelling(owner,repo,using_nltk=using_nltk)
else:
check_spelling(sys.argv[1],sys.argv[2],using_nltk=using_nltk)
else:
for (owner,repo) in get_owner_repos(sys.argv[1]):
print("Checking " + owner + "/" + repo + "....")
check_spelling(owner,repo,using_nltk=using_nltk)
if __name__ == '__main__':
main()