Schema.py
6.06 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
#! /usr/bin/python
# -*- coding: utf-8 -*-
from importer.Position import Position
from syntax.models import SchemaOpinion, Aspect, InherentSie, Negativity, Predicativity, Subentry
class Characteristic:
def __init__(self, name, value):
self._name = name
self._value = value
@classmethod
def fromTree(cls, tree):
name = tree._attrs['name']
if name == 'text_rep':
value = tree._children[0]._content
elif len(tree._children) == 0:
value = None
else:
value = tree._children[0]._attrs['value']
return cls(name, value)
class SchemaCharacteristic:
def __init__(self, opinion, aspect, inherent_sie, negativity, predicativity):
self._opinion = opinion
self._aspect = aspect
self._inherent_sie = inherent_sie
self._negativity = negativity
self._predicativity = predicativity
@classmethod
def fromCharacteristics(cls, characteristics):
data = {
"aspect": None,
"inherent_sie": None,
"negativity": None,
"predicativity": None,
"opinion": None
}
for characteristic in characteristics:
data[characteristic._name] = characteristic._value
return cls(data["opinion"], data["aspect"], data["inherent_sie"], data["negativity"], data["predicativity"])
def analyze_tree(tree):
positions = []
phrases = {}
for position_tree in tree._children[0]._children:
position = Position.fromTree(position_tree)
positions.append(position)
phrases.update(position.getPhraseIds())
return positions, phrases
class Schema:
def __init__(self, id, characteristics, positions, phrases):
self._id = id
self._characteristics = characteristics
self._positions = positions
self._phrases = phrases
self._db_id = None
@classmethod
def fromTree(cls, schema_tree):
characteristics = []
positions = []
phrases = {}
id = schema_tree._attrs['xml:id']
for subtree in schema_tree._children:
if subtree._attrs['name'] != 'positions':
characteristics.append(Characteristic.fromTree(subtree))
else:
positions, phrases = analyze_tree(subtree)
result = cls(id, characteristics, positions, phrases)
for position in positions:
position._schema = result
return result
def getPhraseIds(self):
return self._phrases
def hasInherentSie(self):
for characteristic in self._characteristics:
if characteristic._name == 'inherent_sie':
if characteristic._value == 'true':
return True
else:
return False
return False
def getSubentry(self, entry):
se = SchemaCharacteristic.fromCharacteristics(self._characteristics)
if se._opinion is not None:
opinion = SchemaOpinion.objects.get(key=se._opinion)
else:
opinion = None
if se._aspect is not None:
aspect = Aspect.objects.get(name=se._aspect)
else:
aspect = None
if se._inherent_sie is not None:
inherent_sie = InherentSie.objects.get(name=se._inherent_sie)
else:
inherent_sie = None
if se._negativity is not None:
negativity = Negativity.objects.get(name=se._negativity)
else:
negativity = None
if se._predicativity is not None:
predicativity = Predicativity.objects.get(name=se._predicativity)
else:
predicativity = None
subentry = Subentry.objects.get(entry=entry,
aspect=aspect,
inherent_sie=inherent_sie,
negativity=negativity,
predicativity=predicativity)
return subentry
def store(self, entry, stored_positions):
se = SchemaCharacteristic.fromCharacteristics(self._characteristics)
# @TODO: czy opinia jest w odpowiednim miejscu xml-a?
if se._opinion is not None:
opinion = SchemaOpinion.objects.get(key=se._opinion)
else:
opinion = None
if se._aspect is not None:
aspect = Aspect.objects.get(name=se._aspect)
else:
aspect = None
if se._inherent_sie is not None:
inherent_sie = InherentSie.objects.get(name=se._inherent_sie)
else:
inherent_sie = None
if se._negativity is not None:
negativity = Negativity.objects.get(name=se._negativity)
else:
negativity = None
if se._predicativity is not None:
predicativity = Predicativity.objects.get(name=se._predicativity)
else:
predicativity = None
subentry, _ = Subentry.objects.get_or_create(entry=entry,
aspect=aspect,
inherent_sie=inherent_sie,
negativity=negativity,
predicativity=predicativity)
schema = subentry.schemata.create(opinion=opinion,
phraseologic=False,
positions_count=len(self._positions))
subentry.schemata_count = subentry.schemata_count + 1
subentry.save()
self._db_id = schema.id
schema_positions = set()
for position in self._positions:
position.store(schema, stored_positions, schema_positions, negativity)
def __unicode__(self):
inherent_sie = []
if self.hasInherentSie():
inherent_sie = ['[sie]']
return '[' + ','.join(inherent_sie + [unicode(position) for position in self._positions if position._function is None or position._function._value != 'head']) + ']'