blob: c5eb8ed972702841d95598d9b83374ed87559387 [file] [log] [blame]
Marc Kupietz86044852025-11-29 10:19:03 +01001import requests, logging, json
2import subprocess, time
3import glob, logging
4import os.path, sys
5from lib.CoNLL_Annotation import read_conll, read_conll_generator
6
7logger = logging.getLogger(__name__)
8
9
10def list_to_file(my_list, out_path):
11 with open(out_path, "w") as out:
12 for item_str in my_list:
13 out.write(f"{item_str}\n")
14
15def counter_to_file(my_counter, out_path):
16 with open(out_path, "w") as out:
17 for item, count in my_counter:
18 item_str = "\t".join(item)
19 out.write(f"{item_str}\t{count}\n")
20
21def dict_to_file(my_dict, out_path):
22 with open(out_path, "w", encoding='utf8') as out:
23 json.dump(my_dict, fp=out, ensure_ascii=False)
24
25
26def file_to_dict(file_path):
27 d = {}
28 with open(file_path) as f:
29 d = json.load(f)
30 return d
31
32
33def write_conll_file(conll_objs, out_path):
34 with open(out_path, "w", encoding='utf8') as out:
35 for obj in conll_objs:
36 for tok in obj.tokens:
37 out.write(tok.get_conllU_line()+"\n")
38 out.write("\n")
39
40def file_generator(file_path):
41 with open(file_path, "r") as data_file:
42 logger.info("Reading instances from lines in file at: %s", file_path)
43 for line in data_file:
44 if not line: continue
45 yield line
46
47
48def get_file_annos_chunk(line_generator, chunk_size, token_class, comment_str="###C:", our_foundry="spacy"):
49 file_has_next = True
50 chunk, n_sents = read_conll(line_generator, chunk_size, token_class, comment_str=comment_str, our_foundry=our_foundry)
51 if n_sents == 0: file_has_next = False
52 sents, gld, meta = [], [], []
53 return chunk, file_has_next
54
55
56def get_file_text_chunk(line_generator, chunk_size, token_class, comment_str="###C:"):
57 """ Same as get_file_annos_chunk but directly get (text, labels) pairs"""
58 file_has_next = True
59 chunk, n_sents = read_conll(line_generator, chunk_size, token_class, comment_str=comment_str)
60 if n_sents == 0: file_has_next = False
61 sents, gld, meta = [], [], []
62 for anno in chunk:
63 if len(anno.metadata) > 0: meta.append("\n".join(anno.metadata))
64 sents.append(anno.get_sentence())
65 gld.append(anno.get_pos_tags())
66 return sents, gld, file_has_next
67
68
69def get_file_chunk(line_generator, chunk_size, token_class, comment_str="###C:"):
70 file_has_next = True
71 chunk, n_sents = read_conll(line_generator, chunk_size, token_class, comment_str=comment_str)
72 if n_sents < chunk_size: file_has_next = False
73 raw_text = ""
74 for anno in chunk:
75 if len(anno.metadata) > 0:
76 raw_text += "\n".join(anno.metadata) + "\n"
77 else:
78 raw_text += "\n"
79 for tok in anno.tokens:
80 raw_text += tok.get_conllU_line() + "\n"
81 raw_text += "\n"
82 return raw_text, file_has_next, n_sents
83
84
85def turku_parse_file(raw_text, filename, chunk_ix):
86 out_file_str = f"{filename}.parsed.{chunk_ix}.conllu"
87 # For each file make a request to obtain the parse back
88 logger.info(f"Sending Request {chunk_ix} to Parser Server...")
89 response = requests.post("http://localhost:7689/", data=raw_text.encode('utf-8'))
90 response_to_file(response.text, out_file_str)
91
92
93
94def response_to_file(response_str, fname):
95 fout = open(fname, "w")
96 fout.write(response_str)
97 fout.close()
98
99
100def expand_file(f, substitute_comment=False):
101 # Expand the .gz file
102 fname = f[:-3]
103 if not os.path.isfile(fname):
104 p = subprocess.call(f"gunzip -c {f} > {fname}", shell=True)
105 if p == 0:
106 logger.info("Successfully uncompressed file")
107 else:
108 logger.info(f"Couldn't expand file {f}")
109 raise Exception
110 else:
111 logger.info(f"File {fname} is already uncompressed. Skipping this step...")
112
113 # Substitute the Commentary Lines on the Expanded file
114 if substitute_comment:
115 fixed_filename = f"{fname}.fixed"
116 p = subprocess.call(f"sed 's/^# /###C: /g' {fname}", shell=True, stdout=open(fixed_filename, "w")) # stdout=subprocess.PIPE
117 if p == 0:
118 logger.info("Successfully fixed comments on file")
119 else:
120 logger.info(f"Something went wrong when substituting commentaries")
121 raise Exception
122 return fixed_filename
123 else:
124 return fname