def test_get_similar_words_weighted(self): synsets = Synsets() self.assertIsNotNone(synsets) self.assertEqual(['hack', 'cab', 'chop'], synsets.get_similar_words("hack", weight=3)) self.assertEqual([], synsets.get_similar_words("hack", weight=10))
def test_get_similar_words(self): synsets = Synsets() self.assertIsNotNone(synsets) self.assertEqual(['hack', 'machine_politician', 'cab', 'chop'], synsets.get_similar_words("hack")) self.assertEqual([], synsets.get_similar_words("")) self.assertEqual([], synsets.get_similar_words("lkjghjokg"))
class IsSynset(DynamicSet): NAME = "SYNSET" SIMILAR = 'similar' def __init__(self, config): DynamicSet.__init__(self, config) self._synset = Synsets() def is_member(self, client_context, value, additional=None): if value is not None: if additional is not None: if IsSynset.SIMILAR in additional: similar = additional[IsSynset.SIMILAR] similars = self._synset.get_similar_words(similar) for word in similars: if word.upper() == value.upper(): return True else: YLogger.error(self, "Missing 'similar' attribute passed to IsSynset.is_member!") else: YLogger.error(self, "Missing additional parameters passed to IsSynset.is_member!") else: YLogger.error(self, "None value passed to IsSynset.is_member!") return False
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"
def is_member(self, client_context, value, additional=None): if value is not None: if additional is not None: if 'similar' in additional: similar = additional['similar'] similars = Synsets.get_similar_words(similar) for word in similars: if word.upper() == value.upper(): return True return False
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"
def test_get_similar_words(self): self.assertEquals(['hack', 'machine_politician', 'cab', 'chop'], Synsets.get_similar_words("hack")) self.assertEquals([], Synsets.get_similar_words("")) self.assertEquals([], Synsets.get_similar_words("lkjghjokg"))
def test_get_similar_words_weighted(self): self.assertEquals(['hack', 'cab', 'chop'], Synsets.get_similar_words("hack", weight=3)) self.assertEquals([], Synsets.get_similar_words("hack", weight=10))