Exemple #1
0
 def test_get_similarity(self):
     self.assertEquals([('octopus', 'runt', 0.1),
                        ('octopus', 'prawn', 0.3333333333333333),
                        ('octopus', 'shrimp', 0.0625),
                        ('octopus', 'runt', 0.1),
                        ('octopus', 'prawn', 0.0625),
                        ('octopus', 'shrimp', 0.1111111111111111)],
                       Synsets.get_similarity("Octopus", "Shrimp"))
Exemple #2
0
class SynsetsExtension(Extension):
    def __init__(self):
        self._synsets = Synsets()

    def _get_similarities(self, word1, word2, weight):
        results = self._synsets.get_similarity(word1, word2)
        for result in results:
            if result[2] >= weight:
                return True

        return False

    # execute() is the interface that is called from the <extension> tag in the AIML
    def execute(self, client_context, data):
        del client_context

        words = data.split(" ")
        if words[0] == 'SIMILAR':
            if len(words) == 3:
                word1 = words[1]
                word2 = words[2]
                weight = 0.00
            elif len(words) == 6:
                word1 = words[1]
                word2 = words[2]
                weight = float("%s.%s" % (words[3], words[5]))
            else:
                return "FALSE"

            try:
                if self._get_similarities(word1, word2, weight) is True:
                    return "TRUE"

            except Exception as e:
                YLogger.exception_nostack(self, "Failed to get similarity", e)

        elif words[0] == 'SIMILARS':

            if len(words) == 3:
                word_type = words[1]
                word = words[2]

                if word_type == 'WORDS':
                    results = self._synsets.get_similar_words(word)
                elif word_type == 'VERBS':
                    results = self._synsets.get_similar_verbs(word)
                elif word_type == 'NOUNS':
                    results = self._synsets.get_similar_nouns(word)
                elif word_type == 'ADJECTIVES':
                    results = self._synsets.get_similar_adjectives(word)
                elif word_type == 'ADVERBS':
                    results = self._synsets.get_similar_adverbs(word)
                else:
                    return "FALSE"

                return "TRUE %s" % " ".join([word.upper() for word in results])

        return "FALSE"
Exemple #3
0
    def test_get_similarity(self):
        synsets = Synsets()
        self.assertIsNotNone(synsets)

        self.assertEqual([('octopus', 'runt', 0.1),
                          ('octopus', 'prawn', 0.3333333333333333),
                          ('octopus', 'shrimp', 0.0625),
                          ('octopus', 'runt', 0.1),
                          ('octopus', 'prawn', 0.0625),
                          ('octopus', 'shrimp', 0.1111111111111111)],
                         synsets.get_similarity("Octopus", "Shrimp"))
Exemple #4
0
    def execute(self, context, data):

        words = data.split(" ")
        if words[0] == 'SIMILAR':
            if len(words) == 3:
                word1 = words[1]
                word2 = words[2]
                weight = 0.00
            elif len(words) == 6:
                word1 = words[1]
                word2 = words[2]
                weight = float("%s.%s"%(words[3], words[5]))
            else:
                return "FALSE"

            try:
                results = Synsets.get_similarity(word1, word2)
                for result in results:
                    if result[2] >= weight:
                        return "TRUE"
            except Exception as e:
                print(e)

        elif words[0] == 'SIMILARS':

            word_type = words[1]
            word = words[2]

            if word_type == 'WORDS':
                results = Synsets.get_similar_words(word)
            elif word_type == 'VERBS':
                results = Synsets.get_similar_verbs(word)
            elif word_type == 'NOUNS':
                results = Synsets.get_similar_nouns(word)
            elif word_type == 'ADJECTIVES':
                results = Synsets.get_similar_adjectives(word)
            elif word_type == 'ADVERBS':
                results = Synsets.get_similar_adverbs(word)
            else:
                return "FALSE"

            return "TRUE %s"%" ".join([word.upper() for word in results])

        return "FALSE"