PFR人民日报标注语料库(版本1.0,下面简称PFR语料库)是在得到人民日报社新闻信息中心许可的条件下,以1998年人民日报语料为对象,由北京大学计算语言学研究所和富士通研究开发中心有限公司共同制作的标注语料库。该语料库对600多万字节的中文文章进行了分词及词性标注,其被作为原始数据应用于大量的研究和论文中。由于该语料库是以文本形式提供的,本文给出了读取该语料库的C#代码,供网友参考。
人民日报1998年中文标注语料库及读取代码
代码作者:肖波
语料库:北京大学计算语言学研究所和富士通研究开发中心有限公司
PFR人民日报标注语料库(版本1.0,下面简称PFR语料库)是在得到人民日报社新闻信息中心许可的条件下,以1998年人民日报语料为对象,由北京大学计算语言学研究所和富士通研究开发中心有限公司共同制作的标注语料库。该语料库对600多万字节的中文文章进行了分词及词性标注,其被作为原始数据应用于大量的研究和论文中。
由于该语料库是以文本形式提供的,本文给出了读取该语料库的C#代码,供网友参考,代码中用到了一些公共的类,如CRegx,CFile等位于KTDictSeg组件中,可以到我的另一篇文章《KTDictSeg 一种简单快速准确的中文分词方法》中下载。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using KTDictSeg;
using General;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace MachineLeaning
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
class T_Word
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public String Word; //单词
public int POS; //词性
public long Freq; //词频
};
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 1998年人民日报标注预料库的提取
/// </summary>
class People1998
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Private Members#region Private Members
static Hashtable m_WordTbl = new Hashtable(); //单词表
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 将字符串类型的词性转换为位域型的词性变量
/// </summary>
/// <param name="strPOS">字符串类型的词性标注</param>
/// <returns>位域型词性标注</returns>
static private T_POS GetPosFromString(String strPOS)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strPOS = strPOS.Trim().ToLower();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
switch(strPOS)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
case "ag":
case "ad":
case "an":
case "a": // 形容词 形语素
return T_POS.POS_D_A;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "bg":
case "b": // 区别词 区别语素
return T_POS.POS_D_B;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "c": // 连词 连语素
return T_POS.POS_D_C;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "dg":
case "d": // 副词 副语素
return T_POS.POS_D_D;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "e": // 叹词 叹语素
return T_POS.POS_D_E;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "f": // 方位词 方位语素
return T_POS.POS_D_F;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "i": // 成语
return T_POS.POS_D_I;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "j":
case "l": // 习语
return T_POS.POS_D_L;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "mg":
case "m": // 数词 数语素
return T_POS.POS_A_M;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "mq": // 数量词
return T_POS.POS_D_MQ;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "na":
case "ng":
case "n": // 名词 名语素
return T_POS.POS_D_N;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "o": // 拟声词
return T_POS.POS_D_O;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "p": // 介词
return T_POS.POS_D_P;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "q": // 量词 量语素
return T_POS.POS_A_Q;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "rg":
case "r": // 代词 代语素
return T_POS.POS_D_R;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "s": // 处所词
return T_POS.POS_D_S;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "tg":
case "t": // 时间词
return T_POS.POS_D_T;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "u": // 助词 助语素
return T_POS.POS_D_U;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "vg":
case "vn":
case "vd":
case "vv":
case "v": // 动词 动语素
return T_POS.POS_D_V;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "w": // 标点符号
return T_POS.POS_D_W;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "x": // 非语素字
return T_POS.POS_D_X;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "yg":
case "y": // 语气词 语气语素
return T_POS.POS_D_Y;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "z": // 状态词
return T_POS.POS_D_Z;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "nr":// 人名
return T_POS.POS_A_NR;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "ns":// 地名
return T_POS.POS_A_NS;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "nt":// 机构团体
return T_POS.POS_A_NT;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "nx":// 外文字符
return T_POS.POS_A_NX;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "nz":// 其他专名
return T_POS.POS_A_NZ;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "h": // 前接成分
return T_POS.POS_D_H;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "k": // 后接成分
return T_POS.POS_D_K;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
case "un":// 未知词性
return T_POS.POS_UNK;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
default:
return T_POS.POS_UNK;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 将一个单词插入单词表
/// </summary>
/// <param name="word">单词</param>
/// <param name="strPOS">词性</param>
static private void InsertOneWordToTbl(String word, String strPOS)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (word == null || strPOS == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
word = word.Trim();
if (word == "")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
T_POS tPOS = GetPosFromString(strPOS);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
T_Word tWord = (T_Word)m_WordTbl[word];
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (tWord == null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tWord = new T_Word();
tWord.Freq = 0;
tWord.POS = 0;
tWord.Word = word;
m_WordTbl[word] = tWord;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
tWord.Freq++;
tWord.POS |= (int)tPOS;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 删除文本中每句前面的时间
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
static private String DeleteDate(String text)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return CRegex.Replace(text, @"1998\d+-\d+-\d+-\d+\/\w", "", true);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 从文件载入到字符串中
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
static private String LoadFromFile(String fileName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return CFile.ReadFileToString(fileName, "GB2312");
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 从字符串读入到单词表中
/// </summary>
/// <param name="text"></param>
static private void ReadWordFromString(String text)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ArrayList strs = new ArrayList();
CRegex.GetMatchStrings(text, @"(\w+)\s*\/\s*([a-zA-Z]{1,2})", true, ref strs);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
String word = "";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < strs.Count; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (i % 2 == 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
word = (String)strs[i];
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InsertOneWordToTbl(word, (String)strs[i]);
}
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Public Members#region Public Members
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 将预料库中的单词和词性标注载入到单词表中
/// </summary>
/// <param name="fileName">人民日报1998年标注预料库文件</param>
static public void Load(String fileName)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
String text = LoadFromFile(fileName);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
text = DeleteDate(text);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ReadWordFromString(text);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取某个单词的属性
/// </summary>
/// <param name="word">单词</param>
/// <returns></returns>
static public T_Word GetWordAttribute(String word)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (T_Word)m_WordTbl[word];
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取所有单词列表
/// </summary>
/// <returns>单词列表</returns>
static public List<T_Word> GetWordList()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
List<T_Word> retWords = new List<T_Word>();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach(String word in m_WordTbl.Keys)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
retWords.Add((T_Word)m_WordTbl[word]);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return retWords;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
调用示例
if (openFileDialogDict.ShowDialog() == DialogResult.OK)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
People1998.Load(openFileDialogDict.FileName);
}
catch(Exception e1)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CMsgBox.ShowErrorMessageBox(e1.Message);
}
}
语料库下载地址
由于较大,分成5包下载
https://files.cnblogs.com/eaglet/199801.part01.rar
https://files.cnblogs.com/eaglet/199801.part02.rar
https://files.cnblogs.com/eaglet/199801.part03.rar
https://files.cnblogs.com/eaglet/199801.part04.rar
https://files.cnblogs.com/eaglet/199801.part05.rar