2021SC@SDUSC
系列文章目录
(六)PKE模型使用一
目录
二、Document frequency counts即文档频率计数
前言
上一篇博客已经粗略介绍了相关任务和模型,本文将介绍任务的几个评价指标,并介绍pke模型使用的教程
一、任务需要模型做什么
问题描述
输入:一个文本库,算法描述(主要是中文)
输出:1)对于每个文本x,抽取关键词作为算法的技术特征
2)(高)同时在以此关键词在数据集中进行检索时,能够反映本文x的主旨
约束条件:
- 抽取出的关键词与文本内容的相关性(关键词对于当前文本的代表性)
- 给出抽取到的关键词合适的次序
- 抽取出的关键词在数据集中的特异性(关键词在当前本文的重要性,相对于数据集中其他文本的显著性)
评价指标:
准确率PR、召回率RR、F1:
精准率和召回率是广泛应用于关键词抽取任务的评价指标,用于衡量关键词抽取模型的准确程度,其中精准率表示算法提取的关键词与人工给定关键词相匹配的个数与算法提取的总关键词数量的比值,召回率则表示算法提取的关键词与人工给定关键词相匹配的个数与人工给出的关键词数量的比值。F1分数表示精确率和召回率的调和平均数,当精准率和召回率两个指标发生冲突时,通常采用F1分数进行综合考量。为了验证关键词提取算法的性能,采用准确率(PR),召回率(RR),F1值(F1)作为关键词提取评价指标。准确率、召回率、F1值计算公式分别如下:
MRR:排名倒数,评估关键词次序的重要程度,即正确关键词在抽取的关键词结果中的排名来评估抽取的性能。Q表示文本实际的关键词的集合,|Q|表示文本实际的关键词的数量,ranki表示文本实际对应的第i个关键词在抽取出的关键词集合中的位置。如果实际的关键词在抽取出关键词集合中的位置越靠前,MRR的值就会越大。
SN:评估算法抽取的前topK个关键词在数据集中的特异性,即抽取出的关键词相对于数据集中其他文本的显著性。
从本部分开始,将基于pke documentation进行代码分析,介绍使用PKE模型前的准备,本篇博客主要介绍DF
二、Document frequency counts即文档频率计数
文档频率就是一个词在文档中出现的次数。pke 附带在 SemEval-2010 基准数据集上计算的文档频率 (DF) 计数。这些计数用于各种模型(例如 TfIdf 和 Kea)。
首先分析如何计算DF的相关代码,代码将讲解如何从另一个(或更大的)文档集合计算新的文档频率计数:
首先代码实现的功能是:从文档集合中计算DF。提取N-grams 至 3-grams,转化为n-stems形式。那些包含在停止列表中出现的token的代码将被过滤掉。输出文件为压缩的(gzip) 标签分隔值格式(tsv.gz)。
(一)引用的库
from pke import compute_document_frequency
from string import punctuation
pke不用多说,我们正是要分析他。
下面介绍string库中的punctuation
String模块包含大量实用常量和类,以及一些过时的遗留功能,并还可用作字符串操作。这里就是字符串常量中的一个小方法,string.punctuation -> 所有标点字符
在Python3中,string.punctuation是用作字符串常量的预初始化字符串。在Python中,string.punctuation将给出所有的标点符号。
用法: string.punctuation
参数:由于它不是函数,因此不带任何参数。
返回值:返回所有标点符号。
注意:确保导入字符串库函数以便使用string.punctuation
代码:
# import string library function
import string
# Storing the sets of punctuation in variable result
result = string.punctuation
# Printing the punctuation values
print(result)
输出:
!"#$%&'()*+, -./:;<=>?@[\]^_`{|}~
(二)代码部分
首先建立stopplist用于过滤n-grams,之后计算df计数并存储为n-stem ->权重值,参数分别为输入目录、输出文件、输入文件扩展名、文件语言、使用分词算法进行标准化、以及用于过滤n-grams的stopplist
stoplist=list(punctuation)
compute_document_frequency(input_dir='/path/to/collection/of/documents/',
output_file='/path/to/output.tsv.gz',
extension='xml',
language='en',
normalization="stemming",
stoplist=stoplist)
其中这里使用的英语分词算法是Porter stemmer,实现还原英文单词原型,用于单词词干提取,主页是Porter Stemming Algorithm。它被实现为N多版本,C、Java、Perl等,Porter stemmer 并不是要把单词变为规范的那种原来的样子,它只是把很多基于这个单词的变种变为某一种形式!换句话说,它不能保证还原到单词的原本,也就是"created"不一定能还原到"create",但却可以使"create" 和 "created" ,都得到"creat"。
简单介绍如下:
Stemming, in the parlance of searching and information retrieval, is the operation of stripping the suffices from a word, leaving its stem. Google, for instance, uses stemming to search for web pages containing the words connected, connecting, connection and connections when you ask for a web page that contains the word connect.
There are basically two ways to implement stemming. The first approach is to create a big dictionary that maps words to their stems. The advantage of this approach is that it works perfectly (insofar as the stem of a word can be defined perfectly); the disadvantages are the space required by the dictionary and the investment required to maintain the dictionary as new words appear. The second approach is to use a set of rules that extract stems from words. The advantages of this approach are that the code is typically small, and it can gracefully handle new words; the disadvantage is that it occasionally makes mistakes. But, since stemming is imperfectly defined, anyway, occasional mistakes are tolerable, and the rule-based approach is the one that is generally chosen.
In 1979, Martin Porter developed a stemming algorithm that, with minor modifications, is still in use today; it uses a set of rules to extract stems from words, and though it makes some mistakes, most common words seem to work out right. Porter describes his algorithm and provides a reference implementation in C at http://tartarus.org/~martin/PorterStemmer/index.html;
(三)计算反向文档频率 (IDF) 权重
DF计数以压缩(gzip)、制表符分隔值文件的形式存储。用于计算逆文档频率(Inverse Document Frequency, IDF)权重的集合中的文档数被存储为文档的额外行:
--NB_DOC-- tab number_of_documents
下面是这样一个文件的示例(未压缩)
--NB_DOC-- 100
greedi alloc 1
sinc trial structur 1
complex question 1
[...]
应加载新计算的 DF 计数,并将其作为参数提供给方法:candidate_weighting()
下面代码实现的功能是:关键词提取使用TfIdf和新计算的DF计数。
流程为:初始化TfIdf模型 ->从文件中加载DF计数 ->从文档中加载内容 ->关键短语候选人选择 ->使用提供的DF计数进行候选加权 ->n -最佳选择,关键短语包含10个得分最高的候选元组(关键短语,得分)
import pke
extractor = pke.unsupervised.TfIdf()
df_counts = pke.load_document_frequency_file(input_file='/path/to/df_counts')
extractor.load_document(input='/path/to/input.txt')
extractor.candidate_selection()
extractor.candidate_weighting(df=df_counts)
keyphrases = extractor.get_n_best(n=10)
总结
本篇博客主要介绍了任务的几个评价指标,和pke中DF的一些情况,其他pke使用教程请看下篇博客