我如何找到最重复的单词 [英] How do I find the most repeated word

查看:230
本文介绍了我如何找到最重复的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿。我是C#的新手,我想问一下如何找到最常用的词。例如,我有一个.txt文件,其中我有3个单词:Human; Mage; Human。我如何发现人类使用得最多,如何归还?



我尝试过:



我还不熟悉foreach循环,这些值存储在列表中。所以任何帮助都是好的。抱歉我的英语能力差..

Hey. I'm new to C# and i wanted to ask how do I find a word who has been the most used. For example, i have a .txt file in which i have 3 words: Human;Mage;Human. How do I find that Human has been used the most and how do i return it ?

What I have tried:

Im not yet familiar with foreach loop and these values are stored in a list. So any help is good. Sorry for my bad english skills..

推荐答案

与任何任务一样,将其分解为较小的位。

1)读取你的文件

2)将其分解为单词。

3)识别不同/重复的单词

4)统计它们。

)找到最高计数。



在每个阶段,在开始之前先获得书面,编码,测试和完善之前的那些。



现在,第一个我假设你知道怎么做。

第二个(给出你的样本数据)很简单:看看 String.Split方法(系统)| Microsoft Docs [ ^ ]

第三个更复杂,并且有很多方法可以做到这一点。

第四个可能是一旦你完成了第三次工作,最容易完成第三次工作。

完成其余工作后,第五项工作非常简单。



我该怎么办?我会使用Linq,以及一行3,4和5的代码 - 但是如果你还不熟悉循环那么你的作业就不会有任何好处!



所以:设置一对数组,一个持有字符串,一个持有整数(或者更好,一个持有字符串和整数的类,然后如果你已经涵盖了它,则创建一个数组)。

循环你的单词,看看每个单词是否已经在字符串数组中。如果不是,请添加它。

当测试和工作时,添加一个计数器:当你将单词添加到集合时,将相关的整数设置为1,如果它已经存在则增加它。 br />
最后,寻找整数集合中的最高数字,并打印相关的单词。



这并不复杂,不是真的 - 但它是你的作业,所以我不会给你任何代码!
As with any task, break it down into smaller bits.
1) Read your file
2) Break it into words.
3) Identify different / duplicated words
4) Count them.
5) Find the highest count.

At each stage, get the ones before written, coded, tested , and perfect before moving on.

Now, the first I'm going to assume you know how to do.
The second (given your sample data) is simple: look at the String.Split Method (System) | Microsoft Docs[^]
The third is more complicated, and there are a whole load of ways to do it.
The fourth is probably most easily done as part of the third, once you have the third working.
The fifth is pretty trivial when the rest is done.

How would I do it? I'd use Linq, and a single line of code for 3, 4, and 5 all together - but that won't get you anywhere with your homework if you aren't good with loops yet!

So: Set up a pair of arrays, one holding strings, one holding integers (or better, a class holding a string and an integer then create an array of them if you've covered that yet).
Loop through your words, and see if each one is in the strings array already. If it aren't, add it.
When that's tested and working, add a counter: set the relevant integer to one when you add the word to the collection, increment it if it's already there.
Finally, look for the highest number in the integers collection, and print the associated word.

It's not complicated, not really - but it's your homework, so I'll give you no code!


这篇关于我如何找到最重复的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆