计算重复的单词 [英] Count duplicate word

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

问题描述

大家好

我正在尝试编写一个获得句子的代码,我需要计算每个单词的频率。例如:

是或不是

结果shuold是:

to2

be2

或1

不是1

我甚至都在努力争取一个算法。

我不是要求你提供100%的解决方案,但我会很感激你的建议或重定向。

THX。

Hi guys
i''m trying to write a code that gets a sentence, and i need to count the frequency for each word. for example:
"to be or not to be"
the result shuold be:
"to" 2
"be" 2
"or" 1
"not" 1
i''m really struggle to even wrigt an algorithem.
i''m not asking from you an 100% solution but i''ll appreciate a good advice or redirection.
THX.

推荐答案

这并不复杂:但它有点费力。



最简单的方法是做几次通过:

1)处理输入并将其分解为单独的单词数组。假设它只是一个句子,那不是太糟糕 - 只需将它分成空格,逗号,冒号,分号和括号,并丢弃拆分字符和任何空白条目。同时,将所有字符转换为小写。所以成为或不成为。将成为六个单词的数组:

It''s not complex: but it is a bit laborious.

The simplest way is to do several passes:
1) Process the input and break it into an array of separate words. Assuming it''s a single sentence, that''s not too bad - just split it at space, comma, colon, semicolon, and brackets and discard the split character and any blank entries. At the same time, convert all characters to lower case. So "To be, or not to be." would become an array of six words:
to
be
or
not
to
be



2)对数组进行排序。然后该示例变为:


2) Sort the array. The example then becomes:

be
be
not
or
to
to



3)现在你所要做的就是遍历数组,将数值与下一个进行比较。如果它们相同,则递增计数。如果他们不是,你有这个词和它的数量,并且可以将计数重新设置为下一次。



每个部分都很简单on - 只需一次实现一个,然后随时测试。不要继续前进,直到现阶段工作!它根本不应该花太长时间......:笑:


3) Now all you have to do is loop through the array, comparing values with the next. If they are the same, increment a count. If they aren''t, you have the word and its count, and can set the count back to one for next time.

Each part is pretty simple to work on - just implement them one at a time, and test it as you go. Don''t move on until the current stage works! It shouldn''t take you too long at all...:laugh:


Solution1非常好。

我会分享一个例子:如何比较数组中的元素(C) [ ^ ]
Solution1 is very good.
I would share with an example: How to Compare elements in arrays (C)[^]


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

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