PHP 字符串的字长密度/计数计算
给定文本,我如何计算单词长度的密度/计数,以便得到如下输出
- 1 个字母单词:52 / 1%
- 2 个字母单词:34 / 0.5%
- 3 个字母单词:67 / 2%
找到这个但对于 python
Given a text, how could I count the density / count of word lengths, so that I get an output like this
- 1 letter words : 52 / 1%
- 2 letter words : 34 / 0.5%
- 3 letter words : 67 / 2%
Found this but for python
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以首先使用
explode()
< 将文本拆分为单词。 /strong> (作为一个非常/太简单的解决方案) 或preg_split()
(允许使用更强大的东西):Then, iterate over the words, getting, for each one of those, its length, using [**`strlen()`**][3] ; and putting those lengths into an array :
如果您正在工作对于 UTF-8,请参阅
mb_strlen()
.At the end of that loop, `$results` would look like this :
The total number of words, which you'll need to calculate the percentage, can be found either :
foreach
循环内的计数器,array_sum( )
在循环完成后的$results
上。对于百分比的计算,这是一些数学知识——我不会那么有帮助,关于这个^^
You could start by splitting your text into words, using either
explode()
(as a very/too simple solution) orpreg_split()
(allows for stuff that's a bit more powerful) :Then, iterate over the words, getting, for each one of those, its length, using [**`strlen()`**][3] ; and putting those lengths into an array :
If you're working with UTF-8, see
mb_strlen()
.At the end of that loop, `$results` would look like this :
The total number of words, which you'll need to calculate the percentage, can be found either :
foreach
loop,array_sum()
on$results
after the loop is done.And for the percentages' calculation, it's a bit of maths -- I won't be that helpful, about that ^^
您可以按空格分解文本,然后对于每个结果单词,计算字母数。如果存在标点符号或任何其他单词分隔符,则必须考虑到这一点。
ps:我还没有证明这一点,但应该可以
You could explode the text by spaces and then for each resulting word, count the number of letters. If there are punctuation symbols or any other word separator, you must take this into account.
ps: I have not proved this, but should work
使用 preg_replace 可以更聪明地删除标点符号。
You can be smarter about removing punctuation by using preg_replace.