2021SC@SDUSC
使用StandfordNlp替换nltk的相关方法
将nltk中的相关方法替换为nlp的相关方法,运行程序。
# 初始化standfordNlp
from stanfordcorenlp import StanfordCoreNLP
nlp = StanfordCoreNLP('http://localhost', port=9000,lang='zh')
# 修改分句、词性标记的相关方法
self.sentences = [nlp.pos_tag(s) for s in re.split(r'[,。 .]',self.input_text)]
# 修改分词方法
sentences = [nlp.pos_tag(nlp.word_tokenize(s)) for s in re.split(r'[,。 .]',text)]
# LoadFile初始化程序
def __init__(self, input_text):
"""
初始化LoadFile类
:param input_text: 输入的文本文件
"""
self.input_text = input_text
""" 输入文件路径. """
self.sentences = []
""" 输入文本的句子列表 """
self.words = []
""" 文本的此列表 (unigrams)"""
self.candidates = []
""" 输入文本的候选词列表. """
self.weights = {
}
""" 候选词的得分(权重). """
self.gold_keyphrases = []
# the text is tokenized at the sentence level and part-of-speech tags are assigned
# 元素是列表,代表一个句子,句子用列表表示,元素是(word,词性)
self.sentences = [nlp.pos_tag(s) for s in re.split(r'[,。 .]',self.input_text)]
存在的问题1
起初程序运行正常正常,然后出现错误:
[pool-1-thread-5] WARN CoreNLP - java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - Error at index 0 in: "和7"
修改数据集内容多次运行仍存在相同的问题。
由错误信息可知错误原因是decode的信息中有特殊字符’%‘,于是找到源码中关于’%‘的处理:
public static String decode(String s, String enc)
throws UnsupportedEncodingException{
boolean needToChange = false;
int numChars = s.length();
StringBuffer sb = new StringBuffer(numChars > 500 ? numChars / 2 : numChars);
int i = 0;
if (enc.length() == 0) {
throw new UnsupportedEncodingException ("URLDecoder: empty string enc parameter");
}
char c;
byte[] bytes = null;
// 对%,+做了特殊处理
while (i < numChars) {
c = s.charAt(i);
switch (c) {
case '+':
sb.append(' ');
i++;
needToChange = true;
break;
case '%':
/*
/*
*从%这个实例开始,处理所有
*形式为%xy的连续子字符串。 每一个
*子字符串%xy将产生一个字节。 转换所有
*通过这种方式获得的连续字节
*所提供的*个字符
*编码。
*/
try {
// (numChars-i)/3 is an upper bound for the number
// of remaining bytes
if (bytes == null)
bytes = new byte[(numChars-i)/3];
int pos = 0;
while ( ((i+2) < numChars) &&
(c=='%')) {
int v = Integer.parseInt(s.substring(i+1,i+3),16);
if (v < 0)
throw new IllegalArgumentException("URLDecoder: Illegal hex characters in escape (%) pattern - negative value");
bytes[pos++] = (byte) v;
i+= 3;
if (i < numChars)
c = s.charAt(i);
}
// A trailing, incomplete byte encoding such as
// "%x" will cause an exception to be thrown
if ((i < numChars) && (c=='%'))
throw new IllegalArgumentException(
"URLDecoder: Incomplete trailing escape (%) pattern");
sb.append(new String(bytes, 0, pos, enc));
} catch (NumberFormatException e) {
throw new IllegalArgumentException(
"URLDecoder: Illegal hex characters in escape (%) pattern - "
+ e.getMessage());
}
needToChange = true;
break;
default:
sb.append(c);
i++;
break;
}
}
return (needToChange? sb.toString() : s);
}
通过阅读URLDecode 解码源码可以看出程序是将“%”紧跟的后两位一起处理。
解决方案
对%紧跟的后两位做了处理,所以我们需要将%后两位做下处理,保证%的格式是%+两位数字。处理方式如下:
data= data.replace("%(?![0-9a-fA-F]{2})", "%25");
在这里利用了正则表达式的零宽负向先行断言(zero-width negative lookahead assertion)
再次运行程序,处理正常:
运行截图:
StandfordNlp服务端截图:
存在的问题2
Evaluation metrics均为0:
不难想到应该是评分相关的方法对无法处理中文,因此还需修改evaluation.py模块来对中文进行处理。
在下一篇博客中我将对Evaluation.py进行代码分析。