def tagQuery(tag, factList): #print "tag:", tag answers = [] for tK in factList: # for each topic tKScore = wordSim(tag, tK, basic=True) for qK in factList[tK]: # for each sub-topic in the topic qKScore = wordSim(tag, qK, basic=True) for a in factList[tK][qK]: aScore = wordSim(tag, a, basic=True) score = max(tKScore, qKScore, aScore) if score >= 0.9: #print "adding:", [tK, qK, a, score] answers.append([tK, qK, a, 1.0]) return answers
def addToFacts(factList, fact, replaceInfo=False): # fact ex. ["apples", "what who", "red"] # want: # factList["apples"]["what who"] = "red" sub = fact[0] wType=fact[1] prop = fact[2] # make sure fact topic already in dictionary inDict = False for key in factList: #if sub.lower() == key.lower(): if wordSim(sub.lower(), key.lower(), basic=True) > 0.95: sub = key inDict = True break if not inDict: factList[sub] = {} # check if subtopic is in dictionary inSubDict=False for key in factList[sub]: if wType.lower() == key.lower(): wType = key inSubDict=True break if not inSubDict: factList[sub][wType] = [] if replaceInfo: factList[sub][wType] = prop else: factList[sub][wType].append(prop) return