-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_random_sentences.py
More file actions
96 lines (81 loc) · 3.5 KB
/
generate_random_sentences.py
File metadata and controls
96 lines (81 loc) · 3.5 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
import os
import random
dataset_file = "yestu hottige\nyaavaga meeting\nyelli bartire meeting ge\nyaaru bartire class ge\nenu maditiri\nyaake class\nyestu meeting hottige\nyaake meeting\nyaavaga bartire\nyelli ide class\nyaaru avaru\nyestu togottiri"
model = {}
generated = []
predicted = []
def save_data_to_file(filename):
f = open(fileName, "w+")
f.write(dataset_file)
f.close()
def set_corpus(filename):
with open(filename) as f:
for line in f:
line = line.split()
for i, word in enumerate(line):
if i == len(line)-1:
model['END'] = model.get('END', []) + [word]
else:
if(i == 0):
model['START'] = model.get('START', []) + [word]
model[word] = model.get(word, []) + [line[i+1]]
i+= 1
def get_sentence(startWord, predicted):
words = []
output = ""
while True:
if len(predicted) == 0:
words = startWord
elif predicted[-1] in model['END']:
break
else:
try:
words = model[predicted[-1]]
except KeyError:
predicted = []
break
if(len(predicted) == 0):
predicted.append(words)
else:
predicted.append(random.SystemRandom().choice(words))
#print("Data in Predicted list = ",predicted)
if(len(predicted) == 0):
output = "No corpus available to generate sentence for the given word"
else:
output = ' '.join(predicted)
return(output)
eng_trans = {"yestu hottige": "What time?", "yaavaga meeting": "What time is the meeting?", "yelli bartire meeting ge": "Where do you come for meeting?",
"yaaru bartire class ge": "Who will come for the class?", "enu maditiri": "What are you doing?", "yaake class": "Why there is a class?", "yestu meeting hottige": "What time will you go for meeting?",
"yaake meeting": "Why is the meeting for?", "yaavaga bartire": "When will you come?", "yelli ide class": "Where is the class?", "yaaru avaru": "Whoz that?", "yestu togottiri": "How much is it?"}
def get_english_translation(kannada):
output = ""
try:
output = eng_trans[kannada]
except KeyError:
for key in eng_trans:
if kannada in key:
output = eng_trans[key]
if(len(output) == 0):
output = "No translation available for the generated Kannada Sentence"
return output
fileName = os.getcwd()+"\datafile.txt"
save_data_to_file(fileName)
set_corpus(fileName)
while(True):
print("Enter the word to generate sentence: Example words: Yestu, Enu, Yaavaga, Yaaru, Yelli, Yaake")
print("Enter quit to quit from the learning")
line = input()
line = line.lower()
if(line != 'quit'):
kannada_sentence = get_sentence(line,predicted)
if(kannada_sentence != "No corpus available to generate sentence for the given word"):
print ("Example usage for the word: ",line)
eng_sentence = get_english_translation(kannada_sentence)
print(kannada_sentence, ", Loosely translated to: ", eng_sentence)
else:
print ("No corpus available to generate sentence for the given word: ", line)
predicted=[]
print("\n")
else:
print ("Hope you had fun, while learning Kannada :)")
break