【问题标题】:Separate lists of words for nouns, verbs, adjectives, etc名词、动词、形容词等的单独单词列表
【发布时间】:2015-05-14 12:20:22
【问题描述】:

我正在尝试解析一行并提取在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);

我正在寻找用于创建数据挖掘工具的字典数据库。

【问题讨论】:

  • 你到底想在那个字符串中匹配什么?您拥有的正则表达式是 JavaScript 样式的正则表达式,在 C# 中无法按预期工作。如果您打算只匹配单词,我会使用 @"\b\p{L}+\b" 正则表达式并使用 RegexMatches 返回字符串中的单词集合。
  • 对不起,我从文件中发布了错误的文本,现在可以找到我添加的行的正则表达式。一些单词也包含_
  • 对我来说,这看起来像是一个以空格分隔的列表。为什么需要正则表达式?

标签: c# regex parsing dictionary


【解决方案1】:

此任务不需要正则表达式,因为此输入是简单的(空白)空格分隔文本。使用此代码:

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;
      }
 }

您可以对其进行调整并进一步工作。

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-20
    • 2016-03-07
    • 2021-03-06
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多