朴素的句子相似度分析
任务
本任务数据来自SemEval 2012,任务是通过训练集对测试集句子给出相似度,任务评价指标是你给出的答案与golden standard的Pearson系数,即线性相关系数。
预处理
接下来实现一些预处理功能,并且把它们代码封装在tool.py和parser.py模块中。
处理接口
这个任务的文件名和脚本很不友好,所以我经过了更改文件名和脚本,把测试集和训练集重命名一下。并且能打印测试报告。
分割句子
文件句子之间用TAB即chr(9)分隔,所以以此为依据分割两个句子。
预处理句子
比如去掉特殊符号和数字,统一大小写(后来还加了抽词干等优化)。
构造词典
根据训练集和测试接构造词典。
句子向量化
把句子转化为向量。并且输出到data文件给C语言模块使用。
朴素算法
0.square version
sample版本,直接字符串相似度判断。
1.common version
判断共同单词个数计算相似度。
2.relate version
相比common,对不同单词计算不同单词直接相似度,这个数值由训练集得到。
句子相似度 = (每个单词和它最可能相关单词的相似度)的平均值
如果单词一样,认为它们最可能相关,否则找共同出现次数最多的单词。
训练时候把一个句子相似度分给句子里的不同单词组。
3.rel version
修正了relate版本的bug
并且句子相似度计算有了不同尝试,例如不计算最可能相关,计算所有相关单词组加权平均,或者计算topk相关单词组。训练时候也不平摊,给最相关topk平分句子相似度之类的。
所以rel有rel版本,rel+topk版本,rel+weight版本。
优化
CPP实现核心计算
由于词汇量有6000多,开一个词与词相关矩阵的话内存消耗很大,不适合用python书写,所以用C++实现核心部分。为此,必须把句子向量化以后输出到data文件中,所以特意实现来parser.py模块。
词汇抽主干
我尝试预处理时候加优化,尝试了nltk模块的lem和stem抽主干方法,结果lem(lemmatization)不仅要下载一大堆数据而且效果是stem好,所以用stem抽主干。
朴素算法细节
以下节选自我的文件夹里的readme.txt,记录了细节信息。
0.square version
This is a sample version implementing with difflib.
1.common version
Simply calculate the common words of two sentences:
Parse a line into vec1 and vec2, let c = len(vec1 & vec2), ci = len(veci),
Calculate sim(c, c1, c2) = (c/(c1+c2))*10.
2.relate version
Based on calculating the relation of word1 and word2:
word1 == word2, relation = 5
else if word1 or word2 is not in train file, make relation be 2.5
else calculate relation according to train file
For vec1, try to find the fittest word in vec2 for every word.
And so for vec2, then calculate the ave relation of them.
*A small bug detected: sim will fail to get the right t[vec1[i1]][vec2[i2]]
and r[vec1[i2]][vec2[i1]] reverse vec1 and vec2, fix it later in rel.
3.rel version
Fix the bug in relate version
And it also has a top-k version, in relate, find fittest word, but in
rel+topk, find top-k fittest words, and I find the best BASE 3.28.
And if count all words in, though considering weight, it will not do
better, such as rel+weight, in which weight is t[][].
测试结果
以下节选自我的文件夹里的result.odt,记录了细节信息。其中编号是当年比赛队伍的名次。
| Rank | ALL | MSRpar | MSRvid | SMT-eur | On-WN | SMT-news |
|---|---|---|---|---|---|---|
| 1 | 0.82392 | 0.68301 | 0.87390 | 0.52797 | 0.66408 | 0.49365 |
| 10 | 0.75621 | 0.60498 | 0.79389 | 0.42936 | 0.58705 | 0.33659 |
| 20 | 0.69458 | 0.41061 | 0.83514 | 0.51279 | 0.72730 | 0.43826 |
| 30 | 0.65287 | 0.61240 | 0.72403 | 0.55812 | 0.67025 | 0.45327 |
| rel+stem+topk | 0.64914 | 0.54242 | 0.72801 | 0.47065 | 0.69051 | 0.40858 |
| 40 | 0.62893 | 0.46862 | 0.80267 | 0.45739 | 0.65907 | 0.47131 |
| rel+stem | 0.62206 | 0.55246 | 0.70182 | 0.43565 | 0.64622 | 0.42871 |
| relate+stem | 0.60607 | 0.54596 | 0.70794 | 0.33776 | 0.62375 | 0.40262 |
| 50 | 0.59766 | 0.52941 | 0.74699 | 0.55306 | 0.56984 | 0.36589 |
| relate+lem | 0.59657 | 0.53764 | 0.70291 | 0.39200 | 0.61648 | 0.40860 |
| rel+stem+weight | 0.55711 | 0.50296 | 0.62515 | 0.28721 | 0.65020 | 0.38083 |
| relate | 0.54687 | 0.51783 | 0.61321 | 0.37839 | 0.60431 | 0.43599 |
| common+stem | 0.53148 | 0.47617 | 0.58151 | 0.49295 | 0.69284 | 0.39429 |
| common+lem | 0.53072 | 0.47552 | 0.58186 | 0.49443 | 0.68568 | 0.44757 |
| 60 | 0.52531 | 0.57348 | 0.71226 | 0.47805 | 0.69838 | 0.41773 |
| common | 0.47093 | 0.44824 | 0.44367 | 0.48509 | 0.66604 | 0.44783 |
| square+stem | 0.46503 | 0.32272 | 0.49689 | 0.48723 | 0.65936 | 0.39991 |
| 70 | 0.45924 | 0.65227 | 0.66907 | 0.35659 | 0.61167 | 0.46031 |
| 80 | 0.41570 | 0.42595 | 0.56284 | 0.15459 | 0.45522 | 0.19229 |
| baseline | 0.31096 | 0.43340 | 0.29957 | 0.45423 | 0.58642 | 0.39075 |
| 88 | -0.02596 | 0.11090 | 0.00569 | 0.03484 | 0.17880 | 0.19637 |
2697

被折叠的 条评论
为什么被折叠?



