def __init__(self, blackboard):
     super(
         ApostropheExpert,
         self).__init__(
         blackboard,
         "Apostrophe Expert",
         2)
     self.apostrophes = [Word("O"), Word("Oh")]
Пример #2
0
    def generate_phrase(self):
        super(MetaphoreExpert, self).generate_phrase()
        phrase = choice(self.productions)
        try:
            noun = choice([
                n for n in self.blackboard.pool.nouns
                if len(self.blackboard.pool.epithets[n]) > 0
            ])
            adj = choice(self.blackboard.pool.epithets[noun])
        except IndexError:
            return

        replace_words = {
            'adj': adj,
            'n': noun,
            'be': self.conjugate("be"),
            'person': self.persons[self.tense][0]
        }
        for pos in replace_words:
            while pos in phrase:
                # try:
                phrase = self.replace_pos(pos, replace_words[pos], phrase)
                # except:
                #     return
        for w in phrase:
            if not isinstance(w, Word):
                phrase[phrase.index(w)] = Word(w)
        return phrase
Пример #3
0
 def generate_phrase(self):
     """Parse string phrase to list of words with tags """
     phrase = self.blackboard.pool.title
     tokens = nltk.word_tokenize(phrase)
     pos_phrase = nltk.pos_tag(tokens)
     new_phrase = [Word(w[0], w[1]) for w in pos_phrase]
     return new_phrase
 def generate_phrase(self):
     try:
         phrase = deepcopy(choice(
             self.blackboard.pool.phrases_dict))[0].words
     except IndexError:
         return
     if phrase[-1].name not in ["!", "?"]:
         phrase.append(Word("!"))
     return phrase
Пример #5
0
 def __init__(self, words=None):
     self.words = []
     if words is not None:
         if isinstance(words[0], Word):  # if list of Word
             self.words = copy.deepcopy(words)
         else:  # if list of strings
             for w in words:
                 self.words.append(Word(w))
     else:
         self.words = []
Пример #6
0
 def find_emotional_words(self):
     # try:
     text_sentiment = self.calculate_phrase_sentiment(
         self.blackboard.pool.sentences)
     emotion = self.get_emotion(text_sentiment)
     self.blackboard.pool.emotion = emotion
     affect_knowledge = [
         Word(e) for e in self.generate_affect_words(emotion)
     ]
     return affect_knowledge
Пример #7
0
    def add_keywords(self, phrase):

        sent = en.Sentence(en.parse(phrase))
        nouns = search('NN', sent)
        self.blackboard.pool.nouns.update(
            set(Word(en.singularize(n[0].string)) for n in nouns))
        adjs = search('JJ', sent)
        self.blackboard.pool.adjectives.update(
            set(Word(en.lemma(a[0].string)) for a in adjs))

        try:
            nps = search('NP', sent)
            for np in nps:
                self.blackboard.pool.epithets.update({
                    Word(en.singularize(w.string), "NN"):
                    [Word(jj.string, "JJ") for jj in np if "JJ" in jj.tag]
                    for w in np if "NN" in w.tag
                })
        except IndexError:
            pass
 def generate_phrase(self):
     noun = choice(list(self.blackboard.pool.nouns))
     apostrophe = choice(self.apostrophes)
     try:
         hypernyms = self.blackboard.pool.hypernyms[noun]
         hypernym = choice(hypernyms)
         epithets = list(self.blackboard.pool.epithets[noun])
         epithet = choice(epithets)
     except:
         return
     verse = [apostrophe, noun, Word("the"), epithet, hypernym]
     return verse
Пример #9
0
    def _find_antonyms(self, word):
        antonyms = []
        try:
            antonyms = set(
                Word(a.name(), word.pos)
                for a in wn.synsets(word.name)[0].lemmas()[0].antonyms())
        except IndexError as e:
            pass

        if word.pos.startswith("N"):
            self.blackboard.pool.nouns |= set(antonyms)
        # elif word.pos.startswith("V"):
        #     pool.verbs |= antonyms
        elif word.pos.startswith("JJ"):
            self.blackboard.pool.adjectives |= antonyms
        self.blackboard.pool.antonyms[word.name] += [a.name for a in antonyms]
        return antonyms
Пример #10
0
    def _find_synonyms(self, word):
        synonyms = []
        try:
            synonyms = set(
                Word(w, word.pos)
                for w in wn.synsets(word.name)[0].lemma_names())

        except IndexError as e:
            pass

        if word.pos.startswith("N"):
            self.blackboard.pool.nouns |= set(synonyms)
        # elif word.pos.startswith("V"):
        #     pool.verbs |= synonyms
        elif word.pos.startswith("JJ"):
            self.blackboard.pool.adjectives |= synonyms
        self.blackboard.pool.synonyms[word.name] += [s.name for s in synonyms]
        return synonyms
Пример #11
0
    def _find_hyponym(self, word):
        hyponyms = []
        try:
            hyponyms = set(
                Word(w.name().split('.')[0], word.pos)
                for w in wn.synsets(word.name)[0].hyponyms())

        except IndexError as e:
            pass

        if word.pos.startswith("N"):
            self.blackboard.pool.nouns |= set(hyponyms)
        # elif word.pos.startswith("V"):
        #     pool.verbs |= hyponyms
        elif word.pos.startswith("JJ"):
            self.blackboard.pool.adjectives |= hyponyms
        self.blackboard.pool.hyponyms[word.name] += [h.name for h in hyponyms]
        return hyponyms
Пример #12
0
 def generate_phrase(self):
     super(RhetoricalExpert, self).generate_phrase()
     phrase = choice(self.productions)
     phrase.append("?")
     noun = choice(self.blackboard.pool.epithets.keys())
     try:
         adj = choice(self.blackboard.pool.epithets[noun])
     except:
         return
     replace_words = {'adj': adj, 'n': noun, 'be': en.conjugate("be", tense=self.tense.replace("1", "3").replace("2", "3"))}
     for pos in replace_words:
         while pos in phrase:
             try:
                 phrase = self.replace_pos(pos, replace_words[pos], phrase)
             except:
                 return
     for w in phrase:
         if not isinstance(w, Word):
             phrase[phrase.index(w)] = Word(w)
     return phrase
Пример #13
0
    def generate_phrase(self):
        adj = choice([a for a in self.blackboard.pool.comparisons if len(self.blackboard.pool.comparisons[a]) > 0])
        parser = ChartParser(self.grammar)
        gr = parser.grammar()
        phrase = self.produce(gr, gr.start())
        noun = choice(list(self.blackboard.pool.comparisons[adj]))
        noun.name = en.singularize(noun.name)
        article = en.referenced(noun.name).split(" ")[0]
        replace_words = {'adj': adj, 'n': noun, 'det': article}

        for pos in replace_words:
            while pos in phrase:
                try:
                    phrase = self.replace_pos(
                        pos, replace_words[pos], phrase)
                except:
                    return
        for w in phrase:
            if not isinstance(w, Word):
                phrase[phrase.index(w)] = Word(w)
        return phrase
Пример #14
0
 def generate_phrase(self):
     super(SentenceExpert, self).generate_phrase()
     phrase = choice(self.productions)
     noun = choice(list(self.blackboard.pool.nouns))
     try:
         replace_words = {
             'n': [noun],
             'v': [
                 Word(
                     en.conjugate(v.name,
                                  tense=self.tense.replace("1",
                                                           "3").replace(
                                                               "2", "3")))
                 for v in list(self.blackboard.pool.verbs[noun])
             ],
             'adj':
             self.blackboard.pool.epithets[noun],
             'atv': [Word(self.conjugate(v)) for v in self.atv],
             'eva': [Word(self.conjugate(v)) for v in self.eva],
             'ej':
             self.blackboard.pool.emotional_adjectives,
             'en':
             self.blackboard.pool.emotional_nouns,
             'erb':
             self.blackboard.pool.emotional_adverbs,
             'person': [Word(self.persons[self.tense][0])],
             'pron': [Word(self.persons[self.tense][1])]
         }
     except (IndexError, TypeError):
         return
     for pos in replace_words:
         while pos in phrase:
             words = replace_words[pos]
             if len(words) > 0:
                 word = choice(words)
                 phrase = self.replace_pos(pos, word, phrase)
             else:
                 return
     for w in phrase:
         if not isinstance(w, Word):
             phrase[phrase.index(w)] = Word(w)
     return phrase
Пример #15
0
 def _add_epithets(self, word):
     epithets = set([Word(w, "JJ") for w in self._find_epithets(word)])
     if word not in self.blackboard.pool.epithets:
         self.blackboard.pool.epithets[word] = []
     self.blackboard.pool.epithets[word] += list(epithets)
     return epithets
Пример #16
0
    def _add_comparisons(self, adj):

        comparisons = set([Word(w, "N") for w in self._find_comparisons(adj)])
        self.blackboard.pool.comparisons[adj] = comparisons
        return comparisons
Пример #17
0
 def _add_verbs(self, word):
     verbs = set([Word(w, "V") for w in self._find_verbs(word)])
     self.blackboard.pool.verbs[word] = list(verbs)
     return verbs