def Search(input): ResNodes = {x: 100 for x in range(1, 21)} SearchRes = [] nodes = load2prog("./data.dat") for node in nodes: # 完全匹配搜索 MatchTarget = [node.name, node.desc] for i in range(len(MatchTarget)): SelectRes = SelectAlogrithm(str(input), str(MatchTarget[i])) if SelectRes == 100: SelectRes = SelectAlogrithm(GetPinYin(input), GetPinYin(MatchTarget[i])) SelectRes += 1 if i == 1 and SelectRes != 100: SelectRes += 2 ResNodes[node.id] = min(ResNodes[node.id], SelectRes) input_word, input_pinyin = CutWord(input) desc_word, desc_pinyin = CutWord(node.desc) if (ResNodes[node.id] == 100): # 部分匹配搜索 for word in input_word: for i in range(len(MatchTarget)): ResNodes[node.id] = min(ResNodes[node.id], SelectAlogrithm(word, str(MatchTarget[i]))) if (ResNodes[node.id] == 100): # 编辑距离搜索 ResNodes[node.id] = max(ResNodes[node.id], Levenshtein(GetPinYin(input), GetPinYin(desc_word[0])) + 5) for word in desc_word: ResNodes[node.id] = min(ResNodes[node.id], Levenshtein(GetPinYin(input), GetPinYin(word)) + 5) ResNodesAftSort = sorted(ResNodes.items(), key=lambda item: item[1], reverse=False) for i in range(len(ResNodesAftSort)): SearchRes.append(GetNode(ResNodesAftSort[i][0])) return SearchRes
from models import load2prog nodes=load2prog("data.dat") def DFS(node, AftNodeList): for Aft_node in node.aft_list: if(Aft_node not in AftNodeList): AftNodeList.append(Aft_node) DFS(Aft_node, AftNodeList) def FindAft(node): AftNodeList = [] DFS(node,AftNodeList) return AftNodeList
from Search.search import Search from models import load2prog data = load2prog("./data.dat") TestInput = ["数组", "书组", "素组", "线信表", "数"] for input in TestInput: SearchRes = Search(input) print("输入内容为:" + input) print("结果为") for node in SearchRes: MatchTarget = [node.name, node.desc] for line in MatchTarget: print(line)
from models import load2prog from FindPreAft.FindPre import FindPre from FindPreAft.FindAft import FindAft nodes = load2prog("../data.dat") AftNodes = FindAft(nodes[0]) PreNodes = FindPre(nodes[-1]) for Nodes in [AftNodes, PreNodes]: print("-----------------") for node in Nodes: print(node.name)
def GetNode(id): nodes = load2prog("./data.dat") for node in nodes: if id == node.id: return node
def Sort_by_level(): nodes = models.load2prog('data.dat') sorted_nodes = QuickSort(nodes, cmp1) return sorted_nodes
def Sort_by_time(): nodes = models.load2prog('data.dat') sorted_nodes = QuickSort(nodes, cmp2) return sorted_nodes
def Sort_by_similarity(node): nodes = models.load2prog('data.dat') edgs = models.To_Graph() dis = heap_Dijsktra(edgs, node.id - 1) sorted_nodes = QuickSort(nodes, cmp3, dis=dis) return sorted_nodes