C# 使用数组c停止字删除
我有一个停止字的字符串数组和输入文本的字符串数组,即C# 使用数组c停止字删除,c#,arrays,stop-words,removeall,word-boundaries,C#,Arrays,Stop Words,Removeall,Word Boundaries,我有一个停止字的字符串数组和输入文本的字符串数组,即 string[] stopWords = File.ReadAllLines(@"C:\stopWords.txt"); 及 然后匹配stopWords数组中的所有字串,即 // have to match all the indexes of stopWords[] with input_Texts[0] stopWords[] 然后在从input_Texts数组的索引0文本中删除所有停止字后,必须对input_Texts数组中的
string[] stopWords = File.ReadAllLines(@"C:\stopWords.txt");
及
然后匹配stopWords数组中的所有字串,即
// have to match all the indexes of stopWords[] with input_Texts[0]
stopWords[]
然后在从input_Texts数组的索引0文本中删除所有停止字后,必须对input_Texts数组中的所有文本重复该操作
任何修改后的建议和代码示例都将受到高度赞赏并予以确认
谢谢。您可以使用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]," ");
}
}
试试这个:
string[] result = input_Texts.Except(stopWords).ToArray();
也可以这样做:
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)));
}
这将处理输入文本中的每个文本,并删除其中的所有停止字。这只是显示相同的文本而没有任何删除代码是多少长度,不能立即显示为什么使用嵌套循环。。。只需使用一个循环,您使用的是forint j=0…..我写的是不同的测试循环,因为我们必须将stopWords数组中的每个停止字与输入文本数组中的每个文本进行比较检查这个问题Tariq先生使用c删除停止字,你可能对这个问题有了更好的理解。我已经更优雅地阐述了这个问题。停止使用c删除单词。你可能对此有自己的解决方案。请对此解决方案提供更多帮助
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 i=0;i<input_Texts.Length;i++)
{
input_Texts[i]=string.Join(" ", input_Texts[i].Split(' ').Except(input_Texts[i].Split(' ').Intersect(stopWords)));
}
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.");
}
}
}
}