1. 程式人生 > 實用技巧 >第七天學習進度--(KBQA)初接觸知識圖譜之最終改進(四)

第七天學習進度--(KBQA)初接觸知識圖譜之最終改進(四)

通過這幾天對於知識圖譜的簡單構建,簡單地瞭解到了對應的知識圖譜中相關的工作原理。

對於networkx構建知識圖譜的學習本來打算是昨天就完結了,可是在昨天的最後測試中發現了對應新增動態知識的過程還存在bug,因此今天對其進行最終的改進。

昨天對之前編寫的知識圖譜添加了動態提取知識的功能,其中對應的動態知識提取的功能在句子的前面還添加了對應的node1節點,在平時時候的時候node1節點並不是一定要放在第一個位置的,因此今天打算通過對昨天提取動態知識的函式進行一個。

昨天編寫的最終動態知識提取的時候

可以看到自定義句型中

這一句我們在自定義動態語句的時候,對應的主語前面並沒有新增上node1節點,可是最終的結果中,依舊存在node1(揭陽)

因此我們需要對之前的原子處理的部分進行一個改進。讓其在處理簡單的知識的時候,自動對對應的句子新增上node1,在處理動態知識的時候,則只考慮自定義函式中的返回值。

首先對原子處理部分進行簡單的更改(思路:返回時新增一個返回布林值,當返回布林值為True時,則新增node1節點)

# 原子資訊處理
def nlp_atom_handle(digraph: nx.DiGraph, node1, node2, relation="relation"):
    ptype = get_nodes_relation(digraph, node1, node2, relation)
    n_2 
= str(node2) n_1 = str(node1) try: n_relation = str(digraph[node1][node2][relation]) except: n_relation= str(digraph[node2][node1][relation]) global dictfunction try: if not dictfunction: dictfunction = {} except: dictfunction = {}
if (dictfunction): if (node1 in dictfunction): return dictfunction[node1](digraph, node1, node2, n_relation, relation),False elif (relation in dictfunction): return dictfunction[n_relation](digraph, node1, node2, n_relation, relation),False elif (node2 in dictfunction): return dictfunction[node2](digraph, node1, node2, n_relation, relation),False if (ptype == 4): return "" + n_2 + "" + n_relation,True elif (ptype == 3): return n_relation + n_2 + ";" + n_2 + n_relation + n_1,True elif (ptype == 2): return n_2 + n_relation + n_1,True elif (ptype == 1): return n_relation + n_2,True else: return None,True

則對應的nlp中要加入判斷來對根據返回布林值進行相應的操作

# 處理長距離節點關係
def nlp_nodes(digraph: nx.DiGraph, node1, node2, relation="relation"):
    try:
        path = nx.dijkstra_path(digraph, node1, node2, weight='weight')
        # result = str(node1)
        result=""
        for i in range(len(path) - 1):
            if(i==0):
                _,g_judge=nlp_atom_handle(digraph, path[i], path[i + 1], relation)
                if not g_judge:
                    result=""
                else:
                    result+=str(node1)
            result += nlp_atom_handle(digraph, path[i], path[i + 1], relation)[0]
            if (i != len(path) - 2):
                result += ","
            else:
                result += ""
    except Exception:
        result = str(node1) + "" + str(node2) + "沒有任何關係。"
    return result


# 單節點釋義(What)
def nlp_node(digraph: nx.DiGraph, node1, relation="relation"):
    try:
        result = str(node1) + "(" + str(digraph.nodes[node1]['attribute']) + ")"
        path = [one for one in digraph.neighbors(node1)]
        for i in range(len(path)):
            if (i == 0):
                _, g_judge =  nlp_atom_handle(digraph, node1, path[i], relation)
                if not g_judge:
                    result = ""
            result += nlp_atom_handle(digraph, node1, path[i], relation)[0]
            if (i != len(path) - 1):
                result += ","
        result += ""
        prepath = path
        path = [one for one in digraph.predecessors(node1) if one not in prepath]

        for i in range(len(path)):
            result += nlp_atom_handle(digraph, node1, path[i], relation)[0]
            if (i != len(path) - 1):
                result += ","
            else:
                result += ""
    except Exception:
        result = "知識圖譜中不存在" + str(node1)
    return result

在執行同樣的程式碼之後

執行結果如下:

可以看到最終的結果已經沒有了node1(揭陽)的主語。