-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_mor_to_cha.py
More file actions
executable file
·184 lines (150 loc) · 5.82 KB
/
add_mor_to_cha.py
File metadata and controls
executable file
·184 lines (150 loc) · 5.82 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
#!/usr/bin/env python3
"""
Command line interface for adding morphological information to CHAT files.
"""
import sys
import argparse
import logging
import coloredlogs
import os
from tqdm import tqdm
from conversion import morph_enricher
from conversion import pos_mapping
from conversion import punctuation_mapping
DEFAULT_POS_MAPPING = "conversion/data/pos_mapping.csv"
DEFAULT_PUNCTUATION_MAPPING = "conversion/data/punctuation_mapping.csv"
def main(argv):
"""
Main entry point.
"""
coloredlogs.install()
try:
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
"-c", "--chat",
dest="chat_filenames",
help="The CHAT file(s) to enrich.",
metavar="FILE",
required=True,
nargs='*')
parser.add_argument(
"-p", "--pos",
dest="pos_filepath",
help="The Treebank LASSY/Alpino XML file or directory containing the POS information.",
metavar="FILE",
required=True)
parser.add_argument(
"-u", "--punctuation",
dest="punctuation_filename",
help="The file containing the punctuation mapping.")
parser.add_argument(
"-m", "--mapping",
dest="mapping_filename",
help="The mapping file to use.",
metavar="FILE")
parser.add_argument(
"-o", "--output",
dest="output_directory",
help="The output_directory to use.",
metavar="DIRECTORY")
parser.add_argument(
"-f",
"--force",
dest="force",
default=False,
help="Continue on error",
action='store_true')
parser.set_defaults(
mapping_filename=DEFAULT_POS_MAPPING,
punctuation_filename=DEFAULT_PUNCTUATION_MAPPING)
options = parser.parse_args(argv)
write_files = True if options.output_directory else False
enricher = prepare_map(options.mapping_filename,
options.punctuation_filename,
options.pos_filepath,
write_files)
for chat_file in options.chat_filenames:
if write_files:
filename = os.path.basename(chat_file)
writer = FileWriter(options.output_directory, filename)
else:
writer = ConsoleWriter()
try:
perform_map(enricher, chat_file, options.pos_filepath, writer)
except Exception as exception:
sys.stderr.write('Problem in file: ' + chat_file + '\n')
if not options.force:
raise exception
except Exception as exception:
sys.stderr.write(repr(exception) + "\n")
sys.stderr.write("for help use --help\n\n")
raise exception
def prepare_map(mapping_filename, punctuation_filename, pos_filepath, show_progress):
"""
Prepare the mapping and output to console.
"""
mapping = pos_mapping.PosMapping(
punctuation_mapping.PunctuationMapping(punctuation_filename))
mapping.read(mapping_filename)
enricher = morph_enricher.MorphEnricher(mapping)
if os.path.isfile(pos_filepath):
iterator = enricher.read_pos_file(pos_filepath)
single_file = True
else:
iterator = enricher.read_pos_directories(pos_filepath)
single_file = False
if not show_progress:
enumerate(iterator)
else:
with tqdm(iterator, total=1, unit=' sentences', desc=pos_filepath) as progress:
if single_file:
for i in iterator:
progress.total = enricher.sentence_count
progress.update(1)
else:
current_directory = -1
current_file = -1
directory_file_count = 0
sentence_count = 0
total_file_index = 0
total_file_estimate = 1
total_sentence_estimate = 1
for (directory_i, file_i, sentence_i) in iterator:
if directory_i != current_directory:
current_directory = directory_i
directory_file_count += enricher.file_count
average = directory_file_count / (directory_i + 1)
total_file_estimate = (
enricher.directory_count - directory_i) * average
if file_i != current_file:
sentence_count += enricher.sentence_count
current_file = file_i
total_file_index += 1
average = sentence_count / total_file_index
total_sentence_estimate = average * total_file_estimate
progress.total = total_sentence_estimate
progress.update(1)
return enricher
def perform_map(enricher, chat_filename, pos_filepath, writer):
for line in enricher.map(chat_filename, pos_filepath):
writer.writeline(line)
if enricher.has_failures:
logging.error("%s sentence(s) have no tag mapping defined!",
enricher.failed_sentences_count)
logging.error("Missing mapping(s):\n%s",
"\n".join(sorted(enricher.missing_tags)))
class FileWriter:
def __init__(self, directory, filename):
os.makedirs(directory, exist_ok=True)
self.file = open(os.path.join(directory, filename), mode='w')
def writeline(self, line):
self.file.write(line + '\n')
def close(self):
self.file.close()
class ConsoleWriter:
def writeline(self, line):
print(line)
def close(self):
pass
main(sys.argv[1:])