Schema.py
4.46 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
#! /usr/bin/python
# -*- coding: utf-8 -*-
from Position import Position
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[charactristic._name] = characteristic._value
return cls(name, data.aspect, data.inerent_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
@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 store(self, entry):
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,
defaults={'schemata': None})
schema = subentry.schemata.create(opinion=opinion, phraseologic=False)
for position in positions:
position.store(schema)
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']) + ']'