Exemplo n.º 1
0
    def semantics(self, match):
        entity = HasKeyword(match.entity.tokens)
        target_type = HasKeyword(match.target.lemmas)
        target = HasType(target_type) + IsRelatedTo(entity)
        label = LabelOf(target)

        return label, "enum"
Exemplo n.º 2
0
def example_4():
    class IsPerson(FixedType):
        fixedtype = "foaf:Person"

    a = IsPerson() + HasKeyword(u"John")
    e = IsPerson() + HasKeyword(u"Mary") + IsRelatedTo(a)
    return e
Exemplo n.º 3
0
def handle_keywords(text, split=True):
    """
    Automatic handling of Keywords from a text.
    It runs the sanitize function for keywords on every
    keyword.

    If `split` it's True, it splits the text by white spaces.

    Returns an :class:`quepy.expression.Expression` that represents
    the fact of having the keywords extracted from text.
    """

    assert_valid_encoding(text)

    from quepy.semantics import HasKeyword

    if split:
        keywords = [HasKeyword.sanitize(x) for x in text.split()]
    else:
        keywords = (HasKeyword.sanitize(text),)

    if not keywords:
        raise ValueError(u"Couldn't extract any keyword from '%s'" % text)

    expr = None
    for keyword in keywords:
        if expr is not None:
            expr += HasKeyword(keyword)
        else:
            expr = HasKeyword(keyword)

    return expr
Exemplo n.º 4
0
def handle_keywords(text, split=True):
    """
    Automatic handling of Keywords from a text.
    It runs the sanitize function for keywords on every
    keyword.

    If `split` it's True, it splits the text by white spaces.

    Returns an :class:`quepy.expression.Expression` that represents
    the fact of having the keywords extracted from text.
    """

    assert_valid_encoding(text)

    from quepy.semantics import HasKeyword

    if split:
        keywords = [HasKeyword.sanitize(x) for x in text.split()]
    else:
        keywords = (HasKeyword.sanitize(text), )

    if not keywords:
        raise ValueError(u"Couldn't extract any keyword from '%s'" % text)

    expr = None
    for keyword in keywords:
        if expr is not None:
            expr += HasKeyword(keyword)
        else:
            expr = HasKeyword(keyword)

    return expr
Exemplo n.º 5
0
    def semantics(self, match):
        expr = None

        for word in match.words:
            if expr is not None:
                expr += HasKeyword(word.token)
            else:
                expr = HasKeyword(word.token)

        return expr
Exemplo n.º 6
0
    def test_has_keyword(self):

        HasKeyword.relation = u"uranium:keyword"
        keywordinstance = HasKeyword(u"soplete")

        head = keywordinstance.get_head()
        edges = list(keywordinstance.iter_edges(head))
        self.assertEqual(len(edges), 1)
        self.assertIsInstance(edges[0][0], unicode)
        self.assertEqual(edges[0][0], u"uranium:keyword")
        self.assertIsInstance(edges[0][1], unicode)
        self.assertEqual(edges[0][1], u'soplete')

        # With language
        HasKeyword.language = "en"
        keywordinstance = HasKeyword("soplete")

        head = keywordinstance.get_head()
        edges = list(keywordinstance.iter_edges(head))
        self.assertEqual(len(edges), 1)
        self.assertIsInstance(edges[0][1], unicode)
        self.assertEqual(edges[0][1], u'"soplete"@en')

        # With sanitize
        HasKeyword.sanitize = staticmethod(lambda x: x.upper())
        keywordinstance = HasKeyword(u"soplete")

        head = keywordinstance.get_head()
        edges = list(keywordinstance.iter_edges(head))
        self.assertEqual(len(edges), 1)
        self.assertIsInstance(edges[0][1], unicode)
        self.assertEqual(edges[0][1], u'"SOPLETE"@en')
Exemplo n.º 7
0
def example_6():
    class HeadOf(FixedRelation):
        relation = ""
        reverse=True

    # The cat ate a fish

    the  = HasKeyword(u"The")
    cat  = HasKeyword(u"cat")  + HeadOf(the)
    a    = HasKeyword(u"a")
    fish = HasKeyword(u"fish") + HeadOf(a)
    ate  = HasKeyword(u"ate")  + HeadOf(cat) + HeadOf(fish)
    return ate
Exemplo n.º 8
0
def handle_noun_phrase(words, flatten=False, ignore_jj=False):
    # FIXME: doc

    from quepy.semantics import HasKeyword, IsRelatedTo
    if not words:
        raise BadNounLike(u"{0!r} is not noun-phrase-like".format(words))

    head = handle_nounlike(words.pop())
    xs = [head]
    while words:
        word = words.pop()
        if word.pos == u"JJ" and not ignore_jj:
            head += HasKeyword(word.lemma)
        else:
            head = handle_nounlike(word)
            xs.append(head)
    if flatten:
        head = xs.pop()
        for x in xs:
            head += x
    else:
        head = xs.pop()
        while xs:
            head = xs.pop() + IsRelatedTo(head)

    return head
Exemplo n.º 9
0
def example_5():
    class IsPerson(FixedType):
        fixedtype = "foaf:Person"

    class BirthDateOf(FixedRelation):
        relation = "dbpprop:birthDate"
        reverse = True

    john = IsPerson() + HasKeyword(u"John")
    e    = BirthDateOf(john)
    return e
Exemplo n.º 10
0
def handle_nounlike(word):
    """
    Handles things that might be a noun or a special
    thing like a Handler.

    Returns an :class:`quepy.expression.Expression`.
    """

    from quepy import handlers
    from quepy.semantics import HasKeyword

    handler = handlers.get_handler(word)

    if handler:
        return handler(word)
    elif word.pos in (u"NN", u"NP", u"NNP", u"NNS"):
        return HasKeyword(word.lemma)
    else:
        raise UnhandledWord(u"Couldn't handle word {0!r}".format(word))
Exemplo n.º 11
0
 def semantics(self, match):
     name = match.words.tokens
     return IsDirector() + HasKeyword(name)
Exemplo n.º 12
0
 def semantics(self, match):
     name = match.words.tokens
     return IsPerson() + HasKeyword(name)
Exemplo n.º 13
0
    def semantics(self, match):
        thing = HasKeyword(match.thing.tokens)
        location = LocationOf(thing)
        location_name = LabelOf(location)

        return location_name, "enum"
Exemplo n.º 14
0
    def semantics(self, match):
        place = HasKeyword(match.place.lemmas.title()) + IsPlace()
        utc_offset = UTCof(place)

        return utc_offset, "time"
Exemplo n.º 15
0
def example_0():
    e = HasKeyword(u"John")
    return e
Exemplo n.º 16
0
 def semantics(self, match):
     return HasKeyword(match.words.tokens)
Exemplo n.º 17
0
 def semantics(self, match):
     return HasKeyword(match.words[0].token), "<user data>"
Exemplo n.º 18
0
    def test_has_keyword(self):

        HasKeyword.relation = u"uranium:keyword"
        keywordinstance = HasKeyword(u"soplete")

        head = keywordinstance.get_head()
        edges = list(keywordinstance.iter_edges(head))
        self.assertEqual(len(edges), 1)
        self.assertIsInstance(edges[0][0], unicode)
        self.assertEqual(edges[0][0], u"uranium:keyword")
        self.assertIsInstance(edges[0][1], unicode)
        self.assertEqual(edges[0][1], u'soplete')

        # With language
        HasKeyword.language = "en"
        keywordinstance = HasKeyword("soplete")

        head = keywordinstance.get_head()
        edges = list(keywordinstance.iter_edges(head))
        self.assertEqual(len(edges), 1)
        self.assertIsInstance(edges[0][1], unicode)
        self.assertEqual(edges[0][1], u'"soplete"@en')

        # With sanitize
        HasKeyword.sanitize = staticmethod(lambda x: x.upper())
        keywordinstance = HasKeyword(u"soplete")

        head = keywordinstance.get_head()
        edges = list(keywordinstance.iter_edges(head))
        self.assertEqual(len(edges), 1)
        self.assertIsInstance(edges[0][1], unicode)
        self.assertEqual(edges[0][1], u'"SOPLETE"@en')
Exemplo n.º 19
0
 def semantics(self, match):
     name = match.words.tokens.title()
     return IsBand() + HasKeyword(name)
Exemplo n.º 20
0
def example_2():
    class IsPerson(FixedType):
        fixedtype = "foaf:Person"

    e = IsPerson() + HasKeyword(u"John")
    return e