summaryrefslogtreecommitdiff
path: root/monkey/monkey.py
diff options
context:
space:
mode:
Diffstat (limited to 'monkey/monkey.py')
-rwxr-xr-xmonkey/monkey.py25
1 files changed, 23 insertions, 2 deletions
diff --git a/monkey/monkey.py b/monkey/monkey.py
index 99f5a2a..02048de 100755
--- a/monkey/monkey.py
+++ b/monkey/monkey.py
@@ -2,11 +2,11 @@
import math
import time
-
import prolog.engine
+
from .edits import classify_edits
from prolog.util import Token, annotate, compose, map_vars, normalized, rename_vars, stringify
-from .util import PQueue
+from .util import damerau_levenshtein, PQueue
# Check whether all tests for problem [name] succeed.
def test(name, code):
@@ -311,3 +311,24 @@ def fix_hints(code, path):
program[idx:idx+len(a)] = [t.clone(pos=program[idx].pos) for t in b]
yield fix_type, start, end, msg
+
+
+# Checks for typos in the code and suggest the nearst uploaded term by other users.
+def check_typos(code, names):
+ for token in annotate(code):
+ if token.type == 'NAME':
+ nearest_name = ' '
+ nearest_dist = 1000
+ own_count = names.get(token.val, 0) # count of the token.val which is compared with the
+ # each name in the names
+ for name in names.items():
+ if name[0] == token.val: # If the names are the skip the code
+ continue
+
+ distance = damerau_levenshtein(token.val, name[0])
+
+ if distance < nearest_dist and distance > 0 and own_count < name[1]:
+ nearest_dist = distance # Set best_dist and best_name if the less one is found
+ nearest_name = name[0]
+ if nearest_dist > 0 and nearest_dist/len(nearest_name) <= 1/3:
+ yield 'typo', token.pos, token.pos + len(token.val) , 'Did you mean "{}"?'.format(nearest_name)