【问题标题】:remove stopword from a String in asp.net c#从asp.net c#中的字符串中删除停用词
【发布时间】:2015-09-11 19:42:48
【问题描述】:

我在创建从字符串中删除停用词的代码时遇到问题。这是我的代码:

String Review="The portfolio is fine except for the fact that the last movement of sonata #6 is missing. What should one expect?";

string[] arrStopword = new string[] {"a", "i", "it", "am", "at", "on", "in", "to", "too", "very","of", "from", "here", "even", "the", "but", "and", "is","my","them", "then", "this", "that", "than", "though", "so", "are"};
StringBuilder sbReview = new StringBuilder(Review);
foreach (string word in arrStopword){
sbReview.Replace(word, "");}
Label1.Text = sbReview.ToString();

运行时Label1.Text = "The portfolo s fne except for fct tht lst movement st #6 s mssng. Wht should e expect? "

我希望它必须返回"portofolio fine except for fact last movement sonata #6 is missing. what should one expect?"

有人知道怎么解决吗?

【问题讨论】:

    标签: c# asp.net stop-words


    【解决方案1】:

    问题是您正在比较子字符串,而不是单词。您需要拆分原始文本,删除项目,然后重新加入。

    试试这个

    List<string> words = Review.Split(" ").ToList();
    foreach(string stopWord in arrStopWord)
        words.Remove(stopWord);
    string result = String.Join(" ", words);
    

    我能看到的唯一问题是它不能很好地处理标点符号,但你明白了。

    【讨论】:

      【解决方案2】:

      您可以使用 LINQ 来解决这个问题。您首先需要使用Split 函数将string 转换为stringlist,以" "(空格) 分隔,然后使用Except 获取结果将包含的单词,然后可以申请string.Join

      var newString = string.Join(" ", Review.Split(' ').Except(arrStopword));
      

      【讨论】:

      • 那是辣肉丸。不会想到“除了”。
      • 可爱、优雅的解决方案。通过使用 except 重载忽略大小写来增强,例如.Except(arrStopword, StringComparer.InvariantCultureIgnoreCase)
      【解决方案3】:

      您可以使用“a”、“I”等来确保程序仅在它们被用作单词时才删除这些单词(因此它们周围有空格)。只需将它们替换为空格即可保持格式不变。

      【讨论】:

        【解决方案4】:

        或者您可以使用dotnet-stop-words package。 只需调用RemoveStopWords 方法

        (yourString).RemoveStopWords("en");
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-12-16
          • 2014-06-06
          • 2015-02-25
          • 2014-05-22
          • 1970-01-01
          • 2019-12-18
          • 2016-10-06
          相关资源
          最近更新 更多