【问题标题】:stop words removal using c#使用 c# 去除停用词
【发布时间】:2015-09-05 06:34:36
【问题描述】:

我有两个字符串数组,即

string[] text = {"Paragraph 1 containing long text of ten to 20 lines", "Paragraph 2 containing long text of ten to 20 lines", "Paragraph 3 containing long text of ten to 20 lines",.....};

和另一个停用词数组,即

string[] stop_words = File.ReadAllLines(@"C:\stopWords.txt");

string[] text 数组包含文本段落,string[] stop_words 数组包含要从存储的所有文本中删除的停用词 string[] text数组

如何使用 c# 删除停用词。代码建议将不胜感激。

谢谢

【问题讨论】:

  • 这对我来说似乎是一个家庭作业......所以我会引导你朝着正确的方向前进。查看forwhile 循环(我假设您还没有使用LINQ)和Replace 类的Replace 方法。
  • 问题是如何将 stop_words 数组中的每个停用词与文本数组的每个文本进行匹配
  • 您可以有一个for-loop 遍历text 中的字符串,然后是一个内部for-loop,用于替换text 中当前字符串中的所有停用词.
  • 我已经检查了嵌套循环,它可以工作,但是如果字符串“i”被替换,那么它会在那里留下一个空格,如果我们必须删除下一个单词,即“am”,但在原始文本中删除“i”后返回为“am”,因此无法将stopword_list中的“am”与文本中的“am”匹配
  • 你能按照我的回答流程,现在告诉我它不适用于你的情况吗?

标签: c# text stop-words removeall


【解决方案1】:

让我解释一下流程:

1) 我必须遍历 input_Texts 字符串数组..fine.

2) 在循环中,我根据空格分割段落,即 (' '),以便我得到其中的所有单词。

3) 然后我找到它们与 stopWords 之间的所有相交/匹配词。

4) 然后取除匹配词以外的所有词。

5) 再次用空格将它们连接起来,从单词中创建文本(没有停止词)并再次将其放回相同的位置。

  for(int i=0;i<input_Texts.Length;i++)
  {
    input_Texts[i]=string.Join(" ", input_Texts[i].Split(' ').Except(input_Texts[i].Split(' ').Intersect(stopWords)));
  }

你能关注这个吗?

【讨论】:

    【解决方案2】:

    试试这样:

    string[] result = text.Except(stop_words).ToArray();
    

    或者你可以尝试使用for循环

    string[] stop_word = new string[] { "please", "try", "something" };
    
    string str = "Please try something by yourself before asking";
    foreach (string word in stop_word )
    {
       str = str.Replace(word, "");
    }
    

    【讨论】:

    • text 和 stop_words 都是字符串数组........所以不能像这里说的那样处理
    • 单个字符串,即“请在询问之前自己尝试一下”可以通过这种方法处理,但我有一个字符串数组,每个数组索引处都包含长文本段落
    • @user3495173:- 你有一个想法......现在努力尝试一下。相信我,这并不难做到。
    • 长文本段落造成什么问题,每一个又是一个字符串,可能是一个大字符串
    猜你喜欢
    • 2015-09-04
    • 2014-05-20
    • 2019-01-25
    • 2017-01-21
    • 2020-08-10
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多