def __init__(self, concepts, settings): from SimilarityCalculator import SimilarityCalculator self.sc = SimilarityCalculator(settings) from WordNetSynonyms import WordNetSynonyms self.wns = WordNetSynonyms() self.totalThreshold = settings.SimilarityThreshold
def TestPipeline(): from SimilarityCalculator import SimilarityCalculator sc = SimilarityCalculator(settings) from WordNetSynonyms import WordNetSynonyms wns = WordNetSynonyms() while True: wordA = GetInputWord(1) if wordA != "n": wordB = GetInputWord(2) if wordB != "n": if settings.Reload is True: sc.ReloadSettings(settings) similarity = sc.GetSimilarity(wordA, wordB) print("Similarity: %f" % similarity) if similarity > settings.SimilarityThreshold: synonymity = wns.IsSynonym(wordA, wordB) print("Synonymity: %s" % synonymity) if synonymity is True: print("The words are both accepted in the model!") else: print( "One of the words is not accepted. They are synonyms." ) else: print("The words are both accepted in the model!") else: break else: break
class SynonymRemover(object): """Removes synonyms using a variety of methods""" def __init__(self, concepts, settings): from SimilarityCalculator import SimilarityCalculator self.sc = SimilarityCalculator(settings) from WordNetSynonyms import WordNetSynonyms self.wns = WordNetSynonyms() self.totalThreshold = settings.SimilarityThreshold # Returns all items in newCollection that don't have a synonym in oldCollection. def RemoveSynonyms(self, oldCollection, newCollection): noSynonyms = [] for newItem in newCollection: print("Comparing %s to collection:" % newItem) # DEBUG if (not self.IsSynonym(newItem, oldCollection)): noSynonyms.append(newItem) return noSynonyms # Returns whether a synonym of item is in collection. def IsSynonym(self, item, collection): similarity = self.sc.GetMaxSimilarity(item, collection) if similarity >= self.totalThreshold: if self.wns.HasSynonym(item, collection): return True else: return False else: return False
def calculateSimilarity(self): similarityCalculator = SimilarityCalculator(self.__movieUserMatrix) self.__similarityMatrix = similarityCalculator.calculateSimilarity()
def __init__(self, settings, loadAll=False): self.s = settings from SimilarityCalculator import SimilarityCalculator self.sc = SimilarityCalculator(self.s, loadAll=loadAll)
class CompoundHandler(object): """Handles compound terms.""" import Settings def __init__(self, settings, loadAll=False): self.s = settings from SimilarityCalculator import SimilarityCalculator self.sc = SimilarityCalculator(self.s, loadAll=loadAll) def GetSimilarity(self, compoundA, compoundB): wordsA = compoundA.split() wordsB = compoundB.split() if len(wordsA) > 2 or len(wordsB) > 2: print("We don't support compounds larger than two words yet.") return -1.0 if len(wordsA) == 0 or len(wordsB) == 0: print("One of the compounds is empty.") return -1.0 compoundAisCompound = len(wordsA) > 1 compoundBisCompound = len(wordsB) > 1 # Level - Altitude if compoundAisCompound is not True and compoundBisCompound is not True: # print("Comparing %s to %s:" % (wordsA[0], wordsB[0])) # DEBUG similarity = self.sc.GetSimilarity(wordsA[0], wordsB[0]) print("Similarity: %s <> %s = %f" % (wordsA[0], wordsB[0], similarity)) # DEBUG # Flight Level - Altitude elif compoundAisCompound is True and compoundBisCompound is not True: # print("Comparing %s to %s:" % (wordsA[0], wordsB[0])) # DEBUG similarityA = self.sc.GetSimilarity(wordsA[0], wordsB[0]) # Flight - Altitude print("similarityA: %s <> %s = %f" % (wordsA[0], wordsB[0], similarityA)) # DEBUG # print("Comparing %s to %s:" % (wordsA[1], wordsB[0])) # DEBUG similarityB = self.sc.GetSimilarity(wordsA[1], wordsB[0]) # Level - Altitude print("similarityB: %s <> %s = %f" % (wordsA[1], wordsB[0], similarityB)) # DEBUG similarity = (self.s.Alpha * similarityA + self.s.Beta * similarityB) print("Similarity: %f * %f + %f * %f = %f" % (self.s.Alpha, similarityA, self.s.Beta, similarityB, similarity)) # DEBUG # Level - Flight Altitude elif compoundAisCompound is not True and compoundBisCompound is True: # print("Comparing %s to %s:" % (wordsA[0], wordsB[0])) # DEBUG similarityA = self.sc.GetSimilarity(wordsA[0], wordsB[0]) # Level - Flight print("similarityA: %s <> %s = %f" % (wordsA[0], wordsB[0], similarityA)) # DEBUG # print("Comparing %s to %s:" % (wordsA[0], wordsB[1])) # DEBUG similarityB = self.sc.GetSimilarity(wordsA[0], wordsB[1]) # Level - Altitude print("similarityB: %s <> %s = %f" % (wordsA[0], wordsB[1], similarityB)) # DEBUG similarity = (self.s.Alpha * similarityA + self.s.Beta * similarityB) print("Similarity: %f * %f + %f * %f = %f" % (self.s.Alpha, similarityA, self.s.Alpha, similarityB, similarity)) # DEBUG # Flight Level - Flight Altitude else: # print("Comparing %s to %s:" % (wordsA[0], wordsB[0])) # DEBUG similarityC = self.sc.GetSimilarity(wordsA[0], wordsB[0]) # Flight - Flight print("similarityC: %s <> %s = %f" % (wordsA[0], wordsB[0], similarityC)) # DEBUG # print("Comparing %s to %s:" % (wordsA[1], wordsB[1])) # DEBUG similarityD = self.sc.GetSimilarity(wordsA[1], wordsB[1]) # Level - Altitude print("similarityD: %s <> %s = %f" % (wordsA[1], wordsB[1], similarityD)) # DEBUG # print("Comparing %s to %s:" % (wordsA[0], wordsB[1])) # DEBUG similarityEa = self.sc.GetSimilarity( wordsA[0], wordsB[1]) # Flight - Altitude print("similarityEa: %s <> %s = %f" % (wordsA[0], wordsB[1], similarityEa)) # DEBUG # print("Comparing %s to %s:" % (wordsA[1], wordsB[0])) # DEBUG similarityEb = self.sc.GetSimilarity(wordsA[1], wordsB[0]) # Level - Flight print("similarityEb: %s <> %s = %f" % (wordsA[1], wordsB[0], similarityEb)) # DEBUG similarity = (self.s.Gamma * similarityC + self.s.Delta * similarityD + self.s.Epsilon * similarityEa + self.s.Epsilon * similarityEb) print( "Similarity: (%f * %f) + (%f * %f) + (%f * %f) + (%f * %f) = %f" % (self.s.Gamma, similarityC, self.s.Delta, similarityD, self.s.Epsilon, similarityEa, self.s.Epsilon, similarityEb, similarity)) # DEBUG return similarity def ReloadSettings(self, settings): self.s = settings self.sc.ReloadSettings(settings)
def calculateSimilarity(self): similarityCalculator = SimilarityCalculator(self.__userMovieMatrix) self.__similarityMatrix = similarityCalculator.calculateSimilarity()
from SimilarityCalculator import SimilarityCalculator s = SimilarityCalculator(2)