van der Lee / syllable
使用Frank M. Liang的TeX算法进行文本音节分割和连字符化。
Requires
- php: >=5.6
- ext-dom: *
- ext-json: *
- ext-libxml: *
- ext-mbstring: *
Requires (Dev)
- ext-curl: *
- phpunit/phpunit: ^5.7 || ^6.5 || ^7.5 || ^8.5
README
版本 1.7
版权所有 © 2011-2023 Martijn van der Lee。适用MIT开源许可证。
介绍
PHP音节分割和连字符化。或者更确切地说... PHP Syl-la-ble split-ting 和 hy-phen-ation。
基于Frank M. Liang(http://www.tug.org/docs/liang/)的工作以及TeX社区中的许多志愿者。
支持多种语言。例如,英语(美/英)、西班牙语、德语、法语、荷兰语、意大利语、罗马尼亚语、俄语等,共76种语言。
语言来源:http://tug.org/tex-hyphen/#languages
支持PHP 5.6及以上版本,因此您可以在旧服务器上使用它。
安装
使用Composer安装phpSyllable
composer require vanderlee/syllable
或直接将phpSyllable添加到项目中,并设置项目自动加载器以使用phpSyllable的src/目录。
用法
实例化一个Syllable对象并开始连字符化。
最小示例
$syllable = new \Vanderlee\Syllable\Syllable('en-us'); echo $syllable->hyphenateText('Provide a plethora of paragraphs');
扩展示例
use Vanderlee\Syllable\Syllable; use Vanderlee\Syllable\Hyphen; // Globally set the directory where Syllable can store cache files. // By default, this is the cache/ folder in this package, but usually // you want to have the folder outside the package. Note that the cache // folder must be created beforehand. Syllable::setCacheDir(__DIR__ . '/cache'); // Globally set the directory where the .tex files are stored. // By default, this is the languages/ folder of this package and // usually does not need to be adapted. Syllable::setLanguageDir(__DIR__ . '/languages'); // Create a new instance for the language. $syllable = new Syllable('en-us'); // Set the style of the hyphen. In this case it is the "-" character. // By default, it is the soft hyphen "­". $syllable->setHyphen(new Hyphen\Dash()); // Set the minimum word length required for hyphenation. // By default, all words are hyphenated. $syllable->setMinWordLength(5); // Output hyphenated text .. echo $syllable->hyphenateText('Provide your own paragraphs...'); // .. or hyphenated HTML. echo $syllable->hyphenateHtmlText('<b>... with highlighted text.</b>');
查看demo.php文件以获取一个工作示例。
Syllable
API参考
以下描述了主Syllable类的API。在大多数情况下,您不会使用其他任何函数。在src/下浏览代码以查看所有可用的函数。
public __construct($language = 'en-us', string|Hyphen $hyphen = null)
创建一个新的Syllable类,带有默认设置。
public static setCacheDir(string $dir)
设置编译语言文件可能存储的目录。默认为当前目录的cache
子目录。
public static setEncoding(string|null $encoding = null)
设置要使用的字符编码。指定null
编码以不应用任何编码。
public static setLanguageDir(string $dir)
设置可以找到语言源文件的目录。默认为当前目录的languages
子目录。
public setLanguage(string $language)
设置用于连字符化的语言规则。
public setHyphen(mixed $hyphen)
设置用作连字符标记的连字符文本或对象。
public getHyphen(): Hyphen
获取当前的连字符对象。
public setCache(Cache $cache = null)
public getCache(): Cache
public setSource($source)
public getSource(): Source
public setMinWordLength(int $length = 0)
单词需要包含至少这么多字符才能进行连字符化。
public getMinWordLength(): int
public setLibxmlOptions(int $libxmlOptions)
用于libxml解析HTML的选项。查看: https://php.ac.cn/manual/de/libxml.constants.php.
public excludeAll()
排除所有元素。
public excludeElement(string|string[] $elements)
将一个或多个元素添加到要排除的HTML中。
public excludeAttribute(string|string[] $attributes, $value = null)
将一个或多个具有属性要排除的元素添加到HTML中。
public excludeXpath(string|string[] $queries)
将一个或多个xpath查询添加到要排除的HTML中。
public includeElement(string|string[] $elements)
将一个或多个元素添加到要包含的HTML中。
public includeAttribute(string|string[] $attributes, $value = null)
从HTML中包含一个或多个具有属性的元素。
public includeXpath(string|string[] $queries)
将一个或多个xpath查询添加到HTML中。
public splitWord(string $word): array
在连字符应放置的位置拆分单个单词。不支持标点符号,只有简单单词。请使用Syllable::splitWords()或Syllable::splitText()来解析整个句子。
public splitWords(string $text): array
将文本拆分为一个包含标点符号和单词的数组,在每个单词的连字符应放置的位置拆分。
public splitText(string $text): array
在连字符应放置的位置拆分文本。
public hyphenateWord(string $word): string
连字符化单个单词。
public hyphenateText(string $text): string
连字符化纯文本中的所有单词。
public hyphenateHtml(string $html): string
连字符化HTML中的所有可读文本,排除HTML标签和属性。 已弃用: 使用具有UTF-8能力的hyphenateHtmlText()代替。此方法仅用于向后兼容,将在下一个主要版本2.0中删除。
public hyphenateHtmlText(string $html): string
连字符化HTML中的所有可读文本,排除HTML标签和属性。此方法具有UTF-8能力,应优先于hyphenateHtml()。
public histogramText(string $text): array
计算文本中的音节数量,并返回一个映射,音节计数作为键,该音节计数的单词数量作为值。
public countWordsText(string $text): int
计算文本中的单词数量。
public countSyllablesText(string $text): int
计算文本中的音节数量。
public countPolysyllablesText(string $text): int
计算文本中的多音节单词数量。
开发
更新语言文件
运行
composer dump-autoload --dev
./build/update-language-files
以获取最新的语言文件远程,并可选地使用环境变量来自定义更新过程
CONFIGURATION_FILE
指定配置文件的绝对路径,其中定义了要下载的语言文件。配置文件具有以下格式
{
"files": [
{
"_comment": "<comment>",
"fromUrl": "<absolute-remote-file-url>",
"toPath": "<relative-local-file-path>",
"disabled": <true|false>
}
]
}
where the attributes are self-explanatory and _comment
and disabled
are optional. See for example build/update-language-files.json. Default: The build/update-language-files.json
file of this package.
MAX_REDIRECTS
指定在检索语言文件时允许的最大URL重定向数。默认值:1
。
WITH_COMMIT
从更新的语言文件创建(1)或跳过(0)Git提交。默认值:0
。
LOG_LEVEL
将脚本的详细程度设置为详细(6),警告和错误(4),仅错误(3)或静默(0)。默认值:6
。
例如,使用
composer dump-autoload --dev
LOG_LEVEL=0 ./build/update-language-files
在没有任何日志输出的情况下静默运行脚本。
更新API文档
运行
composer dump-autoload --dev
./build/generate-docs
以更新此README.md中的API文档。应在修改Syllable类时执行此操作。可选地,您可以使用环境变量来修改文档更新过程
WITH_COMMIT
创建(1)或跳过(0)从修改的文件创建Git提交。默认值:0
。
LOG_LEVEL
将脚本的详细程度设置为详细(6),警告和错误(4),仅错误(3)或静默(0)。默认值:6
。
创建发行版
运行
composer dump-autoload --dev
./build/create-release
通过向此README.md添加更改日志来创建项目的本地发行版。可选地,您可以使用环境变量来修改发行版过程
RELEASE_TYPE
将发行版类型设置为主要(0)、次要(1)或补丁(2)发布。默认值:2
。
WITH_COMMIT
创建(1)或跳过(0)从修改的文件创建Git提交并应用发行版标签。默认值:0
。
LOG_LEVEL
将脚本的详细程度设置为详细(6),警告和错误(4),仅错误(3)或静默(0)。默认值:6
。
测试
运行
composer install
./vendor/bin/phpunit
以执行测试。
更改
1.7
- 使用 \hyphenations 不区分大小写(如 \patterns)
- 使用新的Syllable::hyphenateHtmlText()时正确处理UTF-8字符集以连字符化HTML
- 将无效的 "en" 替换为 Syllable 的默认语言 "en-us"
- 更新 hyph-de.tex
1.6
- 撤销API方法名称重命名
- 将缓存版本作为字符串而不是数字使用
- 使用测试对封面缓存进行测试
- 将PHP测试矩阵减少到最新的PHP 5、7和8版本
- 通过GitHub Action检查API文档是否最新
- 更新API参考
- 修复将数组作为默认参数值的API文档
- 满足StyleCI要求
- 在构建上下文中提交整个工作树的变化文件
- 支持在README.md中生成API文档
- 从PR #26添加减少连字符的单词到集合中
- 满足StyleCI要求
- 添加减少连字符的单词集合的测试
- 重构Syllable类的splitWord()、splitWords()和splitText()
- 在测试中移除@covers注释
- 添加splitWords和多个代码质量改进
- 在发布时更新README.md的版权声明
- 在分叉中跳过GitHub Action调度程序,仅在PR上下文中运行测试
- 允许GitHub Action "更新语言"工作流绕过审查
- 使用2006年的德语正字法作为标准正字法
1.5.5
- 自动更新74种语言
1.5.4
- 自动对每次推送和拉取请求运行测试
- 自动每月更新和发布语言文件
- 修复README中的小错误,并在示例中添加'使用'。
- 使用与src/Source/File.php相同的代码格式
- 修复大括号
- 移除空格
- 修复闭合大括号
- 使用PHP语法高亮
1.5.3
- 通过@Dargmuesli修复了PHP 7.4兼容性问题(#37)。
1.5.2
- 通过@Dargmuesli修复了重构中撤销的bug(继续3)。
1.5.1
- 通过@Dargmuesli修复了重构中撤销的bug(继续2)。
1.5
- 重构以支持现代PHP和当前PHP版本。
1.4.6
- 添加
setMinWordLength($length)
和getMinWordLength()
以将连字符限制在至少指定数量的字符的单词上。
1.4.5
- 对composer的修复。
1.4.4
- 添加了composer自动加载器
1.4.3
- 改进文档
1.4.2
- 更新了西班牙语语言文件。
- 初始PHPDoc。
1.4.1
- 更多关于分割中的引号的修复。
1.4
- 修复了法语语言处理问题
- 将.text加载重构到源类中。
- 大幅提高缓存性能(过度写入)。
1.3.1
- 修复缓慢的初始缓存写入;写入太多(只需要一次)。
- 移除min_hyphenation;mb_strlen比hashmap查找花费更多时间。
1.3
- 添加了
array histogramText($text)
、integer countWordsText($text)
和integer countPolysyllableText($text)
方法。 - 重构缓存接口。
- 改进unittests。
1.2
- 弃用了阈值功能。该功能基于算法的错误解释。方法、常量和构造函数签名未更改,尽管你现在可以选择省略阈值(或保留它,它会被检测为“假”阈值)。