承接 bigwhoop/sentence-breaker 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bigwhoop/sentence-breaker

Composer 安装命令:

composer require bigwhoop/sentence-breaker

包简介

Sentence boundary disambiguation (SBD) - or sentence breaking - library written in PHP.

README 文档

README

Build Status

Sentence boundary disambiguation (SBD) - or sentence breaking - library written in PHP.

Installation

composer require bigwhoop/sentence-breaker

Usage

<?php
use Bigwhoop\SentenceBreaker\SentenceBreaker;

$breaker = new SentenceBreaker();
$breaker->addAbbreviations(['Dr', 'Prof']);

// returns a generator, the text is parsed lazily
$sentences = $breaker->split("Hello Dr. Jones! How are you? I'm fine, thanks!");

// get first
$sentences->current() // 'Hello Dr. Jones!'

// get all as array
iterator_to_array($sentences) // ['Hello Dr. Jones!', 'How are you?', "I'm fine, thanks!"]

Rules

By default the rules/rules.ini file is loaded. Its format is a list of patterns ...

TOKEN [... TOKEN] = PROBABILITY
T_CAPITALIZED_WORD <T_PERIOD> T_WHITESPACE T_CAPITALIZED_WORD = 75

The token enclosed in < / > is the one that defines for which token the pattern is applied. The example pattern above would be applied to each T_PERIOD token found in the input data. The probability defines how likely a sentence boundary is after this token.

So for this pattern to match, the input text would need to contain something along the lines of This is Waldo. He likes dogs..

The available tokens are:

Token Description Example
T_WORD A non-capitalized word. hello, world
T_CAPITALIZED_WORD A capitalized word. Hello, World
T_EOF The end of the input. -
T_PERIOD A period. .
T_EXCLAMATION_POINT An exclamation point. !
T_QUESTION_MARK A question mark. ?
T_QUOTED_STR A string enclosed in single or double quotes "Hello world!", 'Hello world...'
T_WHITESPACE Whitespace characters like spaces, LF, CR. -
T_ABBREVIATION An abbreviation without the trailing period. Dr, Prof

TIP: You can add your own rules via $breaker->addRules().

Abbreviation Providers

Inside the data directory are flat files containing abbreviations (in English), collected from various sources. They can be loaded like this:

use Bigwhoop\SentenceBreaker\Abbreviations\FlatFileProvider;

// Load legal.txt and biz.txt
$breaker->addAbbreviations(new FlatFileProvider('/path/to/data/directory', ['legal', 'biz']));

// Load all files
$breaker->addAbbreviations(new FlatFileProvider('/path/to/data/directory', ['*']));

To make it fast and easy, all abbreviations are available in the all.txt file. You can load it like this:

$breaker->addAbbreviations(new FlatFileProvider('/path/to/data/directory', ['all']));

How does it work?

The input text is run through a lexer.

In computer science, lexical analysis is the process of converting a sequence of characters into a sequence of tokens, i.e. meaningful character strings.

So for example He asked: "What's on TV?" On T.V.? I have no clue. Really! would result in the following sequence of tokens:

"He" "asked:" T_QUOTED_STR "On" "T.V" T_PERIOD T_QUESTION_MARK
"I" "have" "no" "clue" T_PERIOD "Really" T_EXCLAMATION_POINT

This sequence of tokens is then run through a probability calculator that calculates for each token the probability of it being the boundary of a sentence. The calculator uses rules that are matched against each token. For example if a T_EXCLAMATION_POINT is followed by a capitalized string the chance of it being a sentence boundary is 100%.

In the end the tokens are re-assembled into the sentences. The user can choose which threshold he wants to apply when starting new sentences. For example the probability must be greater or equal to 50% that a boundary was detected.

TODO

  • calculateCurrentTokenProbability is a big mess. Let's split it up into multiple Rule classes. Maybe use a rules engine.
  • Add abbreviations support for different languages.

Contributing

# Check code style
vendor/bin/php-cs-fixer fix --diff --dry-run

# Fix code style
vendor/bin/php-cs-fixer fix --diff

# Run tests
vendor/bin/phpunit

# Run static analysis
vendor/bin/phpstan

Contributors

Made with contrib.rocks.

License

MIT. See LICENSE file.

bigwhoop/sentence-breaker 适用场景与选型建议

bigwhoop/sentence-breaker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 137.47k 次下载、GitHub Stars 达 42, 最近一次更新时间为 2015 年 05 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 bigwhoop/sentence-breaker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bigwhoop/sentence-breaker 我们能提供哪些服务?
定制开发 / 二次开发

基于 bigwhoop/sentence-breaker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 137.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 42
  • 点击次数: 41
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 42
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-19