示例#1
0
    def create_forests(self, treelist=None):
        """ This will read sentences to parse. One sentence per line, no periods etc. 

        :param treelist: lines of file like above.
        """
        if not treelist:
            treelist = []

        # Clear this screen before we start creating a mess
        ctrl.disable_undo()  # disable tracking of changes (e.g. undo)
        if self.forest:
            self.forest.retire_from_drawing()
        self.forests = []

        start = 0
        end = 10

        for line in treelist:
            sentence = line.strip()
            if (not sentence) or sentence.startswith('#'):
                continue
            forest = Forest(gloss_text=sentence)
            self.forests.append(forest)
            parser = Parser(grammar.g, -0.0001, forest=forest)
            my_success, my_dnodes = parser.parse(sentence=sentence, start='C')
            print(my_success)
        self.current_index = 0
        self.forest = self.forests[0]
        # allow change tracking (undo) again
        ctrl.resume_undo()
示例#2
0
 def create_derivation(self, forest):
     """ This is always called to initially turn syntax available here and some input into a
     structure. Resulting structures are used to populate a forest.
     :return:
     """
     print('create_derivation: ', self.lexicon)
     self.parser = Parser(self.lexicon, -0.0001, forest=forest)
     # parser doesn't return anything, it pushes derivation steps to forest
     self.parser.parse(sentence=self.sentence, start='C')
     ds = forest.derivation_steps
     ds.derivation_step_index = len(ds.derivation_steps) - 1
     ds.jump_to_derivation_step(ds.derivation_step_index)
示例#3
0
 def derive_from_editable_lexicon(self, sentence, lexdata, semantics=''):
     """ Take edited version of get_editable_lexicon output and try derivation with it.
     """
     grammar = load_grammar(g=lexdata)
     self.lexicon = grammar
     ctrl.disable_undo()
     f = ctrl.forest
     f.clear()
     self.sentence = sentence
     self.parser = Parser(grammar, -0.0001, forest=f)
     # parser doesn't return anything, it pushes derivation steps to forest
     self.parser.parse(sentence=self.sentence, start='C')
     ds = f.derivation_steps
     ds.derivation_step_index = len(ds.derivation_steps) - 1
     ds.jump_to_derivation_step(ds.derivation_step_index)
     f.prepare_for_drawing()
     ctrl.resume_undo()
示例#4
0
 def create_derivation(self, forest):
     """ This is always called to initially turn syntax available here and some input into a
     structure. Resulting structures are used to populate a forest.
     :return:
     """
     print('create_derivation: ', self.lexicon)
     self.parser = Parser(self.lexicon, -0.0001, forest=forest)
     # parser doesn't return anything, it pushes derivation steps to forest
     self.parser.parse(sentence=self.sentence, start='C')
     ds = forest.derivation_steps
     ds.derivation_step_index = len(ds.derivation_steps) - 1
     ds.jump_to_derivation_step(ds.derivation_step_index)
示例#5
0
 def derive_from_editable_lexicon(self, sentence, lexdata, semantics=''):
     """ Take edited version of get_editable_lexicon output and try derivation with it.
     """
     grammar = load_grammar(g=lexdata)
     self.lexicon = grammar
     ctrl.disable_undo()
     f = ctrl.forest
     f.clear()
     self.sentence = sentence
     self.parser = Parser(grammar, -0.0001, forest=f)
     # parser doesn't return anything, it pushes derivation steps to forest
     self.parser.parse(sentence=self.sentence, start='C')
     ds = f.derivation_steps
     ds.derivation_step_index = len(ds.derivation_steps) - 1
     ds.jump_to_derivation_step(ds.derivation_step_index)
     f.prepare_for_drawing()
     ctrl.resume_undo()
示例#6
0
class KSyntaxConnection(SyntaxConnection):
    role = "SyntaxConnection"
    supports_editable_lexicon = True
    supports_secondary_labels = True
    display_modes = ['Derivation tree', 'State tree', 'Bare tree', 'XBar tree']

    def __init__(self):
        SavedObject.__init__(self)
        self.Constituent = classes.get('Constituent')
        self.Feature = classes.get('Feature')
        self.trees = []
        self.constituents = {}
        self.features = {}
        self.lexicon = {}
        self.rules = {}
        self.sentence = ''
        self.parser = None
        self.syntax_display_mode = 2
        for key, value in self.options.items():
            self.rules[key] = value.get('default')

    def get_editable_lexicon(self):
        """ If it is possible to provide editable lexicon, where to get it
        :return:
        """
        return '\n'.join([str(const) for const in self.lexicon])

    def derive_from_editable_lexicon(self, sentence, lexdata, semantics=''):
        """ Take edited version of get_editable_lexicon output and try derivation with it.
        """
        grammar = load_grammar(g=lexdata)
        self.lexicon = grammar
        ctrl.disable_undo()
        f = ctrl.forest
        f.clear()
        self.sentence = sentence
        self.parser = Parser(grammar, -0.0001, forest=f)
        # parser doesn't return anything, it pushes derivation steps to forest
        self.parser.parse(sentence=self.sentence, start='C')
        ds = f.derivation_steps
        ds.derivation_step_index = len(ds.derivation_steps) - 1
        ds.jump_to_derivation_step(ds.derivation_step_index)
        f.prepare_for_drawing()
        ctrl.resume_undo()

    def get_editable_sentence(self):
        """ If the current systems supports parsing, return the current parsed string. User can
        edit it and retry parsing.
        :return:
        """
        return self.sentence

    def create_derivation(self, forest):
        """ This is always called to initially turn syntax available here and some input into a
        structure. Resulting structures are used to populate a forest.
        :return:
        """
        print('create_derivation: ', self.lexicon)
        self.parser = Parser(self.lexicon, -0.0001, forest=forest)
        # parser doesn't return anything, it pushes derivation steps to forest
        self.parser.parse(sentence=self.sentence, start='C')
        ds = forest.derivation_steps
        ds.derivation_step_index = len(ds.derivation_steps) - 1
        ds.jump_to_derivation_step(ds.derivation_step_index)

    def set_display_mode(self, i):
        self.syntax_display_mode = i

    def next_display_mode(self):
        self.syntax_display_mode += 1
        if self.syntax_display_mode == len(self.display_modes):
            self.syntax_display_mode = 0
        return self.display_modes[self.syntax_display_mode]

    def transform_trees_for_display(self, synobjs):
        if self.syntax_display_mode == DERIVATION_TREE:
            # Just derivation trees
            return synobjs
        elif self.syntax_display_mode == STATE_TREE:
            # StateTree(dt)
            res = []
            for synobj in synobjs:
                const = StateTree(synobj).to_constituent()
                res.append(const)
            return res
        elif self.syntax_display_mode == BARE_TREE:
            # BareTree(dt)
            res = []
            for synobj in synobjs:
                const = BareTree(synobj).to_constituent()
                res.append(const)
            return res
        elif self.syntax_display_mode == XBAR_TREE:
            # XBarTree(dt)
            res = []
            for synobj in synobjs:
                const = TracelessXBarTree(synobj).to_constituent()
                res.append(const)
            return res
        else:
            return synobjs
示例#7
0
class KSyntaxConnection(SyntaxConnection):
    role = "SyntaxConnection"
    supports_editable_lexicon = True
    supports_secondary_labels = True
    display_modes = ['Derivation tree', 'State tree', 'Bare tree', 'XBar tree']

    def __init__(self):
        SavedObject.__init__(self)
        self.Constituent = classes.get('Constituent')
        self.Feature = classes.get('Feature')
        self.trees = []
        self.constituents = {}
        self.features = {}
        self.lexicon = {}
        self.rules = {}
        self.sentence = ''
        self.parser = None
        self.syntax_display_mode = 2
        for key, value in self.options.items():
            self.rules[key] = value.get('default')

    def get_editable_lexicon(self):
        """ If it is possible to provide editable lexicon, where to get it
        :return:
        """
        return '\n'.join([str(const) for const in self.lexicon])

    def derive_from_editable_lexicon(self, sentence, lexdata, semantics=''):
        """ Take edited version of get_editable_lexicon output and try derivation with it.
        """
        grammar = load_grammar(g=lexdata)
        self.lexicon = grammar
        ctrl.disable_undo()
        f = ctrl.forest
        f.clear()
        self.sentence = sentence
        self.parser = Parser(grammar, -0.0001, forest=f)
        # parser doesn't return anything, it pushes derivation steps to forest
        self.parser.parse(sentence=self.sentence, start='C')
        ds = f.derivation_steps
        ds.derivation_step_index = len(ds.derivation_steps) - 1
        ds.jump_to_derivation_step(ds.derivation_step_index)
        f.prepare_for_drawing()
        ctrl.resume_undo()

    def get_editable_sentence(self):
        """ If the current systems supports parsing, return the current parsed string. User can
        edit it and retry parsing.
        :return:
        """
        return self.sentence

    def create_derivation(self, forest):
        """ This is always called to initially turn syntax available here and some input into a
        structure. Resulting structures are used to populate a forest.
        :return:
        """
        print('create_derivation: ', self.lexicon)
        self.parser = Parser(self.lexicon, -0.0001, forest=forest)
        # parser doesn't return anything, it pushes derivation steps to forest
        self.parser.parse(sentence=self.sentence, start='C')
        ds = forest.derivation_steps
        ds.derivation_step_index = len(ds.derivation_steps) - 1
        ds.jump_to_derivation_step(ds.derivation_step_index)

    def set_display_mode(self, i):
        self.syntax_display_mode = i

    def next_display_mode(self):
        self.syntax_display_mode += 1
        if self.syntax_display_mode == len(self.display_modes):
            self.syntax_display_mode = 0

    def transform_trees_for_display(self, synobjs):
        if self.syntax_display_mode == 0:
            # Just derivation trees
            return synobjs
        elif self.syntax_display_mode == 1:
            # StateTree(dt)
            res = []
            for synobj in synobjs:
                const = StateTree(synobj).to_constituent()
                res.append(const)
            return res
        elif self.syntax_display_mode == 2:
            # BareTree(dt)
            res = []
            for synobj in synobjs:
                const = BareTree(synobj).to_constituent()
                res.append(const)
            return res
        elif self.syntax_display_mode == 3:
            # XBarTree(dt)
            res = []
            for synobj in synobjs:
                const = TracelessXBarTree(synobj).to_constituent()
                res.append(const)
            return res
        else:
            return synobjs