E033

orsci-dm和nlp中文本分类等技术中常用去除停用词。下面展示编写去除停用词工具。该工具已经在orsci包中作为工具共享。

 

#include "stdafx.h"

#include "orsci.h"
#include "orsciDM.h"

using namespace orsci;
using namespace dm;


bool Demo_FileErazeStopWord()
{
cout << " orsci--tool:文本中去除停用词 ..." << endl;
cout << " http://www.orsci.cn" << endl;
cout << "输入:(1)样本文件A:存储分词后文档或样本文档,每行代表一篇文档或一个段落,各词使用空格或TAB键分隔;" << endl;
cout << " (2)文件A左侧保留列数(例如1):注:样本最左侧列可能作为分类组号,例如0,1,2等,所以可以保留。" << endl;
cout << " (3)停用词文件B:存储停用词表,每行为一个停用词!" << endl;
cout << "输出:自动生成文档A加后缀_stopword.txt文档!" << endl;
cout << "说明:允许文件A保留左侧若干列,比如作为分类标记。" << endl;
cout << "文件A格式举例“1 好好 学习 数据 挖掘”,其中1作为分类号。" << endl;

cout << endl;
cout << "[1]请输入样本文件:";
string sampleFileName;
while (sampleFileName == "") getline(cin, sampleFileName);

int mKeepColCount = 0;
cout << "[2]请输入样本文件左侧保留的列数(例如1):";
cin >> mKeepColCount;

cout << "[3]请输入停用词文件:";
string stopwordFileName;
while (stopwordFileName == "") getline(cin, stopwordFileName);

string mDesFileName = sampleFileName + "_stopword.txt";
cout << endl;

if (jw::FileExist(sampleFileName) == false) {cout << "样本文件不存在:" << sampleFileName << endl; return false;}
if (jw::FileExist(stopwordFileName) == false) {cout << "停用词文件不存在:" << stopwordFileName << endl; return false;}
cout << "[Confirm]即将进行停用词去除,目标文件:" << mDesFileName << endl;
cout << "确认继续(输入1--继续,输入0停止!):";
int mmm;
cin >> mmm;
if (mmm != 1) return false;

grid_wstring_Horizon g; //g0用于读取左侧保留列,g1用于读取右侧数据区。

if (mKeepColCount <= 0) //不需要保留信息
{
vwstring mStopList;
mStopList.loadFromTextFile(stopwordFileName, true);
g.loadFromTextFile(sampleFileName, true, true);
g.erazeStopWord(mStopList);
g.saveToTextFile(mDesFileName, L"\t", true, true);
}
else
{
vwstring mStopList;
mStopList.loadFromTextFile(stopwordFileName, true);
g.loadFromTextFile(sampleFileName, true, true);
grid_wstring mKeep;
vint mKeepColIndex = span(0, mKeepColCount - 1);
mKeep = g.subgrid(vint(), mKeepColIndex); //保存需要的列数
g.delete_col(mKeepColIndex); //暂时去除。
g.erazeStopWord(mStopList);

grid_wstring_Horizon desg = g.merge_insert_col(0, mKeep);
desg.saveToTextFile(mDesFileName, L"\t", true, true);
}
cout << "...OK!" << endl;
return true;
}


int main(int argc, _TCHAR* argv[])
{
Demo_FileErazeStopWord();
cout << endl;
cout << "press any key to stop..." << endl;
char pp;
cin >> pp;

return 1;
}

输出

(一)运行过程

orsci--tool:文本中去除停用词 ...
http://www.orsci.cn
输入:(1)样本文件A:存储分词后文档或样本文档,每行代表一篇文档或一个段落,各词
使用空格或TAB键分隔;
(2)文件A左侧保留列数(例如1):注:样本最左侧列可能作为分类组号,例如0,1
,2等,所以可以保留。
(3)停用词文件B:存储停用词表,每行为一个停用词!
输出:自动生成文档A加后缀_stopword.txt文档!
说明:允许文件A保留左侧若干列,比如作为分类标记。
文件A格式举例“1 好好 学习 数据 挖掘”,其中1作为分类号。

[1]请输入样本文件:E:\temp\sample.txt
[2]请输入样本文件左侧保留的列数(例如1):1
[3]请输入停用词文件:E:\temp\demo_stopword.txt

[Confirm]即将进行停用词去除,目标文件:E:\temp\abc.txt_stopword.txt
确认继续(输入1--继续,输入0停止!):1

press any key to stop...

(二)说明:

(1)在文本分析、情感分析、舆情分析等许多文本处理问题,需要去除停用词,可以构建一个停用词表,每行存储一个停用词,假设名为stopword.txt。

(2)样本文件假设格式为:左侧为分组号(分类号);右侧为文档数据。此时去除停用词只是去除右侧文档数据区,而不应该对分组号(分类号)去除停用词,所以本程序保留左侧若干列,而只是去除右侧文档区的停用词。

(3)程序输入:样本文件名、保留左侧的列数、停用词文件名;

(4)程序输出:自动在样本文件名增加后缀_stopword.txt作为输出文件。

(5)orsci-dm包支持数据分析和数据挖掘计算,可下载配套软件orsci应用。

书籍 姜维. 《数据分析与数据挖掘》、《文本分析与文本挖掘》
软件 orsci-dm开发包(C++语言、Delphi语言和C语言)。