PHP 字符串的字长密度/计数计算

发布于 2024-11-24 15:42:36 字数 374 浏览 1 评论 0原文

给定文本,我如何计算单词长度的密度/计数,以便得到如下输出

  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

病毒体 2024-12-01 15:42:36

您可以首先使用 explode()< 将文本拆分为单词。 /strong> (作为一个非常/太简单的解决方案)preg_split() (允许使用更强大的东西)

$text = "this is some kind of text with several words";
$words = explode(' ', $text);

Then, iterate over the words, getting, for each one of those, its length, using [**`strlen()`**][3] ; and putting those lengths into an array :

$results = array();
foreach ($words as $word) {
    $length = strlen($word);
    if (isset($results[$length])) {
        $results[$length]++;
    }
    else {
        $results[$length] = 1;
    }
    
}

如果您正在工作对于 UTF-8,请参阅 mb_strlen().

At the end of that loop, `$results` would look like this :

array
  4 => int 5
  2 => int 2
  7 => int 1
  5 => int 1

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) or preg_split() (allows for stuff that's a bit more powerful) :

$text = "this is some kind of text with several words";
$words = explode(' ', $text);

Then, iterate over the words, getting, for each one of those, its length, using [**`strlen()`**][3] ; and putting those lengths into an array :

$results = array();
foreach ($words as $word) {
    $length = strlen($word);
    if (isset($results[$length])) {
        $results[$length]++;
    }
    else {
        $results[$length] = 1;
    }
    
}

If you're working with UTF-8, see mb_strlen().

At the end of that loop, `$results` would look like this :

array
  4 => int 5
  2 => int 2
  7 => int 1
  5 => int 1

The total number of words, which you'll need to calculate the percentage, can be found either :

  • By incrementing a counter inside the foreach loop,
  • or by calling 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 ^^

思慕 2024-12-01 15:42:36

您可以按空格分解文本,然后对于每个结果单词,计算字母数。如果存在标点符号或任何其他单词分隔符,则必须考虑到这一点。

$lettercount = array();
$text = "lorem ipsum dolor sit amet";
foreach (explode(' ', $text) as $word)
{
  @$lettercount[strlen($word)]++; // @ for avoiding E_NOTICE on first addition
}

foreach ($lettercount as $numletters => $numwords)
{
  echo "$numletters letters: $numwords<br />\n";
}

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.

$lettercount = array();
$text = "lorem ipsum dolor sit amet";
foreach (explode(' ', $text) as $word)
{
  @$lettercount[strlen($word)]++; // @ for avoiding E_NOTICE on first addition
}

foreach ($lettercount as $numletters => $numwords)
{
  echo "$numletters letters: $numwords<br />\n";
}

ps: I have not proved this, but should work

听风吹 2024-12-01 15:42:36

使用 preg_replace 可以更聪明地删除标点符号。

$txt = "Sean Hoare, who was first named News of the World journalist to make hacking allegations, found dead at Watford home. His death is not being treated as suspiciou";

$txt = str_replace( "  ", " ", $txt );
$txt = str_replace( ".", "", $txt );
$txt = str_replace( ",", "", $txt );

$a = explode( " ", $txt );

$cnt = array();
foreach ( $a as $b )
{
  if ( isset( $cnt[strlen($b)] ) )
    $cnt[strlen($b)] += 1;
  else
    $cnt[strlen($b)] = 1;
}

foreach ( $cnt as $k => $v )
{
  echo $k . " letter words: " . $v . " " . round( ( $v * 100 ) / count( $a ) ) . "%\n";
}

You can be smarter about removing punctuation by using preg_replace.

$txt = "Sean Hoare, who was first named News of the World journalist to make hacking allegations, found dead at Watford home. His death is not being treated as suspiciou";

$txt = str_replace( "  ", " ", $txt );
$txt = str_replace( ".", "", $txt );
$txt = str_replace( ",", "", $txt );

$a = explode( " ", $txt );

$cnt = array();
foreach ( $a as $b )
{
  if ( isset( $cnt[strlen($b)] ) )
    $cnt[strlen($b)] += 1;
  else
    $cnt[strlen($b)] = 1;
}

foreach ( $cnt as $k => $v )
{
  echo $k . " letter words: " . $v . " " . round( ( $v * 100 ) / count( $a ) ) . "%\n";
}
白馒头 2024-12-01 15:42:36
My simple way to limit the number of words characters in some string with php.


function checkWord_len($string, $nr_limit) {
$text_words = explode(" ", $string);
$text_count = count($text_words);
for ($i=0; $i < $text_count; $i++){ //Get the array words from text
// echo $text_words[$i] ; "
//Get the array words from text
$cc = (strlen($text_words[$i])) ;//Get the lenght char of each words from array
if($cc > $nr_limit) //Check the limit
{
$d = "0" ;
}
}
return $d ; //Return the value or null
}

$string_to_check = " heare is your text to check"; //Text to check
$nr_string_limit = '5' ; //Value of limit len word
$rez_fin = checkWord_len($string_to_check,$nr_string_limit) ;

if($rez_fin =='0')
{
echo "false";
//Execute the false code
}
elseif($rez_fin == null)
{
echo "true";
//Execute the true code
}

?>
My simple way to limit the number of words characters in some string with php.


function checkWord_len($string, $nr_limit) {
$text_words = explode(" ", $string);
$text_count = count($text_words);
for ($i=0; $i < $text_count; $i++){ //Get the array words from text
// echo $text_words[$i] ; "
//Get the array words from text
$cc = (strlen($text_words[$i])) ;//Get the lenght char of each words from array
if($cc > $nr_limit) //Check the limit
{
$d = "0" ;
}
}
return $d ; //Return the value or null
}

$string_to_check = " heare is your text to check"; //Text to check
$nr_string_limit = '5' ; //Value of limit len word
$rez_fin = checkWord_len($string_to_check,$nr_string_limit) ;

if($rez_fin =='0')
{
echo "false";
//Execute the false code
}
elseif($rez_fin == null)
{
echo "true";
//Execute the true code
}

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文