Position.py
4.1 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
#! /usr/bin/python
# -*- coding: utf-8 -*-
from Phrase import phrase_from_tree
from syntax.models import Position
class Function:
def __init__(self, value):
self._value = value
@classmethod
def fromTree(cls, tree):
value = tree._children[0]._attrs['value']
return cls(value)
# @TODO: dwa rodzaje kontroli
class Control:
def __init__(self, function):
self._function = function
class Position:
def __init__(self, function, controls, phrases, phrase_ids):
self._function = function
self._control = controls
self._phrases = phrases
self._phrase_ids = phrase_ids
@classmethod
def fromTree(cls, tree):
function = None
controls = None
phrases = []
phrase_ids = {}
for subtree in tree._children:
if subtree._attrs['name'] == 'function':
function = Function.fromTree(subtree)
elif subtree._attrs['name'] == 'control':
controls = [Control(c._attrs['value']) for c in subtree._children[0]._children]
elif subtree._attrs['name'] == 'phrases':
for phrase_tree in subtree._children[0]._children:
phrase = phrase_from_tree(phrase_tree)
phrases.append(phrase)
phrase_ids[phrase.getId()] = phrase
result = cls(function, controls, phrases, phrase_ids)
for phrase in phrases:
phrase._position = result
return result
def store(self, schema):
if self._function is not None:
function = SyntacticFunction.objects.get(name=self._function._value)
else:
function = None
if self._control is not None:
control = Control.objects.get(name=self._control[0]._function)
else:
control = None
pred_control = None
position = schema.positions.create(function=function,
control=control,
pred_control=pred_control)
for phrase in self._phrases:
phrase.store(position)
def getPhraseIds(self):
return self._phrase_ids
def __unicode__(self):
if self._function is None:
return '[' + ','.join([phrase.toUnicode(self._function) for phrase in self._phrases]) + ']'
else:
return self._function._value + '([' + ','.join([phrase.toUnicode(self._function) for phrase in self._phrases]) + '])'
def toUnicode(self, function, phrases=None):
pre = ''
post = ''
if self._control is not None:
for control in self._control:
pre += control._function + '('
post += ')'
if phrases is None:
if self._function is None or self._function._value == 'obj':
return pre + '[' + ','.join([phrase.toUnicode(self._function) for phrase in self._phrases]) + ']' + post
elif self._function._value == 'subj':
return pre + 'subj([' + ','.join([phrase.toUnicode(self._function) for phrase in self._phrases]) + '])' + post
elif self._function._value == 'obj':
return pre + 'obj([' + ','.join([phrase.toUnicode(self._function) for phrase in self._phrases]) + '])' + post
elif self._function._value == 'head':
return pre + 'head([' + ','.join([phrase.toUnicode(self._function) for phrase in self._phrases]) + '])' + post
else:
temp = []
for phrase in self._phrases:
if phrase in phrases:
temp.append(phrase.toUnicode(self._function))
if self._function is None:
return pre + '[' + ','.join(temp) + ']' + post
elif self._function._value == 'subj':
return pre + 'subj([' + ','.join(temp) + '])' + post
elif self._function._value == 'obj':
return pre + 'obj([' + ','.join(temp) + '])' + post
elif self._function._value == 'head':
return pre + 'head([' + ','.join(temp) + '])' + post