Syntax.py
1.02 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
#! /usr/bin/python
# -*- coding: utf-8 -*-
from importer.Schema import Schema
def analyze_tree(tree):
phrases = {}
schemata = []
for schema_tree in tree._children[0]._children[0]._children:
schema = Schema.fromTree(schema_tree)
schemata.append(schema)
phrases.update(schema.getPhraseIds())
return phrases, schemata
class Syntax:
def __init__(self, phrases, schemata):
self._phrases = phrases
self._schemata = schemata
@classmethod
def fromTree(cls, syntax_tree):
phrases, schemata = analyze_tree(syntax_tree)
return cls(phrases, schemata)
def getPhraseIds(self):
return self._phrases
def store(self, entry, stored_positions):
for schema in self._schemata:
schema.store(entry, stored_positions)
entry.schemata_count = len(self._schemata)
entry.save()
def __unicode__(self):
return '[' + ','.join([unicode(schema) for schema in self._schemata]) + ']'