-
Notifications
You must be signed in to change notification settings - Fork 0
/
unknown_entry_encoder.py
117 lines (96 loc) · 4.74 KB
/
unknown_entry_encoder.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
from copy import copy
from string import punctuation
import node_factory as nf
from utils import has_more_cyrillic_than_latin, is_empty_string, SafeString
def deal_with_unknown_entry(entry):
senses = unknown_entry_type_find_senses_start(entry)
if senses:
unknown_entry_partially_encode(entry)
return senses
def unknown_entry_type_find_senses_start(entry):
contents = entry.contents
for i in range(len(contents)):
if contents[i].text.strip().startswith('1.'):
if i > 0:
entry.encoded_parts['morph_part'] = contents[:i]
entry.entry_type = 'unknown but clear senses start'
return contents[i:]
elif contents[i].get('rend' ) == 'bold' and contents[i].text.strip().endswith('1.'):
number_node = copy(contents[i])
number_node.text = '1.'
if contents[i].text.endswith(' '):
number_node.text += ' '
contents[i].text = contents[i].text[:-1]
contents[i].text = contents[i].text.replace('1.', '')
entry.encoded_parts['morph_part'] = contents[:i+1]
entry.entry_type = 'unknown but clear senses start'
res = []
res.append(number_node)
res.extend(contents[i+1:])
return res
elif contents[i].get('rend' ) == 'bold' and contents[i].text.endswith('1') and contents[i+1].text.startswith('.'):
number_node = copy(contents[i])
number_node.text = '1.'
contents[i].text = contents[i].text[:-1]
contents[i+1].text = contents[i+1].text[1:]
entry.encoded_parts['morph_part'] = contents[:i+1]
entry.entry_type = 'unknown but clear senses start'
res = []
res.append(number_node)
res.extend(contents[i+1:])
return res
for i in range(len(contents)):
if has_more_cyrillic_than_latin(SafeString(contents[i].text).strip().split(' ')[0]) and not contents[i].get('rend'):
if i > 0:
entry.encoded_parts['morph_part'] = contents[:i]
entry.entry_type = 'unknown but clear senses start'
return contents[i:]
def unknown_entry_partially_encode(entry):
old_morph_part = copy(entry.encoded_parts['morph_part'])
entry.encoded_parts['morph_part'] = []
counter = 0
while old_morph_part:
content_node = [old_morph_part[0]]
node_content = SafeString(old_morph_part[0].text)
if counter == 0:
content_node = unknown_initial_xml(content_node[0].text)
elif node_content.strip().startswith('(') and node_content.strip().endswith(')'):
content_node = [nf.create_extra_morph(node_content)]
elif node_content.strip() in (punctuation + '–'):
content_node = [nf.create_pc_node(node_content)]
elif old_morph_part[0].get('rend') == "italic":
if node_content.strip() in ('m', 'f', 'n'):
content_node = [nf.create_gram_grp(node_content)]
elif len(entry.encoded_parts['morph_part']) == 1 and len(old_morph_part) >= 2 and old_morph_part[1].get('rend' ) == 'bold' and \
node_content.strip() == 'и' and entry.encoded_parts['morph_part'][0].tag == nf.get_ns('form'):
content_node = []
entry.encoded_parts['morph_part'][0].append(old_morph_part[0])
entry.encoded_parts['morph_part'][0].append(nf.create_orth_node(SafeString(old_morph_part[1].text)))
old_morph_part.pop(0)
else:
content_node = nf.create_usg_node(node_content)
elif node_content.strip() in ('1', '2', '3', '4') and (len(old_morph_part) == 1 or old_morph_part[1].text.strip() != '.'):
content_node = [nf.create_gram_grp(node_content, 'iType')]
[entry.encoded_parts['morph_part'].append(x) for x in content_node]
old_morph_part.pop(0)
counter += 1
def unknown_initial_xml(content0):
res = []
content0_split = [x for x in content0.split(', ') if not is_empty_string(x)]
for i in range(len(content0_split)):
if i == 0:
form_lemma = nf.create_form_lemma_node(content0_split[i])
res.append(form_lemma)
if len(content0_split) > 1:
pc = nf.create_pc_node(', ')
res.append(pc)
# TODO: Extra comma / missing comma
elif i == len(content0_split)-1:
form_inflected = nf.create_form_inflected_node(content0_split[i])
res.append(form_inflected)
else:
form_inflected = nf.create_form_inflected_node(content0_split[i])
pc = nf.create_pc_node(', ')
res.append(form_inflected)
res.append(pc)
return res