项目之文本相似度比较

​ 对于当代大学生而言,毕业时写的论文是一个对自己知识的

1.基于jieba库对文件进行分词

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Similarity::wordfreq Similarity::getWordFreq(const char* filename)

{

ifstream fin(filename);

if(!fin.is_open())

{

cout << "Open File" << filename << "Failed"<<endl;

return wordfreq();

}

string line;
wordfreq freq;
while(!fin.eof())
{
getline(fin,line);
//GBK-->UTF8
line = GBKToUTF8(line);
//分词
vector<string> words;
_jieba.Cut(line,words,true);
//统计词频,统计时先对应停用词表去掉停用词
for(const auto& e : words)
{
if(_StopWordSet.count(e) > 0)
continue;
else
{
if(freq.count(e) > 0)
freq[e]++;
else
freq[e] = 1;
}
}
return freq;
}
}

2.而在统计词频时,需要对分好的词去掉停用词,使用jieba中给出的停用词文件构造停用词表(人类语言包含很多功能词。与其他词相比,功能词没有什么实际含义。 停用词主要包括数字、标点符号及使用频率 特高的词(代词,语气助词、副词、介词、连接词 )等。 我 我们 怎么办 总之 此外 然而 不如 不妨 。 , ? …….. 停用词不代表实际意义,所以不需要统计停用词的词频,停用词不参与构建词频向量 )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Similarity::getStopWord(const char* stopwordsFile)
{
ifstream fin(stopwordsFile);
if(!fin.is_open())
{
cout<<"Open File" << stopwordsFile <<"Failed" <<endl;
return;
}

string line;
while(!fin.eof())
{
getline(fin,line);
_StopWordSet.insert(line);
}
fin.close();
}
0%