单词的名词,动词,形容词等的单独列表
问题描述:
我想解析一行并提取在Wordnet database 中找到的单词,但我不知道该怎么做。例如,index.adj文件包含以下行:单词的名词,动词,形容词等的单独列表
abactinal a 1 1 ! 1 0 01665972
abandoned a 2 1 & 2 1 01313004 01317231
abashed a 1 1 & 1 1 00531628
abasic a 1 2 \ + 1 0 02598608
abatable a 1 2 & + 1 0 02288022
abatic a 1 2 \ + 1 0 02598608
abaxial a 1 2 ! ; 1 0 00002312
abbatial a 1 2 \ + 1 0 02598768
abbreviated a 2 1 & 2 1 01436432 01442597
abdicable a 1 2 & + 1 0 02528048
abdominal a 1 2 \ + 1 1 02934594
abdominous a 1 2 & + 1 0 00986457
我使用.NET和C#,我曾尝试:
Regex regex = new Regex(@"/^(\S+?)[\s%]/");
Match match = regex.Match(line);
我找字典数据库创建数据挖掘工具。
答
由于此输入是简单的(白色)空格分隔文本,因此您不需要使用正则表达式来完成此任务。使用此代码:
var txt5 = "abactinal a 1 1 ! 1 0 01665972\r\nabandoned a 2 1 & 2 1 01313004 01317231\r\nabandon v 2 1 & 2 1 01313004 01317231 ";
var dic = new List<KeyValuePair<string, string>>();
var lines = txt5.Split(new string[] {"\r\n"}, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var cells = line.Split();
switch (cells[1])
{
case "a":
dic.Add(new KeyValuePair<string, string>("adjective", cells[0]));
break;
case "v":
dic.Add(new KeyValuePair<string, string>("verb", cells[0]));
break;
// Add more to cover all POS values
default:
break;
}
}
您可以调整它并进一步工作。
输出:
究竟什么是你想在该字符串相匹配?您拥有的正则表达式是一种JavaScript风格的正则表达式,在C#中无法按预期工作。如果您打算匹配单词,我会使用'@“\ b \ p {L} + \ b”'正则表达式并使用'RegexMatches'来返回字符串中的单词集合。 –
对不起,我从文件中发布了错误的文本,可以找到我现在添加的行的正则表达式。有些单词包含_也 – jobinelv
这看起来像是一个空格分隔列表给我。为什么你需要正则表达式? –