Exemplo n.º 1
0
    def score_options(self):
        word = self.text[self.index]
        potential_words = {word: 0}

        w = thesaurus.Word(word)
        synonyms = w.synonyms()
        for synonym in synonyms:
            potential_words[synonym] = 0

        for nextword in potential_words.keys():
            if len(self.poem) > 0:
                phonetic_similarity = score_phonetic_similarity(
                    self.poem[-1], nextword)
            else:
                phonetic_similarity = 0.5

            if self.next_syllable == UNSTRESSED:
                meter = "01"
            else:
                meter = "10"
            metric_conformity = score_metric_conformity(meter, nextword)

            score = phonetic_similarity * self.phonetic_similarity_weight + metric_conformity * self.metric_conformity_weight
            potential_words[nextword] = score

        return potential_words
Exemplo n.º 2
0
 async def thesaurize(self, ctx, *, message=None):
     if not message and not self.bot.db['last_message'][
             ctx.message.channel].content.startswith('__'):
         message = self.bot.db['last_message'][ctx.message.channel].content
     th_message = []
     for word in message.split(' '):
         try:
             th_word = random.choice(thesaurus.Word(word).synonyms())
         except Exception:
             th_word = word
         th_message.append(th_word)
     await ctx.send(' '.join(th_message))
Exemplo n.º 3
0
    def searchThesaurus(self, Excerpt):
        print("THESAURUS CHECK")
        verbCandidates = {}
        #Iterates for word in headline
        for word in Excerpt:
            thWord = th.Word(word)
            #Finds verb synonyms
            vSynonyms = thWord.synonyms('all',
                                        partOfSpeech=th.POS_VERB,
                                        allowEmpty=False)
            if vSynonyms:
                print("Candidate found")
                #Finds noun synonyms
                nSynonyms = thWord.synonyms('all',
                                            partOfSpeech=th.POS_NOUN,
                                            allowEmpty=False)
                if not nSynonyms:
                    print("Nonambiguous verb found")
                    verbCandidates = {word: vSynonyms}
                    break
                else:
                    verbCandidates[word] = vSynonyms

        for verb in verbCandidates.keys():
            baseForm = ""
            isPVerb = False
            synonyms_raw = verbCandidates[verb]
            Synonyms = self.deArray(synonyms_raw)

            #Adds word and recognized synonym version to PVC
            for synonym in Synonyms:
                serializedForm = self.search_PVC(synonym)
                if serializedForm:
                    isPVerb = True
                    baseWord = serializedForm
                    break
            if isPVerb:
                self.addWord([verb, baseWord])
                print(verb + ", " + baseWord)
                break
            else:
                print("Base form not found of: " + verb)

        print("CHECK COMPLETED")
Exemplo n.º 4
0
def poetify_old(sentence):
    
    poem = []
    next_syllable = UNSTRESSED

    for word in sentence.split(" "):
        print(word)

        stresses = get_stresses(word)
        if len(stresses) == 1:
            stresses = "3" # Ignore the stress of one-syllable words
        print(stresses)

        if next_syllable == STRESSED:
            desired_syl_pattern = r"[123]([03][123])*[03]*"
        else:
            desired_syl_pattern = r"[03]([123][03])*[123]*"
        print(desired_syl_pattern)
        print(re.match(desired_syl_pattern, stresses))

        if re.match(desired_syl_pattern, stresses):
            poem.append(word)
            if len(stresses) % 2 != 0:
                next_syllable = not next_syllable
        else:
            w = thesaurus.Word(word)
            synonyms = w.synonyms()
            if len(synonyms) > 0:
                good_synonyms = []
                for synonym in synonyms:
                    syn_stresses = get_stresses(synonym)
                    if re.match(desired_syl_pattern, syn_stresses):
                        good_synonyms.append(synonym)
                if len(good_synonyms) > 0:
                    poem.append(good_synonyms[0])
                else:
                    poem.append(word)
            else:
                poem.append(word)

    return poem
Exemplo n.º 5
0
def poetify(sentence, parameters=(PHONETIC_SIMILARITY_WEIGHT, METRIC_CONFORMITY_WEIGHT, 
    LINE_LENGTH_SYLLABLES, POEM_LENGTH, SCORE_THRESHOLD, SWITCH_ODDS)):

    poem = []
    next_syllable = UNSTRESSED
    pre_words = re.findall(r"([\d]+[,\d]+|\w+\'?\w*)", sentence)
    words = []

    for word in pre_words:

        # Check if each word is a number
        if not re.search(r"\d", word):
            words.append(word)
        else:
            # If word contains digits, split into digit and non-digit pieces
            word = re.sub(",", "", word)
            split_by_numbers = re.split(r"(\d+)", word)
            for piece in split_by_numbers:
                #print(piece)
                # Convert digits to strings and add non-digit strings
                if re.fullmatch(r"\d+", piece):
                    word_version = num2words(int(piece), lang="en")
                    words = words + re.findall(r"\w+", word_version)
                elif piece != "": 
                    words.append(piece)

    #print(words)

    index = 0 
    num_words = len(words)

    for i in range(0, POEM_LENGTH):

        #print(index)
        word = words[index]

        # Potential next words are stored in a dictionary with the format
        # word: score
        potential_words = {word:0}
        synonyms = []

        # Get word's synonyms to add to potential words
        try:
            w = thesaurus.Word(word)
            synonyms = w.synonyms()
        except:
            synonyms = []

        for synonym in synonyms:
            potential_words[synonym] =  0

        # Score potential words
        maxscore = 0
        maxscoreword = word

        for nextword in potential_words.keys():
            if len(poem) > 0: 
                phonetic_similarity = score_phonetic_similarity(poem[-1], nextword)

                if next_syllable == UNSTRESSED: 
                    meter = "01"
                else:
                    meter = "10"
                metric_conformity = score_metric_conformity(meter, nextword)

                score = phonetic_similarity * PHONETIC_SIMILARITY_WEIGHT + metric_conformity * METRIC_CONFORMITY_WEIGHT
                potential_words[nextword] = score

                if score > maxscore:
                    maxscore = score
                    maxscoreword = nextword

        
        if maxscore < SCORE_THRESHOLD:
            dice = random.randint(0,9)
            if dice < SWITCH_ODDS:
                i = i - 1
                index = random.randint(0, num_words-1)
                #print("switching")
                continue

        index = index + 1
        if index >= num_words:
            index = random.randint(0, num_words-1)

        #print(maxscoreword + str(maxscore))
        poem.append(maxscoreword)
        stresses = get_stresses(maxscoreword)
        if len(stresses) % 2 == 1:
            next_syllable = not next_syllable

    return poem_to_string(poem)
Exemplo n.º 6
0
def poetify_old_2(sentence):

    poem = []
    next_syllable = UNSTRESSED
    pre_words = re.findall(r"([\d]+[,\d]+|\w+\'?\w*)", sentence)
    words = []

    print(pre_words)

    for word in pre_words:

        print(word)

        # Check if each word is a number
        if not re.search(r"\d", word):
            words.append(word)
        else:
            # If word contains digits, split into digit and non-digit pieces
            word = re.sub(",", "", word)
            split_by_numbers = re.split(r"(\d+)", word)
            print(str(split_by_numbers))
            for piece in split_by_numbers:
                print(piece)
                # Convert digits to strings and add non-digit strings
                if re.fullmatch(r"\d+", piece):
                    word_version = num2words(int(piece), lang="en")
                    print(word_version)
                    words = words + re.findall(r"\w+", word_version)
                elif piece != "": 
                    words.append(piece)

    print(words)

    for word in words:

        # Potential next words are stored in a dictionary with the format
        # word: score
        potential_words = {word:0}

        # Get word's synonyms to add to potential words
        w = thesaurus.Word(word)
        synonyms = w.synonyms()
        for synonym in synonyms:
            potential_words[synonym] =  0

        # Score potential words
        maxscore = 0
        maxscoreword = word

        for nextword in potential_words.keys():
            if len(poem) > 0: 
                phonetic_similarity = score_phonetic_similarity(poem[-1], nextword)

                if next_syllable == UNSTRESSED: 
                    meter = "01"
                else:
                    meter = "10"
                metric_conformity = score_metric_conformity(meter, nextword)

                score = phonetic_similarity * PHONETIC_SIMILARITY_WEIGHT + metric_conformity * METRIC_CONFORMITY_WEIGHT
                potential_words[nextword] = score

                if score > maxscore:
                    maxscore = score
                    maxscoreword = nextword

        poem.append(maxscoreword)
        stresses = get_stresses(maxscoreword)
        if len(stresses) % 2 == 1:
            next_syllable = not next_syllable

    return poem_to_string(poem)
Exemplo n.º 7
0
def get_antonyms(word):
    print("ant", word)
    print(th.Word(word).antonyms(relevance=3, allowEmpty=False))
Exemplo n.º 8
0
def get_synonyms(word):
    print("syn", word)
    print(th.Word(word).synonyms(relevance=3, allowEmpty=False))