Spaces:
Running
Running
File size: 1,521 Bytes
5ac8480 | 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 |
from pathlib import Path
import re
from typing import List, Union
from .classes import Token
def print_to_conll(string_lines):
for line in string_lines:
print(line)
def save_to_file(string_lines: List[str], file_path: Path):
with open(file_path, 'w') as f:
[f.write(f'{line}\n') for line in string_lines]
def text_tuples_to_string(
text_tuples: List[List[tuple]],
file_type,
annotations: Union[List[str], None]=None,
sentences: Union[List[str], None]=None
):
if sentences is not None and file_type != 'conll':
# filter out empty lines
sentences = list(filter(lambda x : len(re.sub(r"\s+", "", x, flags=re.UNICODE)) > 0, sentences))
# get treeTokens
tokens = [[tup[1] for tup in sent] for sent in text_tuples]
string_lines: List[str] = []
for i, sentence_tuples in enumerate(text_tuples):
if file_type == 'conll': # dont add comments to preexisting conll files
pass
elif sentences:
string_lines.extend(
(
f"# text = {sentences[i].strip()}",
f"# treeTokens = {' '.join(tokens[i])}",
)
)
elif annotations:
string_lines.append(annotations[i])
for token_tuple in sentence_tuples:
token = Token(*token_tuple)
string_lines.append(token.to_conll_row())
string_lines.append('') # add empty line between trees
return string_lines |