Commit 94925e96 authored by Franck Thollard's avatar Franck Thollard
Browse files

initial add of smart_completion

parent 0093f7e0
def read_file(filename):
with open(filename) as handle:
text = handle.read().replace("\n", "")
return text
def extract_pattern(text, pattern_size):
"""extract all the pattern of size pattern_size and return a list of them
"""
patterns = []
for i in range(pattern_size, len(text)):
patterns.append(text[i-pattern_size:i])
return patterns
def count_elem(sequence):
d = {}
for elem in sequence:
try:
d[elem.lower()] += 1
except KeyError:
d[elem.lower()] = 1
return d
def predict(pattern, count_dic):
maximum = -1
best = None
for key, value in count_dic.items():
if key.startswith(pattern):
if value > maximum:
best = key
maximum = value
return (best, maximum)
text = read_file("tom_sawyer.txt")
l = extract_pattern(text, 3)
counts = count_elem(l)
# print(f"counts = {counts}")
while True:
to_predict = input("give your prefix (quit for quit)")
to_predict = to_predict.lower()
if to_predict.startswith("quit"):
break
print(f"predicting {to_predict}", end=" ")
predicted = predict(to_predict, counts)
print(f"best prediction: {predicted[0]} (with count {predicted[1]})")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment