N001

手工编写分词程序演示

 

#include "pch.h"
#include "orsci.h"
#include "orsci_dm.h"
#include "orsci_nlp.h"

using namespace orsci;
using namespace orsci::vmt;
using namespace dm;
using namespace nlp;


vwstring chaper10_1_1_exp_1()
{ //模拟词典
vwstring ret;

//[1]装载词库。

//方式一:代码中增加词。
ret.push_back(L"好好");
ret.push_back(L"学习");
ret.push_back(L"文本");
ret.push_back(L"分析");
ret.push_back(L"与");
ret.push_back(L"挖掘");

ret.push_back(L"社会");
ret.push_back(L"社会党");
ret.push_back(L"社会党党员");
ret.push_back(L"社会主义");
ret.push_back(L"中国");
ret.push_back(L"国有");
ret.push_back(L"企业");
ret.push_back(L"才能");
ret.push_back(L"发展");

//方式二:从文件中装载词。
//ret.loadFromTextFile("E:\\elus_dict.txt", true);

//[2]词典排序
ret.sort_inplace(true);

//cout << "装载词典...总数量..." << ret.size() << endl;

return ret;
}

vwstring chaper10_1_1_exp_2(const string & ADictFileName)
{ //从文本文件中装载词典。
vwstring ret;

ret.loadFromTextFile(ADictFileName, true);

//[2]词典排序
ret.sort_inplace(true);

//cout << "装载词典...总数量..." << ret.size() << endl;

return ret;
}

wstring chaper10_1_1_exp_3(const wstring & rawSen)
{ //自制正向最大匹配分词
//[1]装载词库。
vwstring mDictList = chaper10_1_1_exp_1();

//[2]进行词索引。
mDictList.sort_inplace(true); //升序排列,为了快速查找。

//[3]输入语句
//wstring rawSen = L"社会主义中国有企业才能发展";

//[4]开始正向最大匹配分词
vwstring mList;
const int mMaxWordLen = 20;
int k = 0;
while(k < rawSen.length())
{
int wordLen = mMaxWordLen;
for (; wordLen >= 2; -- wordLen)
{
if (rawSen.length() - k < wordLen) continue; //超过最大候选长度
int wordID = mDictList.find_binary_asc(rawSen.substr(k, wordLen));
if (wordID >= 0) break; //存在词。
}
mList.push_back(rawSen.substr(k, wordLen));
k += wordLen;
}
return mList.join(L"\t");
}

void chaper10_1_1_exp_4()
{ //自制正向最大匹配分词
wstring mSen = L"社会主义中国有企业才能发展";
cout << chaper10_1_1_exp_3(mSen) << endl;
mSen = L"好好向社会党党员先锋学习";
cout << chaper10_1_1_exp_3(mSen) << endl;

//执行结果:
//社会主义 中国 有 企业 才能 发展
//好好 向 社会党党员 先 锋 学习
}

wstring chaper10_1_1_exp_5(const wstring & rawSen)
{ //自制反向最大匹配分词
vwstring mDictList = chaper10_1_1_exp_1();

vwstring mList;
const int mMaxWordLen = 20;
int k = rawSen.length();
while(k > 0)
{
int wordLen = mMaxWordLen;
for (; wordLen >= 2; -- wordLen)
{
if (k < wordLen) continue; //超过最大候选长度
int wordID = mDictList.find_binary_asc(rawSen.substr(k - wordLen, wordLen));
if (wordID >= 0) break; //存在词。
}
mList.push_back(rawSen.substr(k - wordLen, wordLen));
k -= wordLen;
}
mList.reverse_inplace(); //由于前面是反向加入,现在应该逆转!
return mList.join(L"\t");
}

void chaper10_1_1_exp_6()
{ //自制反向最大匹配分词

wstring mSen = L"社会主义中国有企业才能发展";
cout << chaper10_1_1_exp_5(mSen) << endl;
mSen = L"好好向社会党党员先锋学习";
cout << chaper10_1_1_exp_5(mSen) << endl;

//执行结果:
//社会主义 中 国有 企业 才能 发展
//好好 向 社会党党员 先 锋 学习
}

void chaper10_1_1_exp_7()
{ //自制反向最大匹配分词

wstring mSen = L"2020年05月03日庆祝学习文本分析与文本挖掘10年";
cout << chaper10_1_1_exp_5(mSen) << endl;

//执行结果:
//2 0 2 0 年 0 5 月 0 3 日 庆 祝 学习 文本 分析 与 文本 挖掘 1 0 年
}

wstring chaper10_1_1_exp_8(const wstring & rawSen)
{ //自制正向最大匹配分词
//[1]装载词库。
vwstring mDictList = chaper10_1_1_exp_1();

//[2]进行词索引。
mDictList.sort_inplace(true); //升序排列,为了快速查找。

//[3]输入语句
//wstring rawSen = L"社会主义中国有企业才能发展";

//[4]开始正向最大匹配分词
vwstring mList;
const int mMaxWordLen = 20;
int k = 0;
while(k < rawSen.length())
{
int wordLen = mMaxWordLen;
for (; wordLen >= 2; -- wordLen)
{
if (rawSen.length() - k < wordLen) continue; //超过最大候选长度
int wordID = mDictList.find_binary_asc(rawSen.substr(k, wordLen));
if (wordID >= 0) break; //存在词。
if (TELUS::factoid_keywordType(rawSen.substr(k, wordLen)) != L"") break; //是仿词
}
mList.push_back(rawSen.substr(k, wordLen));
k += wordLen;
}
return mList.join(L"\t");
}

wstring chaper10_1_1_exp_9(const wstring & rawSen)
{ //自制反向最大匹配分词
vwstring mDictList = chaper10_1_1_exp_1();

vwstring mList;
const int mMaxWordLen = 20;
int k = rawSen.length();
while(k > 0)
{
int wordLen = mMaxWordLen;
for (; wordLen >= 2; -- wordLen)
{
if (k < wordLen) continue; //超过最大候选长度
int wordID = mDictList.find_binary_asc(rawSen.substr(k - wordLen, wordLen));
if (wordID >= 0) break; //存在词。
if (TELUS::factoid_keywordType(rawSen.substr(k - wordLen, wordLen)) != L"") break; //是仿词
}
mList.push_back(rawSen.substr(k - wordLen, wordLen));
k -= wordLen;
}
mList.reverse_inplace(); //由于前面是反向加入,现在应该逆转!
return mList.join(L"\t");
}

void chaper10_1_1_exp_10()
{ //自制反向最大匹配分词

wstring mSen = L"2020年05月03日庆祝学习文本分析与文本挖掘10年";
cout << chaper10_1_1_exp_8(mSen) << endl;

//执行结果:
//2 0 2 0 年 0 5 月 0 3 日 庆 祝 学习 文本 分析 与 文本 挖掘 1 0 年
}

void chaper10_1_1_exp_11()
{ //自制反向最大匹配分词

wstring mSen = L"2020年05月03日庆祝学习文本分析与文本挖掘10年";
cout << chaper10_1_1_exp_9(mSen) << endl;

//执行结果:
//2 0 2 0 年 0 5 月 0 3 日 庆 祝 学习 文本 分析 与 文本 挖掘 1 0 年
}

void chaper10_1_1_exp_12()
{ //ELUS系统分词

wstring mSen = L"2020年05月03日庆祝学习文本分析与文本挖掘10年";
cout << TELUS::seg_forwardMM(mSen) << endl;
cout << TELUS::seg_backwardMM(mSen) << endl;
cout << TELUS::seg_bigram(mSen) << endl;
cout << TELUS::seg_trigram(mSen) << endl;
//执行结果:
//2020年05月03日 庆祝 学习 文本 分析 与 文本 挖掘 10年
//2020年05月03日 庆祝 学习 文本 分析 与 文本 挖掘 10年
//2020年05月03日 庆祝 学习 文本 分析 与 文本 挖掘 10年
//2020年05月03日 庆祝 学习 文本 分析 与 文本 挖掘 10年
}

void chaper10_1_1_exp_13()
{ //ELUS系统分词

wstring mSen = L"社会主义中国有企业才能发展";
cout << TELUS::seg_forwardMM(mSen) << endl;
cout << TELUS::seg_backwardMM(mSen) << endl;
cout << TELUS::seg_bigram(mSen) << endl;
cout << TELUS::seg_trigram(mSen) << endl;
//执行结果:
//社会主义 中国 有 企业 才能 发展
//社会主义 中 国有 企业 才能 发展
//社会主义 中 国有 企业 才 能 发展
//社会主义 中 国有 企业 才 能 发展
}

void chaper10_1_1_exp_14()
{ //利用系统词典查词;仿词识别。

cout << TELUS::dict_lookupWordID(L"文本") << endl;
cout << TELUS::dict_lookupWordID(L"挖掘") << endl;
cout << TELUS::dict_lookupWordID(L"学习文本挖掘") << endl;
cout << TELUS::factoid_keywordType(L"2020年5月3日") << endl;
cout << TELUS::factoid_keywordType(L"100%") << endl;
TELUS::option_setFactoidRuleID(0);
cout << TELUS::factoid_keywordType(L"2020年5月3日") << endl;
cout << TELUS::factoid_keywordType(L"5月") << endl;
//执行结果:
//47716
//69114
//0
//date
//real
//
//date
}

void chaper10_1_1_exp_15()
{ //bigram文件分词:输入一个未切分的文件,输出一个分词后的文件。
TELUS::option_setSegTag(L"\t"); //修改为各词以<TAB>键分隔。
TELUS::seg_trigram_FileSeg("E:\\elus_raw.txt", "E:\\elus_seg.txt"); //文件分词。
}

输出

实现中文分词

书籍 姜维.《文本分析与文本挖掘》。
软件 orsci开发包(C++语言)。