当前位置:Gxlcms > PHP教程 > 英文分句/分段落

英文分句/分段落

时间:2021-07-01 10:21:17 帮助过:42人阅读

php环境,指出一个原本分句的时候的误区,分句不用考虑小数点,不用考虑域名,因为标准的句子是句号后面加空格的,唯一要考虑的就是Mr. Li 这种。
先采用分段落的方式是考虑到有些引用采用冒号结尾。
  1. /*TWWY'S ART*/
  2. function break_passage($text){ //分割段落
  3. return preg_split("/(\r|\n|\r\n)/", $text, -1, PREG_SPLIT_NO_EMPTY);
  4. }
  5. function break_sentence($text){ //分割句子 英文的句号后面必须有空格
  6. $re = '/# Split sentences on whitespace between them.
  7. (?<= # Begin positive lookbehind.
  8. [.!?] # Either an end of sentence punct,
  9. | [.!?][\'"] # or end of sentence punct and quote.
  10. ) # End positive lookbehind.
  11. (? Mr\. # Skip either "Mr."
  12. | Mrs\. # or "Mrs.",
  13. | Ms\. # or "Ms.",
  14. | Jr\. # or "Jr.",
  15. | Dr\. # or "Dr.",
  16. | Prof\. # or "Prof.",
  17. | Sr\. # or "Sr.",
  18. # or... (you get the idea).
  19. ) # End negative lookbehind.
  20. \s+ # Split on whitespace between sentences.
  21. /ix';
  22. $sentences = preg_split($re, $text, -1, PREG_SPLIT_NO_EMPTY);
  23. return $sentences;
  24. }
  25. function get_sentence($text){ //先分割段落再分割句子 [推荐]
  26. $passage = break_passage($text);
  27. $return = array();
  28. foreach ($passage as $key => $value) $return = array_merge($return, break_sentence($value));
  29. return $return;
  30. }
  31. ?>

人气教程排行