【问题标题】:Spliting sentences into paragraph based on numbers of words count根据字数将句子分成段落
【发布时间】:2014-06-30 11:41:29
【问题描述】:

我想将一个句子分成一个段落,每个段落的单词数应该少于。例如:

Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. 

Paragraph 1: 
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.

Paragraph 2: 
Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. 

在上面的例子中,少于 20 的单词在第 1 段中,其余的在第 2 段中。

有没有办法使用 php 实现这一点?

我尝试过$abc = explode(' ', $str, 20);,它将 20 个单词存储在一个数组中,然后将其余单词存储到最后一个数组 $abc['21']。如何从前 20 个数组中提取数据作为第一段,然后将其余的作为第二段?

【问题讨论】:

  • 你的最后一段'我试过......'完全错误,请改写。
  • 您可以尝试将字符串转换为数组,然后将前 20 个字符存储在一个字符串中,其余字符存储到另一个字符串中。
  • 只需在explode你的句子之后使用implode。 stackoverflow.com/questions/5956610/…
  • 你的第一段怎么少于20字?显然比您的示例中的更多。

标签: php html regex split sentence


【解决方案1】:

首先将字符串拆分为句子。然后循环遍历句子数组,首先将句子添加到段落数组中,然后计算段落数组的该元素中的单词,如果大于 19,则增加段落计数器。

$string = 'Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source.';

$sentences = preg_split('/(?<=[.?!;])\s+(?=\p{Lu})/', $string);

$ii = 0;
$paragraphs = array();
foreach ( $sentences as $value ) {
    if ( isset($paragraphs[$ii]) ) { $paragraphs[$ii] .= $value; }
    else { $paragraphs[$ii] = $value; }
    if ( 19 < str_word_count($paragraphs[$ii]) ) {
        $ii++;
    }
}
print_r($paragraphs);

输出:

Array
(
    [0] => Contrary to popular belief, Lorem Ipsum is not simply random text.It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.
    [1] => Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source.
)

在此处找到分句器:Splitting paragraphs into sentences with regexp and PHP

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 2013-05-28
    • 2011-01-10
    • 1970-01-01
    相关资源
    最近更新 更多