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"
def example_4(): class IsPerson(FixedType): fixedtype = "foaf:Person" a = IsPerson() + HasKeyword(u"John") e = IsPerson() + HasKeyword(u"Mary") + IsRelatedTo(a) return e
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
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
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
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')
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
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
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
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))
def semantics(self, match): name = match.words.tokens return IsDirector() + HasKeyword(name)
def semantics(self, match): name = match.words.tokens return IsPerson() + HasKeyword(name)
def semantics(self, match): thing = HasKeyword(match.thing.tokens) location = LocationOf(thing) location_name = LabelOf(location) return location_name, "enum"
def semantics(self, match): place = HasKeyword(match.place.lemmas.title()) + IsPlace() utc_offset = UTCof(place) return utc_offset, "time"
def example_0(): e = HasKeyword(u"John") return e
def semantics(self, match): return HasKeyword(match.words.tokens)
def semantics(self, match): return HasKeyword(match.words[0].token), "<user data>"
def semantics(self, match): name = match.words.tokens.title() return IsBand() + HasKeyword(name)
def example_2(): class IsPerson(FixedType): fixedtype = "foaf:Person" e = IsPerson() + HasKeyword(u"John") return e