Syntax.py 973 Bytes
#! /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)
    
    def __unicode__(self):
        return '[' + ','.join([unicode(schema) for schema in self._schemata]) + ']'