【问题标题】:Count the number of unique words and occurrence of each word from txt file计算 txt 文件中唯一单词的数量和每个单词的出现次数
【发布时间】:2015-11-28 12:13:06
【问题描述】:

目前我正在尝试创建一个应用程序来进行一些文本处理以读取文本文件,然后我使用字典来创建单词索引,从技术上讲它会像这样......程序将运行并读取文本文件然后检查它,看看这个词是否已经在那个文件中,以及它的 id 词是什么作为唯一词。如果是这样,它将打印出他们遇到的每个单词的索引号和出现总数,并继续检查整个文件。并产生这样的东西:http://pastebin.com/CjtcYchF

这是我正在输入的文本文件的示例:http://pastebin.com/ZRVbhWhV 快速 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;
            }

有谁知道如何完成该代码?非常感谢任何帮助!

【问题讨论】:

  • 几年前我做了这个完全一样的作业问题......
  • Does anyone know how to complete that code? 您遇到的实际问题是什么?代码在做什么或不做什么?您遇到了哪些错误或意外输出?
  • 你不应该在标点符号上也分开,所以句尾的东西不会和句末的句号一起出现。
  • 为什么在 ASP.NET 文本框控件上使用 File.ReadAllText
  • @DangerZone 你介意与我分享吗? :)

标签: c# visual-studio text-processing


【解决方案1】:

使用此代码

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

【讨论】:

  • 谢谢 Arash jo,我已经尝试了上面的代码,但出现了这个错误:“System.Linq.Enumerable+WhereSelectEnumerableIterator2[System.Linq.IGrouping2[System.String,System.String],CobaTugasAkhir2.Form1 +字]“。那么有什么建议可以解决吗? :(
  • @Indiastradi 您是否完全复制了代码?,我的意思是没有任何更改?因为我检查了代码,它工作正常!
  • 是的,我使用相同的代码,也许我只是用这个代码更改输入:string inputbow = File.ReadAllText(textBox31.Text);然后打印输出..这有什么问题吗?
  • 再次检查您的输入。因为我不知道你的输入到底是什么,所以我帮不了你。给定的输入它工作正常
  • 对于输出我应该使用像这样的东西“string.join(”,,output);” ?到目前为止我所拥有的:pastebin.com/y7xZXVzt。这有什么问题吗?
【解决方案2】:

在空白处分割是不够的。你有一些像temple,photos.cafes/restaraunts这样的词。更好的方法是使用像\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);
} 

【讨论】:

  • 它给了我“PosInText.Count”的错误。我应该插入一些参考吗?
  • @Indiastradi it give me error,等一下,我会用我的水晶球看看你的代码和你得到什么错误:)
  • Eser :啊抱歉,我的意思是对话框说我可能忘记添加更多指令或程序集引用..
【解决方案3】:
### 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

【讨论】:

  • 虽然这可能会回答问题,但请添加解释和/或描述
猜你喜欢
  • 2015-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-20
  • 2019-04-05
  • 2021-04-19
  • 1970-01-01
相关资源
最近更新 更多