-
Notifications
You must be signed in to change notification settings - Fork 0
/
untitled8.py
122 lines (104 loc) · 3.02 KB
/
untitled8.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
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 15 10:08:15 2021
@author: LiWeiJun
"""
def read_dictionary(dictionary_file):
''' read dictionary file'''
d = {}
f = open(dictionary_file, 'r')
for line in f.readlines():
kv = line.strip().split(',')
if len(kv) == 2:
d[kv[0]] = float(kv[1])
f.close()
return d
def read_words(input_file):
''' read words file '''
w = []
f = open(input_file, 'r')
for line in f.readlines():
word = line.strip()
if word:
w.append(word)
f.close()
return w
def read_keyboard(keyboard_file):
''' read keyboard file '''
d = {}
f = open(keyboard_file, 'r')
for line in f.readlines():
letters = line.strip().split(' ')
letter = letters.pop(0)
d[letter] = letters
f.close()
return d
def correct_words(d, w, k):
''' correct words '''
for word in w:
freq = []
if word in d:
print('%15s -> FOUND' % word)
else:
freq.extend(drop(word, d))
freq.extend(insert(word, d))
freq.extend(swap(word, d))
freq.extend(replace(word, d, k))
# sort result
freq.sort(reverse=True)
freq = freq[:3]
# display result
if len(freq) == 0:
print('%15s -> NOT FOUND' % word)
else:
words = []
for f in freq:
if f[1] not in words:
words.append(f[1])
print('%15s -> FOUND %d: %s' % (word, len(freq), ' '.join(words)))
def drop(word, d):
''' try drop '''
freq = []
for i in range(len(word)):
w = word[:i] + word[i + 1:]
if w in d:
freq.append((d[w], w))
return freq
def insert(word, d):
''' try insert '''
freq = []
for i in range(len(word) + 1):
for j in range(97, 123):
ch = chr(j)
w = word[:i] + ch + word[i:]
if w in d:
freq.append((d[w], w))
return freq
def swap(word, d):
''' try swap '''
freq = []
for i in range(len(word) - 1):
w = word[:i] + word[i + 1] + word[i] + word[i + 2:]
if w in d:
freq.append((d[w], w))
return freq
def replace(word, d, k):
''' try replace '''
freq = []
for i in range(len(word)):
for r in k[word[i]]:
w = word[:i] + r + word[i + 1:]
if w in d:
freq.append((d[w], w))
return freq
if __name__ == '__main__':
dictionary_file = input('Dictionary file => ').strip()
print(dictionary_file)
input_file = input('Input file => ').strip()
print(input_file)
keyboard_file = input('Keyboard file => ').strip()
print(keyboard_file)
d = read_dictionary(dictionary_file)
w = read_words(input_file)
k = read_keyboard(keyboard_file)
correct_words(d, w, k)