-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainFinal.py
230 lines (189 loc) · 7.54 KB
/
mainFinal.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
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
import os
import SSF_converter.SSF_to_Input as input_converter
import SSF_converter.output_to_SSF as ssf_converter
import SSF_converter.output_to_SSF2 as ssf_converter2
import morph_analyser.make_prediction as morph_analyser
import Pos_Tagger.final_predict_model as pos_tagger
import chunking.predict as chunker
import lexical.dictionaryAmit1 as lexical
# import morph_generation.morph_inflection as morph_generator
import torch
from wxconv import WXC
con = WXC(order='utf2wx')
con1 = WXC(order='wx2utf', lang='hin')
BASE_DIR = os.path.dirname(os.path.abspath('__file__'))
BASE_DIR += '/SSF_converter/'
main_file = BASE_DIR + "main_format.txt"
local_add = os.path.dirname(os.path.abspath('__file__'))
pos_tagger_input_file = local_add + '/Pos_Tagger/sentinput.txt'
chunker_input_file = local_add + '/chunking/input.txt'
match_list=[]
checklist = []
def main_format_writer(data):
# This file writes in main_format.txt.
out_main_file = open(main_file, 'w', encoding='utf-8')
for each in main_format_data:
out_main_file.write('\t'.join(each) + '\n')
out_main_file.write('\n')
out_main_file.flush()
out_main_file.close()
def block_maker():
# This function print a line in the output file SSF.txt
for i in range(80):
ssf_converter.out_temp_file.write('-')
ssf_converter.out_temp_file.write('\n\n')
ssf_converter.out_temp_file.flush()
while 1:
block_maker()
inp = input("Enter the sentence in Bhojpuri: ").split()
print(inp)
ssf_converter.out_temp_file.write('New Sentence = ' + ' '.join(inp) + '\n\n')
# main format data store the output of different modules.
main_format_data = []
for i in range(1, len(inp) + 1):
temp = []
temp.append(str(i))
temp.append('open_bracket_here')
main_format_data.append(temp)
temp = []
temp.append(str(i) + '.1')
temp.append(str(i - 1))
temp.append(str(i))
temp.append(inp[i - 1])
main_format_data.append(temp)
# Morph analyser is run here and output is store in
# "output" variable.
output = morph_analyser.main(inp)
outputM = output
# print(outputM)
print(output)
# storelist = []
for c in range(len(output)):
# match_list.append(output[c][0][0])
match_list.append(output[c][0][2])
# storelist.append(output[c][0][2])
# storelist.append(output[c][0][3])
# storelist.append(output[c][0][4])
# storelist.append(output[c][0][5])
# storelist.append(output[c][0][7])
print(match_list)
# print(storelist)
# output is stored in "main_format_data" from "output"
# variable
j = 0
for i in range(len(output)):
while main_format_data[j][1] == 'open_bracket_here':
j += 1
main_format_data[j].append(output[i][0][2])
main_format_data[j].append(output[i][0][1])
main_format_data[j].append('')
main_format_data[j].append(output[i][0][3])
main_format_data[j].append(output[i][0][4])
main_format_data[j].append(output[i][0][5])
main_format_data[j].append(output[i][0][6])
main_format_data[j].append(output[i][0][7])
main_format_data[j].append('')
main_format_data[j].append('')
j += 1
# output from morph analyser is stored in file
# main_format.txt
print(main_format_data)
main_format_writer(main_format_data)
ssf_converter.out_temp_file.write('\t\t***Output after Morph Analyser***\n\n')
# this function converts the data from main_format.txt
# to SSF and stored in SSF.txt
ssf_converter.func()
# Input is written in sentinput.txt file in POS_Tagger
# directory in the order word , pos , gender , number,
# person, case ,tam
pos_tagger_input = open(pos_tagger_input_file, 'w', encoding='utf-8')
for j in range(len(main_format_data)):
if main_format_data[j][1] == 'open_bracket_here':
continue
temp = main_format_data[j][3]
for k in range(7, 12):
temp += '\t' + main_format_data[j][k]
temp += '\n'
pos_tagger_input.write(temp)
pos_tagger_input.flush()
pos_tagger_input.close()
# POS tagger module is run here and output is taken in
# in "output" variable
ssf_converter.out_temp_file.write('\t\t***Output after POS Tagger***\n\n')
output = pos_tagger.pos_main()
# print(output)
# output is stored in "main_format_data" from "output"
# variable
i = 0
for j in range(len(main_format_data)):
if main_format_data[j][1] == 'open_bracket_here':
continue
main_format_data[j][4] = output[0][i]
i += 1
# POS tagger output is written in main_format.txt
main_format_writer(main_format_data)
# output is converted in SSF and stored in SSF.txt
ssf_converter.func()
# Input is written in input.txt file in chunking
# directory in the order word , pos , gender , number,
# person, case ,tam
chunker_input = open(chunker_input_file, 'w', encoding='utf-8')
for j in range(len(main_format_data)):
if main_format_data[j][1] == 'open_bracket_here':
continue
temp = main_format_data[j][3]
temp += '\t' + main_format_data[j][4]
for k in range(7, 12):
temp += '\t' + main_format_data[j][k]
temp += '\n'
chunker_input.write(temp)
chunker_input.flush()
chunker_input.close()
# chunker module is run here and output is taken in
# in "output" variable
ssf_converter.out_temp_file.write('\t\t***Output after Chunker***\n\n')
output = chunker.main_chunker()
# print(output)
i = 0
for j in range(len(main_format_data)):
if main_format_data[j][1] == 'open_bracket_here':
continue
main_format_data[j][12] = output[0][i]
i += 1
# Chunker output is written in main_format.txt
main_format_writer(main_format_data)
# output is converted in SSF with the help of second
# type of converter and stored in SSF.txt
ssf_converter2.func()
print(main_format_data)
i = 0
# print(len(main_format_data))
for j in range(len(main_format_data)):
if main_format_data[j][1] == 'open_bracket_here':
# main_format_data[j+1][5] = con1.convert(lexical.convertBhoj(con.convert(main_format_data[j+1][5]), match_list[j//2]))
continue
main_format_data[j][5] = con1.convert(
lexical.convertBhoj(con.convert(main_format_data[j][5]), match_list[(j//2)]))
print(main_format_data[j][5])
i += 1
# print(main_format_data)
ssf_converter.out_temp_file.write('\t\t***Output after Lexical Generator***\n\n')
main_format_writer(main_format_data)
ssf_converter2.func()
print(main_format_data)
# for j in range(len(main_format_data)):
# if main_format_data[j][1] == 'open_bracket_here':
# continue
# if outputM[j//2][0][2] == 'v':
# main_format_data[j][3] = morph_generator.main(main_format_data[j][5], outputM[j//2][0][2] +';'+ outputM[j//2][0][3] +';'+ outputM[j//2][0][4] +';'+ outputM[j//2][0][5] +';'+ outputM[j//2][0][7])
# print(main_format_data)
g = ''
for j in range(len(main_format_data)):
if (j % 2) != 0:
print(main_format_data[j][5])
g = g + main_format_data[j][5] +" "
# print(main_format_data[1][5])
# ssf_converter.out_temp_file.write('Final Output = ' + ' '.join(output) + '\n\n')
print(g)
ssf_converter.out_temp_file.write('\t\t***Target Sentence in Hindi***\n\n')
ssf_converter.out_temp_file.write('Final Output = '+' '+ g)