-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreate_output_file.py
More file actions
191 lines (166 loc) · 7.49 KB
/
Create_output_file.py
File metadata and controls
191 lines (166 loc) · 7.49 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
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
# import os
import csv
import datetime
import json
from rdflib import Graph, URIRef, RDFS
# Namespace, Literal, RDF, BNode
# from tkinter import filedialog
import remi.gui as gui
from Bootstrapping import ini_out_path
# from Expr_Table_Def import *
from Expr_Table_Def import lang_uid_col, lang_name_col, comm_uid_col, comm_name_col,\
intent_uid_col, intent_name_col, idea_uid_col, lh_uid_col, lh_name_col,\
rel_type_uid_col, rel_type_name_col, phrase_type_uid_col, rh_role_uid_col, rh_role_name_col,\
rh_uid_col, rh_name_col, full_def_col, uom_uid_col, uom_name_col, status_col,\
expr_col_ids, header3, default_row
# lh_role_uid_col, lh_role_name_col, part_def_col
from utils import open_file
def Create_gellish_expression(lang_comm, idea_uid, intent_uid_name,
lh_uid_name, rel_uid_phrase_type,
rh_role_uid_name, rh_uid_name, uom_uid_name, full_description):
''' Create a Gellish expression from default_row in Expr_Table_Def'''
gellish_expr = default_row[:]
gellish_expr[lang_uid_col] = lang_comm[0]
gellish_expr[lang_name_col] = lang_comm[1]
gellish_expr[comm_uid_col] = lang_comm[2]
gellish_expr[comm_name_col] = lang_comm[3]
gellish_expr[intent_uid_col] = intent_uid_name[0]
gellish_expr[intent_name_col] = intent_uid_name[1]
gellish_expr[idea_uid_col] = idea_uid
gellish_expr[lh_uid_col] = lh_uid_name[0]
gellish_expr[lh_name_col] = lh_uid_name[1]
gellish_expr[rel_type_uid_col] = rel_uid_phrase_type[0]
gellish_expr[rel_type_name_col] = rel_uid_phrase_type[1]
gellish_expr[phrase_type_uid_col] = rel_uid_phrase_type[2]
gellish_expr[rh_role_uid_col] = rh_role_uid_name[0]
gellish_expr[rh_role_name_col] = rh_role_uid_name[1]
gellish_expr[rh_uid_col] = rh_uid_name[0]
gellish_expr[rh_name_col] = rh_uid_name[1]
gellish_expr[full_def_col] = full_description
gellish_expr[uom_uid_col] = uom_uid_name[0]
gellish_expr[uom_name_col] = uom_uid_name[1]
gellish_expr[status_col] = 'accepted'
return gellish_expr
def Open_output_file(expressions, subject_name, lang_name, serialization):
""" Open a file for saving expressions in some format
such as the CSV in Gellish Expression Format:
Serialization is either 'csv', 'xml', 'n3' or 'json'.
"""
date = datetime.date.today()
# Create header line 1 and an initial file name
if lang_name == 'Nederlands':
header1 = ['Gellish', 'Nederlands', 'Versie', '9.0', date, 'Resultaten',
'over ' + subject_name]
res = 'Resultaten-'
else:
header1 = ['Gellish', 'English', 'Version', '9.0', date, 'Results',
'about ' + subject_name]
res = 'Query_results-'
ini_file_name = ''
if serialization == 'csv':
ini_file_name = res + subject_name + '.csv.csv'
if serialization == 'xml':
ini_file_name = res + subject_name + '.xml.xml'
if serialization == 'n3':
ini_file_name = res + subject_name + '.n3.n3'
if serialization == 'json':
ini_file_name = res + subject_name + '.json.json'
# header2 = expr_col_ids # from Bootstrapping
# Select file name and directory
# Ini_out_path from Bootstrapping
## title = serialization + ' files'
## extension = '*.' + serialization
## output_file = filedialog.asksaveasfilename(filetypes=((title, extension),
## ("All files", "*.*")),
## title="Enter a file name",
## initialdir=ini_out_path,
## initialfile=ini_file_name)
output_file_name = ini_file_name
if output_file_name == '':
output_file = 'Results.' + serialization
if lang_name == 'Nederlands':
print('***De filenaam voor opslaan is blanco of the file selectie is gecancelled. '
'De file met naam ' + output_file + ' is niet opgeslagen')
else:
print('***File name for saving is blank or file selection is cancelled. '
'The file with name ' + output_file + 'is not saved')
else:
Save_expressions_in_file(expressions, output_file_name, header1, serialization)
def fileupload_on_success(widget, filename):
print('File upload success: ' + filename)
def fileupload_on_failed(widget, filename):
print('File upload failed: ' + filename)
def Save_expressions_in_file(expressions, output_file, header1, serialization):
'''Write expressions to an output file in an CSV or RDF serialization'''
if serialization == 'csv':
# Save the result_expr expressions in a CSV file, preceeded by three header lines.
try:
f = open(output_file, mode='a', newline='', encoding='utf-8')
file_writer = csv.writer(f, dialect='excel', delimiter=';')
# Write header rows and expressions
file_writer.writerow(header1)
file_writer.writerow(expr_col_ids)
file_writer.writerow(header3)
for expression in expressions:
file_writer.writerow(expression)
f.close()
# Open the written file in a CSV viewer (e.g. Excel)
# open_file(output_file)
except PermissionError:
print('File {} cannot be opened. Probably already in use'.format(output_file))
return
elif serialization in ['xml', 'n3']:
g1 = Graph()
uri = "http://www.formalenglish.net/dictionary"
# gel = Namespace("http://www.formalenglish.net/dictionary")
g1.bind('gel', "http://www.formalenglish.net/dictionary")
# rdfs = {1146: 'subClassOf'}
for expr in expressions:
rel_name = str(expr[3]).replace(" ", "_")
if expr[2] == 1146:
rel = RDFS.subClassOf
else:
rel = URIRef(uri + rel_name)
lh = URIRef(uri + str(expr[1]))
rh = URIRef(uri + str(expr[5]))
g1.add((lh, rel, rh))
if serialization == 'n3':
s = g1.serialize(format='n3')
f = open(output_file, 'w')
f.write(str(s))
elif serialization == 'xml':
s = g1.serialize(format='xml')
f = open(output_file, 'w')
f.write(str(s))
else:
f = open(output_file, 'w', encoding='utf-8')
json.dump(expressions, f)
f.close()
print('Saved file: {}'.format(output_file))
# Open written file in a viewer
open_file(output_file)
output_file = gui.FileUploader('./', width=200, height=30, margin='10px')
output_file.onsuccess.connect(fileupload_on_success)
output_file.onfailed.connect(fileupload_on_failed)
def Convert_numeric_to_integer(numeric_text):
''' Convert a numeric string into integer value removing dots(.), commas(,) and spaces( )
If string is not numeric, return string and integer = False.
'''
integer = True
try:
int_val = int(numeric_text)
except ValueError:
commas_removed = numeric_text.replace(',', '')
dots_removed = commas_removed.replace('.', '')
spaces_removed = dots_removed.replace(' ', '')
if spaces_removed.isdecimal():
return int(spaces_removed), integer
else:
integer = False
return numeric_text, integer
return int_val, integer
def Message(GUI_lang_index, mess_text_EN, mess_text_NL):
if GUI_lang_index == 1:
print(mess_text_NL)
else:
print(mess_text_EN)