예제 #1
0
파일: test_store.py 프로젝트: szroland/nlu
    def test_concept_iterator_includes_parent_concepts(self):
        parent = Store()
        child = Store(parent)

        parent.add_concept(Concept.word("p"))
        child.add_concept(Concept.word("c"))

        l = set(map(lambda c: c.name, child.concepts()))
        self.assertEqual(l, {"p", "c"})
예제 #2
0
파일: test_store.py 프로젝트: szroland/nlu
    def test_uses_concepts_in_parent(self):
        parent = Store()
        child = Store(parent)

        raw = Concept(
            None, Relation.Class,
            [Concept.word('c1'), Concept.word('c2')])
        in_parent = parent.integrate(raw)
        in_child = child.integrate(raw)

        self.assertEqual(id(in_parent), id(in_child))
예제 #3
0
    def subj(self, sentence) -> Iterable[Concept]:
        s = self.dep(sentence, 'nsubj')
        while s is not None:
            p = self.dep(s, 'poss')
            if p is None:
                yield Concept.word(self.name(s))
            else:
                yield Concept(
                    None, Relation.Part,
                    [Concept.word(self.name(p)),
                     Concept.word(self.name(s))])

            s = self.dep(s, 'conj')
예제 #4
0
파일: parser.py 프로젝트: szroland/nlu
def parse(expression: str, store: Store=None, label: str=None, probability: float=1.0):
    if is_word(expression):
        word = None
        if store is not None:
            word = store.get_word(expression)
        if word is not None:
            return next(word.meanings())

        if label is None:
            label = expression

        return Concept.word(label)
    else:
        return parse_compound_concept(expression, store, label, probability=probability)
예제 #5
0
    def parse_simple(self, root) -> Iterable:
        if root.lemma_ == 'be':
            attr = self.dep(root, 'attr')
            if attr and (attr.pos_ == 'NOUN' or attr.pos_ == 'PROPN'):
                rel = Relation.Class
                prep = self.dep(root, 'prep')
                if prep is not None and prep.lemma_ == 'like':
                    rel = Relation.Feature
                attr_concept = Concept.word(self.name(attr))
                poss = self.dep(attr, "poss")
                if poss is not None:
                    attr_concept = Concept(
                        None, Relation.Part,
                        [Concept.word(self.name(poss)), attr_concept])
                    rel = Relation.Identical
                for subj in self.subj(root):  # type: Concept
                    if subj.relation == Relation.Part:
                        yield Concept(None, Relation.Identical,
                                      [attr_concept, subj])
                    else:
                        yield Concept(None, rel, [subj, attr_concept])
            acomp = self.dep(root, 'acomp')
            if acomp and acomp.pos_ == 'ADJ':
                for subj in self.subj(root):
                    yield Concept(None, Relation.Feature,
                                  [subj, Concept.word(self.name(acomp))])
        elif root.pos_ == 'VERB':
            for subj in self.subj(root):
                rel = Relation.Feature
                aux = self.dep(root, 'aux')
                mark = self.dep(root, 'mark')
                if aux is not None and aux.lemma_ == 'be':  # continuous
                    rel = Relation.Action
                if aux is not None and aux.lemma_ == 'do' and aux.tag_ == 'VBD':  # past tense with aux do
                    rel = Relation.Action
                if root.tag_ == 'VBD':  # past tense
                    rel = Relation.Action
                if mark is not None and mark.lemma_ == 'if':  # conditional
                    rel = Relation.Action

                concept = Concept(None, rel,
                                  [subj, Concept.word(self.name(root))])

                features = []
                npadvmod = self.dep(root, 'npadvmod')
                if npadvmod is not None:
                    features.append(
                        Concept(None, Relation.Time,
                                [Concept.word(self.name(npadvmod))]))

                advmod = self.dep(root, 'advmod')
                if advmod is not None:
                    if advmod.lemma_ == 'where':
                        features.append(
                            Concept(None, Relation.Relative,
                                    [Concept.word('?'),
                                     Concept.word('?')]))
                    if advmod.lemma_ == 'when':
                        features.append(
                            Concept(None, Relation.Time, [Concept.word('?')]))

                for prep in self.deps(root, 'prep'):
                    while prep is not None:
                        obj = self.dep(prep, 'pobj')
                        obj_name = '?'
                        if obj is not None:
                            obj_name = self.name(obj)

                        features.append(
                            Concept(None, Relation.Relative, [
                                Concept.word(self.name(prep)),
                                Concept.word(obj_name)
                            ]))
                        prep = self.dep(prep, 'prep')

                if len(features) > 0:
                    concept = Concept(None, Relation.Feature,
                                      [concept] + features)

                yield concept