【问题标题】:stop words removal using arrays c#使用数组 c# 去除停用词
【发布时间】:2015-09-04 23:09:48
【问题描述】:

我有一个停止词的字符串数组和输入文本的字符串数组,即

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

con.Open();
SqlCommand query = con.CreateCommand();
query.CommandText = "select p_abstract from aminer_paper where pid between 1 and 500 and DATALENGTH(p_abstract) != 0";

SqlDataReader reader = query.ExecuteReader();

var summary = new List<string>();
while(reader.Read())
{
    summary.Add(reader["p_abstract"].ToString());
}

reader.Close();

string[] input_Texts = summary.ToArray();

现在,我必须使用这些 stopWords 数组从 input_Texts 数组中删除。 我使用了以下技术但没有工作,在访问两个数组索引时很奇怪。例如,在 input_Texts 数组的索引 0 处获取第一个文本,即

input_Texts[0]

然后匹配stopWords数组中的所有单词字符串,即

// have to match all the indexes of stopWords[] with input_Texts[0]
stopWords[]   

然后从input_Texts数组的索引0文本中删除所有stopWords后,必须对input_Texts数组中的所有文本重复此操作。

任何有修改的建议和代码示例将不胜感激。

谢谢。

【问题讨论】:

    标签: c# arrays stop-words removeall word-boundaries


    【解决方案1】:

    你可以使用 Linq 来做到这一点

            //string[] input_Text = new string[] { "Ravi Kumar", "Ravi Kumar", "Ravi Kumar" }; 
            //string[] stopWords = new string[] { "Ravi" }; 
            for(int i=0;i<input_Text.Count();i++)
            {
                for (int j = 0; j < stopWords.Count(); j++)
                {
                       input_Text[i] = input_Text[i].Replace(stopWords[j]," ");
                }
            }
    

    【讨论】:

    • 需要更多有关此解决方案的帮助
    【解决方案2】:
    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace StopWords_Removal
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
    
                    string[] stopWords = File.ReadAllLines(@"C:\stopWords.txt");
    
                    SqlConnection con = new SqlConnection("Data Source=ABC;Initial Catalog=xyz;Integrated Security=True");
    
                    con.Open();
                    SqlCommand query = con.CreateCommand();
                    query.CommandText = "select text from table where id between 1 and 500 and DATALENGTH(text) != 0";
    
                    SqlDataReader reader = query.ExecuteReader();
    
                    var summary = new List<string>();
                    while(reader.Read())
                    {
                        summary.Add(reader["p_abstract"].ToString());
                    }
    
                    reader.Close();
                    string[] input_Texts = summary.ToArray();
    
                    for (int i = 0; i < input_Texts.Length; i++)
                    {
                        for (int j = 0; j < input_Texts.Length; j++)
                        {
                            input_Texts[j] = string.Join(" ", input_Texts[j].Split(' ').Except(input_Texts[j].Split(' ').Intersect(stopWords)));
                        }
                    }
    
                    for (int d = 0; d < input_Texts.Length; d++)
                    {
                        Console.WriteLine(input_Texts[d]); 
                        Console.ReadLine();
                    }
    
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: " + e.Message);
                }
                finally
                {
                    Console.WriteLine("Executing finally block.");
                } 
            }
        }
    }
    

    【讨论】:

    • 你为什么使用嵌套循环...只使用一个循环,你正在使用 for(int j=0.....
    • 我写的不一样
    • 嵌套循环是因为我们必须将 stopWords 数组中的每个停用词与 input_Texts 数组中的每个文本进行比较
    • 查看这个问题 Mr. Tariq "stop words removal using c#",你可能对这个问题有更好的理解
    • 我已经更优雅地阐述了这个问题“使用 c# 删除停用词”你可能有自己的解决方案
    【解决方案3】:

    也可以这样:

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

    这将处理 input_Texts 中的每个文本并从中删除所有停用词。

    【讨论】:

    • 这只是显示相同的文本而没有任何删除
    • 代码有些多长,不能一下子显示出来
    【解决方案4】:

    试试这个:

    string[] result = input_Texts.Except(stopWords).ToArray();
    

    【讨论】:

      猜你喜欢
      • 2015-09-05
      • 2014-05-20
      • 2019-01-25
      • 2017-01-21
      • 2020-08-10
      • 1970-01-01
      • 2020-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多