C# 计算txt文件中唯一单词的数量和每个单词的出现次数

C# 计算txt文件中唯一单词的数量和每个单词的出现次数,c#,visual-studio,text-processing,C#,Visual Studio,Text Processing,目前,我正在尝试创建一个应用程序,对文本文件进行一些文本处理,然后使用字典创建单词索引,技术上是这样的。。程序将运行并读取一个文本文件,然后检查它,以查看该单词是否已经在该文件中,以及该单词的id作为唯一单词是什么。如果是这样,它将打印出每个单词的索引号和外观总数,并继续检查整个文件。生产出这样的产品: 下面是我输入的文本文件的一个示例:快速ctrl-F显示“not”出现2次,“that”出现4次。我需要做的是将每个单词编入索引并按如下方式调用: sample input : "that I h

目前,我正在尝试创建一个应用程序,对文本文件进行一些文本处理,然后使用字典创建单词索引,技术上是这样的。。程序将运行并读取一个文本文件,然后检查它,以查看该单词是否已经在该文件中,以及该单词的id作为唯一单词是什么。如果是这样,它将打印出每个单词的索引号和外观总数,并继续检查整个文件。生产出这样的产品:

下面是我输入的文本文件的一个示例:快速ctrl-F显示“not”出现2次,“that”出现4次。我需要做的是将每个单词编入索引并按如下方式调用:

sample input : "that I have not that place sunrise beach like not good dirty beach trash beach" 

    dictionary :            output.txt / output.dat:
    index word                     
      1    I                4:2 1:1 2:1 3:2 5:1 6:1 7:3 8:1 9:1 10:1 11:1
      2   have                   
      3   not                    
      4   that                   
      5   place                  
      6   sunrise
      7   beach
      8   like
      9   good
      10  dirty
      11  trash                  
我试图实现一些代码来创建字典。以下是我到目前为止的情况:

   private void bagofword_Click(object sender, EventArgs e)
            {
                //creating dictionary in background
                    //Dictionary<string, int> dict = new Dictionary<string, int>();
                    string rawinputbow = File.ReadAllText(textBox31.Text);
                    //string[] inputbow = rawinputbow.Split(' ');

                    var inputbow = rawinputbow.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                                   .ToList();
                    var dict = new OrderedDictionary();
                    var output = new List<int>();

                    foreach (var element in inputbow.Select((word, index) => new { word, index }))
                    {

                        if (dict.Contains(element.word))
                        {
                            var count = (int)dict[element.word];
                            dict[element.word] = ++count;
                            output.Add(GetIndex(dict, element.word));
                            //textBoxfile.Text = output.ToString();
                           // textBoxfile.Text = inputbow.ToString();
                            string result = string.Join(",", output);
                            textBoxfile.Text = result.ToString();
                        }
                        else
                        {
                            dict[element.word] = 1;
                            output.Add(GetIndex(dict, element.word));
                            //textBoxfile.Text = dict.ToString();
                            string result = string.Join(",", output);
                            textBoxfile.Text = result.ToString();
                        }

                    }
    }

    public int GetIndex(OrderedDictionary dictionary, string key)
            {
                for (int index = 0; index < dictionary.Count; index++)
                {
                    if (dictionary[index] == dictionary[key])                   
                        return index; // We found the item       
                        //textBoxfile.Text = index.ToString();
                }

                return -1;
            }
private void bagofword\u单击(对象发送者,事件参数e)
{
//在后台创建字典
//Dictionary dict=新字典();
字符串rawinputbow=File.ReadAllText(textBox31.Text);
//字符串[]inputbow=rawinputbow.Split(“”);
var inputbow=rawinputbow.Split(“.ToCharArray(),StringSplitOptions.RemoveEmptyEntries)
.ToList();
var dict=new OrderedDictionary();
var输出=新列表();
foreach(inputbow.Select中的var元素((单词,索引)=>new{word,index}))
{
if(dict.Contains(element.word))
{
var count=(int)dict[element.word];
dict[element.word]=++计数;
Add(GetIndex(dict,element.word));
//textBoxfile.Text=output.ToString();
//textBoxfile.Text=inputbow.ToString();
字符串结果=string.Join(“,”输出);
textBoxfile.Text=result.ToString();
}
其他的
{
dict[element.word]=1;
Add(GetIndex(dict,element.word));
//textBoxfile.Text=dict.ToString();
字符串结果=string.Join(“,”输出);
textBoxfile.Text=result.ToString();
}
}
}
public int GetIndex(OrderedDictionary,字符串键)
{
for(int index=0;index
有人知道如何完成代码吗?非常感谢您的帮助

使用此代码

  string input = "that I have not that place sunrise beach like not good dirty beach trash beach";
        var wrodList = input.Split(null);
        var output = wrodList.GroupBy(x => x).Select(x => new Word { charchter = x.Key, repeat = x.Count() }).OrderBy(x=>x.repeat);
        foreach (var item in output)
        {
            textBoxfile.Text += item.charchter +" : "+ item.repeat+Environment.NewLine;
        }
用于保存数据的类

 public class word
    {
        public string  charchter { get; set; }
        public int repeat { get; set; }
    }

仅在空格上拆分是不够的。你有一些词,比如庙宇,
照片。
咖啡馆/餐厅
。更好的方法是使用类似于
\w+
的正则表达式。此外,应以不区分大小写的方式比较单词

我的做法是:

var words = Regex.Matches(File.ReadAllText(filename), @"\w+").Cast<Match>()
            .Select((m, pos) => new { Word = m.Value, Pos = pos })
            .GroupBy(s => s.Word, StringComparer.CurrentCultureIgnoreCase)
            .Select(g => new { Word = g.Key, PosInText = g.Select(z => z.Pos).ToList() })
            .ToList();


foreach(var item in words)
{
    Console.WriteLine("{0,-15} POS:{1}", item.Word, string.Join(",", item.PosInText));
}


for (int i = 0; i < words.Count; i++)
{
    Console.Write("{0}:{1} ", i, words[i].PosInText.Count);
} 
var words=Regex.Matches(File.ReadAllText(文件名),@“\w+”).Cast()
.Select((m,pos)=>new{Word=m.Value,pos=pos})
.GroupBy(s=>s.Word,StringComparer.CurrentCultureInogoreCase)
.Select(g=>new{Word=g.Key,PosInText=g.Select(z=>z.Pos).ToList()})
.ToList();
foreach(var项目大写)
{
WriteLine(“{0,-15}POS:{1}”,item.Word,string.Join(“,”,item.PosInText));
}
for(int i=0;i
我几年前就做过这个家庭作业……
有人知道如何完成那个代码吗您的实际问题是什么?代码在做什么或不在做什么?您得到了哪些错误或意外输出?您不应该同时使用标点符号,以便句子末尾的某些内容与结尾的句点不符。您为什么在ASP.NET textbox控件上使用
文件。ReadAllText
?@DangerZone您介意与我共享吗?:)谢谢Arash jo,我已经尝试了上面的代码,但是出现了这个错误:“System.Linq.Enumerable+WhereSelectEnumerableInterator
2[System.Linq.iGroup
2[System.String,System.String],CobaTugasAkhir2.Form1+word]”。你有什么建议来解决这个问题吗(@Indiastradi你完全复制了代码吗?我的意思是没有任何更改?因为我检查了代码,它工作得很好!是的,我使用了相同的代码,也许我只是用以下代码更改了输入:string inputbow=File.ReadAllText(textBox31.Text);然后打印输出..这有什么问题吗?请再次检查您的输入。因为我不知道您的输入到底是什么,我无法帮助您。对于给定的输入,它对输出很有效。我是否应该使用类似“string.join(“,”,output);”?这是我到目前为止得到的:。这有什么问题吗?它给了我错误信息“PosInText.Count”。我应该为它插入一些引用吗?@Indiastradi
它给我错误
,等等,我将用我的水晶球查看你的代码和你得到的错误:)Eser:啊,对不起,我的意思是对话框说我可能忘记添加更多的指令或程序集引用。虽然这可能会回答问题,但请添加解释和/或描述
### Sample code for you to tweak for your needs:
touch test.txt
echo "ravi chandran marappan 30" > test.txt                                                                                                                                     
echo "ramesh kumar marappan 24" >> test.txt
echo "ram lakshman marappan 22" >> test.txt
sed -e 's/ /\n/g' test.txt | sort | uniq | awk '{print "echo """,$1,
"""`grep -wc ",$1," test.txt`"}' | sh

Results:                          
22 -1                                                                                                                                                         
24 -1                                                                                                                                                         
30 -1                                                                                                                                                         
chandran -1                                                                                                                                                   
kumar -1                                                                                                                                                      
lakshman -1                                                                                                                                                   
marappan -3                                                                                                                         
ram -1                                                                                                                            
ramesh -1                                                                                                                       
ravi -1