turanjanin/serbian-language-tools
Composer 安装命令:
composer require turanjanin/serbian-language-tools
包简介
Set of tools for tokenization, transliteration and diacritic restoration of a text written in Serbian language.
关键字:
README 文档
README
Serbian Language Tools is a PHP library for dealing with text written in Serbian language. It features:
- Tokenizer
- Diacritic restoration tool
- Transliterator between Serbian Cyrillic and Latin alphabets
- Alphabet detection
Requirements
This library requires PHP 7.4 or greater with sqlite3, intl and mbstring extensions.
Installation
You can install the package via composer:
composer require turanjanin/serbian-language-tools
Usage
In order to use the library, you need to tokenize the string. Tokenization is a process of splitting the string into a series of related characters. This library can recognize the following tokens: Word, Whitespace, URI (which includes URLs, hashtags and at-mentions), Interpunction, HTML and Emoticon.
Tokenizing can be achieved by creating a new instance of Text class using the named constructor:
use Turanjanin\SerbianLanguageTools\Text; $text = Text::fromString('Zdravo svete, ovo je primer teksta!');
Text object will now contain an array of various tokens that can be processed. You can use this object as any other PHP array since it implements ArrayAccess interface.
echo count($text) . "\n"; // 13 echo get_class($text[1]). "\n"; // Turanjanin\SerbianLanguageTools\Tokens\Whitespace echo $text[9] . "\n"; // primer
Diacritic Restoration / Diacritization
Serbian Latin alphabet includes a couple of specific characters that are not found in ASCII encoding table. These characters feature diacritics - č, ć, š, ž, dž, đ - which are often omitted in everyday communication (social media, emails and SMS), mainly due to the widespread usage of English keyboard layouts.
This degraded Latin alphabet can be easily understood by human readers but it poses significant challenge for search engines and natural language processing. Therefore, this library features an algorithm that allows automated restoration of ASCII text by using a dictionary of Serbian words and phrases for context disambiguation.
The algorithm inspects all Word tokens and looks for restoration candidates - the words with s, c, z or dj characters. After that, the following two steps are applied:
-
The most common phrases are searched for inside the text and, if found, words are replaced with their diacritical equivalents. This step takes word context into consideration which allows us to give advantage to some less used variations. For example,
sto hiljadawon't be replaced withšto hiljada, even though the formšto(why) has much greater frequency compared to wordsto(hundred). -
Every restoration candidate is looked up in the dictionary and, if there are known variations, token is replaced with
RestoredWord(if there is only one possible variation) orMultipleRestoredWord(if there are more possible variations). In case of more than one variation, the one with the highest frequency will be marked as preferred.
Diacritic restoration can be performed by calling the invokable class:
use Turanjanin\SerbianLanguageTools\Text; use Turanjanin\SerbianLanguageTools\Transformers\DiacriticRestorer; $text = Text::fromString('Cetiri cavke cuceci dzangrizavo cijucu u zeleznickoj skoli.'); echo (new DiacriticRestorer)($text); // Četiri čavke čučeći džangrizavo cijuču u železničkoj školi.
Dictionary needed for this algorithm is stored in custom-made SQLite database that is included with this library. You can extend this database or use different storage solution by providing custom implementation of Turanjanin\SerbianLanguageTools\Dictionary\Dictionary interface.
Transliteration
Library supports transliteration of text between Cyrillic, Latin and ASCII alphabets. Transliteration can be performed by calling appropriate invokable class:
use Turanjanin\SerbianLanguageTools\Text; use Turanjanin\SerbianLanguageTools\Transformers\ToAsciiLatin; use Turanjanin\SerbianLanguageTools\Transformers\ToCyrillic; use Turanjanin\SerbianLanguageTools\Transformers\ToLatin; $cyrillic = Text::fromString('Ово је ћирилични текст'); $latin = Text::fromString('Primer latiničnog teksta'); echo (new ToLatin)($cyrillic); // Ovo je ćirilični tekst echo (new ToCyrillic)($latin); // Пример латиничног текста echo (new ToAsciiLatin)($cyrillic); // Ovo je cirilicni tekst
If you need only transliteration between Latin and Cyrillic alphabets, take a look at the simpler library - turanjanin/serbian-transliterator.
Alphabet Detection
Library can be used to detect if text is written in Serbian Cyrillic or Latin alphabet:
use Turanjanin\SerbianLanguageTools\Text; Text::fromString('Ovo je latinica')->isLatin(); // true Text::fromString('Ovo je latinica')->isCyrillic(); // false
Author
License
The MIT License (MIT). Please see License File for more information.
turanjanin/serbian-language-tools 适用场景与选型建议
turanjanin/serbian-language-tools 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.36k 次下载、GitHub Stars 达 19, 最近一次更新时间为 2021 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「tokenizer」 「transliteration」 「cyrillic」 「Serbian」 「latin」 「diacritics」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 turanjanin/serbian-language-tools 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 turanjanin/serbian-language-tools 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 turanjanin/serbian-language-tools 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A simple library to sanitize text elements
Library emulating the PHP internal reflection using just the tokenized source code.
i18n helpers (transliteration)
A program for performing lexical analysis, written in PHP
A simple library for make Lexer and Parsers to build a language
Wrapper around PHP's tokenizer extension.
统计信息
- 总下载量: 2.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 19
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-24