实验报告
Ytinrete
课程:自然语言处理导论
题目:实现中文分词算法
实现中文分词程序
从可行性(对我而言)的角度考虑,我只能实现基于字符串匹配的分词算法。具体为:
从字典中将词全部存入内存当中。
将词典的词一个一个跟待处理文件字节匹配,取最大的匹配长度并打印出空格。
继续读入直到将待处理文件读完。
//假设字典文件处于相对目录文件名:,其中的单词以每行一词存储
//源文件为:1998-01-qiefen-
//待处理文件处于相对目录文件名:(通过kill_space消除空行)
//
#include<iostream>
#include<fstream>
#include<map>
#include<string>
/*
Name: 删除空格
Description: 删除空格
*/
using namespace std;
int main()
{
FILE *f_in, *f_out;//输入输出文件
char ch;
f_in=fopen("1998-01-qiefen-", "r");
f_out=fopen("", "w");
ch=getc(f_in);
while(EOF!=ch)
{
if(' '!=ch&&'\n'!=ch)
putc(ch, f_out);
ch=getc(f_in);
}
return 0;
}
/*
Name: 中文分词程序
Description: 实现中文分词
*/
#include<iostream>
#include<fstream>
#include<map>
#include<string>
using namespace std;
const char *file_in = "1998-01-qiefen-";
const char *file_out = "";
const char *dictionary="";
map <string, int> dic;
void init(void)//初始化,存储字典进map
{
int count=0;
FILE *dic_in;
dic_in=fopen(dictionary, "r");
string word;
char ch;
ch=getc(dic_in);
while(EOF!=ch)
{
count++;
if('\n'!=ch)
{
(1, ch);
}
else
{
(pair<string,int>(word,count));
();
}
ch=getc(dic_in);
}
//cout<<count<<endl;
}
int main()
{
in
NPL 最大匹配分词 来自淘豆网www.taodocs.com转载请标明出处.