Meanings.py
3.01 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
#! /usr/bin/python
# -*- coding: utf-8 -*-
from meanings.models import LexicalUnit, Synset
from connections.models import POS
class Meaning:
def __init__(self, id, name, variant, luid, gloss, sid):
self._id = id
self._name = name
self._variant = variant
self._luid = luid
self._gloss = gloss
self._sid = sid
@classmethod
def fromTree(cls, meaning_tree):
id = meaning_tree._attrs['xml:id']
name = meaning_tree._children[0]._children[0]._content
variant = meaning_tree._children[1]._children[0]._content
luid = int(meaning_tree._children[2]._children[0]._attrs['value'])
if len(meaning_tree._children) == 4:
gloss = ""
sid = int(meaning_tree._children[3]._children[0]._attrs['value'])
elif len(meaning_tree._children) == 5:
gloss = meaning_tree._children[3]._children[0]._content
sid = int(meaning_tree._children[4]._children[0]._attrs['value'])
else:
raise UnknownError
return cls(id, name, variant, luid, gloss, sid)
def getId(self):
return self._id
def save(self, pos):
synset = Synset(id=self._sid)
synset.save()
unit = LexicalUnit(entry=None,
base=self._name,
sense=self._variant,
pos=pos,
synset=synset,
text_rep=str(self))
unit.save()
def store(self, entry, frame):
units = LexicalUnit.objects.filter(base=self._name, sense=self._variant)
if len(units) == 0:
synset = Synset(id=self._sid)
synset.save()
unit = LexicalUnit(entry=entry,
base=self._name,
sense=self._variant,
pos=entry.pos.tag,
synset=synset,
text_rep=str(self))
unit.save()
else:
unit = units[0]
unit.entry = entry
unit.save()
frame.lexical_units.add(unit)
def __unicode__(self):
return '\'' + self._name + '\'-' + self._variant
def __str__(self):
return self._name + '-' + self._variant
class Meanings:
def __init__(self, meanings):
self._meanings = meanings
@classmethod
def fromTree(cls, meanings_tree):
meanings = {}
for subtree in meanings_tree._children[0]._children[0]._children:
meaning = Meaning.fromTree(subtree)
meanings[meaning.getId()] = meaning
return cls(meanings)
def getMeanings(self):
return self._meanings
def locate(self, meaning_id):
if meanings_id in self._meanings:
return self._meanings[meaning_id]
else:
return None
def __unicode__(self):
return '[' + ','.join([unicode(meaning) for meaning in self._meanings.values()]) + ']'