最近研究知识图谱,看到了两篇知识图谱的文章[1][2],里面提到了一个项目,本人下载下来后,遇到了一些错误,在此把fix的过程分享一下。

先看README

第一步就是pyltp库安装不上,上网搜索得知,LTP库已经支持python了,只需要pip install ltp即可。

然后安装neo4j,选择对应的平台下载,解压后运行bin/neo4j console。此时终端显示:

2021-04-28 12:48:56.162+0000 INFO  Starting...
2021-04-28 12:48:58.117+0000 INFO  ======== Neo4j 4.1.8 ========
2021-04-28 12:48:59.236+0000 INFO  Performing postInitialization step for component 'security-users' with version 2 and status CURRENT
2021-04-28 12:48:59.236+0000 INFO  Updating the initial password in component 'security-users'
2021-04-28 12:48:59.721+0000 INFO  Bolt enabled on localhost:7687.
2021-04-28 12:49:00.579+0000 INFO  Remote interface available at http://localhost:7474/
2021-04-28 12:49:00.580+0000 INFO  Started.

然后访问http://localhost:7474/,使用默认用户名neo4j密码neo4j登录,然后修改密码。填入config.py中。

然后执行python .\neo_db\creat_graph.py,出现ImportError,在IDE中发现这一行标灰没什么用

from py2neo import Graph, Node, Relationship,NodeSelector

删掉这一行就可以运行了。

由于把依赖从pyltp改为了ltp,涉及到的代码均要修改。首先是KGQA/ltp.py,修改后的代码如下。

# -*- coding: utf-8 -*-
from ltp import LTP


def cut_words(words):
    ltp = LTP()
    seg, hidden = ltp.seg([words])
    pos = ltp.pos(hidden)
    return seg, pos


def get_target_array(words):
    arr = []
    seg, pos = cut_words(words)
    for k in zip(seg[0], pos[0]):
        if k[1] in ('nh', 'n'):
            arr.append(k[0])
    return arr

其他均为一些小的修改,具体可以查看Git Repo