vanderlee/php-sentence
Composer 安装命令:
composer require vanderlee/php-sentence
包简介
Simple text sentence splitting and counting. Supports at least english, german and dutch, possibly more.
README 文档
README
Version 1.0.8
Copyright © 2016-2024 Martijn van der Lee (@vanderlee), parts copyright © 2017 @marktaw.
MIT Open Source license applies.
Introduction
PHP natural language sentence segmentation (splitting) and counting. Sentence boundary disambiguation.
Still early, but should support most western languages. If you find any problems, please let me know.
Supports PHP 5.3 and up, so you can use it on older servers.
Installation
Requires PHP 5.4 or greater. PHP 5.3 is supported as long as no more recent features are absolutely necessary.
To install using Composer:
composer require vanderlee/php-sentence
Methods
integer count(string $text)
Counts the number of sentences in the text.
Provided for convenience; this is exactly the same as counting the number of
returned array items from split, so if you need both results, just do that.
array split(string $text, integer $flags = 0)
Splits the text into sentences.
$flags is zero (0, default) or the following class constant:
Sentence::SPLIT_TRIM: Trim whitespace off the left and right sides of each returned sentence.
Documentation
You can find documentation generated from the source code by ApiGen here: ApiGen documentation
Examples
<?php
// This is the test text we're going to use
$text = "Hello there, Mr. Smith. What're you doing today... Smith,"
. " my friend?\n\nI hope it's good. This last sentence will"
. " cost you $2.50! Just kidding :)";
// Create a new instance
$Sentence = new \Sentence;
// Split into array of sentences
$sentences = $Sentence->split($text);
// Count the number of sentences
$count = $Sentence->count($text);
?>
How it works
The method used is not based on any on the established or published methods. It seems to work pretty well, though.
The method follows a number of simple steps in splitting and re-merging the text into full sentences. You can easily check the steps in the code.
Though the splitting may be a bit off, in particular abbreviations at the start of sentences tend to be merged with the preceding sentences. In most ordinary text this should pose no problem. In either case this should not affect the sentence count except in very uncommon situations.
It should be noted that this algorithm depends on reasonably gramatically correct punctuation. Do not L33t-5p3ak!!!!!1!1!11!eleven!!
Rules
The following is a rough list of the rules used to split sentences.
- Each linebreak separates sentences.
- The end of the text indicates the end if a sentence if not otherwise ended through proper punctuation.
- Sentences must be at least two words long, unless a linebreak or end-of-text.
- An empty line is not a sentence.
- Each question- or exclamation mark or combination thereof, is considered the end of a sentence.
- A single period is considered the end of a sentence, unless...
- It is preceded by one word, or...
- It is followed by one word.
- A sequence of multiple periods is not considered the end of a sentence.
vanderlee/php-sentence 适用场景与选型建议
vanderlee/php-sentence 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 599.78k 次下载、GitHub Stars 达 84, 最近一次更新时间为 2015 年 09 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「split」 「count」 「segmentation」 「sentence」 「boundary」 「disambiguation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vanderlee/php-sentence 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vanderlee/php-sentence 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vanderlee/php-sentence 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The "View Counter" bundle
A server-side A/B/n testing tool
mobilpay split
A/B Testing of content elements
Analyze user input string, and split it into Russian first name, second name and last name
Official PHP client (SDK) for the Newsdata.io News API — fetch real-time, historical, crypto, and stock-market news via REST with validation, retries, and error handling.
统计信息
- 总下载量: 599.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 85
- 点击次数: 27
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-05