在项目原数据集上运行Position Rank代码存在的问题

2021SC@SDUSC

简介

在github上关于position rank的项目中提供了三个数据集,分别是“www”、”KDD“、”Nguyen“,本周的主要任务是在这三个数据集上跑通代码。目前存在的问题包括:1.数据集的读取方式未知。2.因实验室要求,将项目环境从python2修改为python3.7后部分库存在不兼容的情况。

根据python版本更新库函数

futures3.2.0

在安装futures3.2.0时,pip报错提示没有符合要求的包

CRITICAL:pip.index:Could not find a version that satisfies the requirement futures==3.2.0 (from versions: 0.2.python3, 0.1, 0.2, 1.0, 2.0, 2.1, 2.1.1, 2.1.2, 2.1.3, 2.1.4, 2.1.5, 2.1.6, 2.2.0, 3.0.0, 3.0.1, 3.0.2, 3.0.3, 3.0.4, 3.0.5, 3.1.0, 3.1.1)

在pypi.org上找到如下内容:

futures3.2.0 is a backport of the concurrent.futures standard library module to Python 2.

It does not work on Python 3 due to Python 2 syntax being used in the codebase. Python 3 users should not attempt to install it, since the package is already included in the standard library.

从中可以看出futures3.2.0不兼容python3.7,可以使用如下命名重装适用于python3的futures3 1.0.0

pip install futures3

其他部分依赖库存在的问题也可通过这种方式解决,这里不做过多赘述。

nltk

在使用nltk提供的分句、分词工具时出现如下错误

Please use the NLTK Downloader to obtain the resource:
nltk.download(‘punkt’)
Attempted to load [93mtokenizers/punkt/english.pickle[0m

经过分析,发现安装nltk后还需联网下载资源包。按照提示操作后又出现提示无法连接到服务器。在采用科学上网方式后下载正常。经过查阅资料,我发现还可以通过离线下载资源包,拷贝到nltk资源目录的方式解决该问题。

数据集读取

数据集路径可以作为参数传入运行指令,但是要注意路径中要避免使用中文路径来避免某些包不兼容中文路径读取的情况。

PositionRank
--input_data
D:\Desktop\PositionRank\PositionRank-master\data\data\WWW\docs
--input_gold
D:\Desktop\PositionRank\PositionRank-master\data\data\WWW\gold

Nguyen数据集部分内容读取失败

在读取Nguyen数据集时,编译器提示某些字符无法被utf-8解码,错误如下:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xad in position 121: invalid start byte

将错误定位到如下代码:

 if os.path.exists(this_file):
        with codecs.open(this_file, "r", encoding='utf-8') as f:
            text = f.read()
    else:
        text = None

这里采用的是读取时使用utf-8译码的操作,这种方式并不能解决编码错误的问题。因此我们需要一种能够将读取和译码分开的方式,同时这种方式能够解决乱码的问题。

python中bytes对象有decode方法,该方法的第一个参数是解码字符集,第二个参数是乱码的处理方式,‘ignore’忽略乱码的字符。

修改后的代码如下:

 if os.path.exists(this_file):
        with codecs.open(this_file, "rb") as f:
            b = f.read()
            # 忽略存在问题的编码
            text = b.decode('utf-8','ignore')
    else:
        text = None

在读取关键短语文件时也出现了类似的问题:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xad in position 19: invalid start byte

定位到如下代码:

    if os.path.exists(this_gold):
        with codecs.open(this_gold, "r", encoding='utf-8') as f:
            gold_list = f.readlines()
        f.close()
    else:
        gold_list = None

我们需要将读取到的关键词以列表的形式返回,因此借助了一个for循环,在译码的同时生成结果列表。

    if os.path.exists(this_gold):
        with codecs.open(this_gold, "rb") as f:
            b_list = f.readlines()
            gold_list = []
            for b in b_list:
                s = b.decode('utf-8','ignore')
                gold_list.append(s)
        f.close()
    else:
        gold_list = None

小结

在解决上述问题后代码在三个数据集上均顺利运行,得到了position rank模型在WWW、KDD、Nguyen上的表现情况。

例如在WWW数据集上结果如下:

在这里插入图片描述

おすすめ

転載: blog.csdn.net/Simonsdu/article/details/120856335