esperecyan / dictionary-php
提供API,用于对传统字典格式和“主要用于单词答题游戏中的通用字典格式”进行语法解析和序列化。
Requires
- php: >=7.1
- ext-exif: *
- ext-gd: *
- ext-gettext: *
- ext-intl: *
- ext-mbstring: *
- ext-spl: *
- ext-zip: *
- danielstjules/stringy: ^3.0.0
- esperecyan/html-filter: ^1.0.1
- esperecyan/url: ^5.0.0
- intervention/image: ^2.5.1
- james-heinrich/getid3: ^1.9.12
- league/commonmark: ^1.2.2
- league/html-to-markdown: ^4.2.0
- logue/igo-php: ^0.2.0
- mbilbille/jpnforphp: ^0.8.1
- nelexa/zip: ^3.1.4
- psr/log: ^1.0.0
- scriptfusion/byte-formatter: ^3.2.0
Requires (Dev)
- phpunit/phpunit: ^8.5.2
- dev-master
- v0.9.2
- v0.9.1
- v0.9.0
- v0.8.3
- v0.8.2
- v0.8.1
- v0.8.0
- v0.7.9
- v0.7.8
- v0.7.7
- v0.7.6
- v0.7.5
- v0.7.4
- v0.7.3
- v0.7.2
- v0.7.1
- v0.7.0
- v0.6.0
- v0.5.1
- v0.5.0
- v0.4.0
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.0
- v0.1.0
- dev-dependabot/composer/guzzlehttp/psr7-1.8.5
- dev-dependabot/composer/james-heinrich/getid3-1.9.21
- dev-esperecyan/dictionary-api
This package is auto-updated.
Last update: 2024-08-29 04:14:35 UTC
README
提供API,用于解析以下游戏的字典,并实现相互转换。
- 主要用于单词答题游戏中的通用字典格式(以下简称通用字典格式)
- キャッチフィーリング、Drawing Catch (*.cfq)
- きゃっちま (*.dat) ※加密后的文件无法处理
- Inteligenceω (*.txt, *.zip) ※加密后的文件无法处理
- ピクトセンス
如果Inteligenceω的字典中包含指向图像或音频文件的路径,则需要将包含这些文件的文件夹与dat文件一起归档。无法处理包含同名文件的字典(将同名文件分别存放在不同文件夹中的归档)。
示例
<?php require_once 'vendor/autoload.php'; use esperecyan\dictionary_php as dictionary; $file = new \SplTempFileObject(); $file->fwrite(mb_convert_encoding(<<<'EOD' % 選択問題 Q,2,,../images/sun.mp4 A,1,地球,カロン,太陽,\seikai Q,0,仲間外れはどれでしょう A,1,リンゴ,\seikai,ゴリラ,ラクダ,ダチョウ,\explain=選択肢を表示しなければ問題が成立しない場合。 % 答えが複数あり、どれか1つを選択すれば正解になる場合 Q,0,食べ物はどれでしょう (答えが複数ある場合はどれが1つだけ選択) A,1,リンゴ,\seikai,ゴリラ,ラッパ,パン,\seikai % 答えが複数あり、すべて選択する必要がある場合 Q,0,同じ種類のものを選びましょう A,3,リンゴ,\seikai,ゴリラ,ラッパ,パン,\seikai % 並べ替え問題 q,0,しりとりが成立するように並べ替えてください % 問題行と解答行の間のコメント行と空行 a,2,リンゴ,1,パン,4,ゴリラ,2,ラッパ,3 EOD , 'Windows-31J', 'UTF-8')); $parser = new dictionary\Parser(null, '選択・並べ替え問題.txt'); $dictionary = $parser->parse($file); $serializer = new dictionary\serializer\GenericDictionarySerializer(); $serializer->response($dictionary);
上面的示例输出如下。
text,image,answer,answer,description,specifics,question,option,option,option,option,type,@title
太陽,local/sun.png,太陽,,,,,地球,カロン,太陽,,selection,選択・並べ替え問題
リンゴ,,リンゴ,,選択肢を表示しなければ問題が成立しない場合。,,仲間外れはどれでしょう,リンゴ,ゴリラ,ラクダ,ダチョウ,selection,
「リンゴ」か「パン」,,リンゴ,パン,,,"食べ物はどれでしょう (答えが複数ある場合はどれが1つだけ選択)",リンゴ,ゴリラ,ラッパ,パン,selection,
「リンゴ」と「パン」,,リンゴ,パン,,require-all-right=,同じ種類のものを選びましょう,リンゴ,ゴリラ,ラッパ,パン,selection,
"リンゴ → ゴリラ → ラッパ → パン",,,,,,しりとりが成立するように並べ替えてください,リンゴ,ゴリラ,ラッパ,パン,selection,
安装
composer require esperecyan/dictionary-php
有关Composer的安装方法,请参阅Composer的全球安装 - Qiita等。
要求
- PHP 7.1以上
- php-mbstring (mbstring扩展模块)
- exif扩展模块
- php-intl (Intl扩展模块)
- php-gd (GD扩展模块)
- php-pecl-zip (Zip扩展模块)
公共API
class esperecyan\dictionary_php\Parser(string $from = null, string $filename = null, string $title = null)
语法解析器。
string $from = null
转换源字典格式。キャッチフィーリング
きゃっちま
Inteligenceω クイズ
Inteligenceω しりとり
ピクトセンス
通用字典
之一。
如果未指定或指定了错误的值,则从 $filename
判断。在这种情况下,Inteligenceω的判断方法是,除了注释行和空行之外,第一行是否以 Q,
开头。
string $filename = null
转换源文件名。
string $title = null
字典的标题。
如果未指定,则从 $filename
判断。
如果通用字典格式中存在 @title
字段,则忽略此指定。
Dictionary esperecyan\dictionary_php\Parser#parse(SplFileInfo $file, bool $header = null, string[] $filenames = [])
SplFileInfo $file
提供转换源的文件,使用 SplFileInfo 或其派生类。
bool $header = null
如果转换源是 通用字典
,则存在标题行则为 true
,不存在则为 false
,不明确则指定 null
。
string[] $filenames = []
如果转换源是 通用字典
,则提供ZIP文件的代替品,将CSV文件提供给 $file
并将文件名列表提供给此参数,可以解析包含图像、音频和视频文件的文件格式。
异常 esperecyan\dictionary_php\exception\SyntaxException
可以从 esperecyan\dictionary_php\exception\SyntaxException 的 getMessage()
方法中获取用于显示给用户的错误消息。
日志记录
esperecyan\dictionary_php\Parser 实现了 PSR-3: Logger Interface 的 Psr\Log\LoggerAwareInterface。
class esperecyan\dictionary_php\Serializer(string $to = '汎用辞書')
序列化器。
string $to = '汎用辞書'
转换目标字典格式。キャッチフィーリング
きゃっちま
Inteligenceω クイズ
Inteligenceω しりとり
ピクトセンス
通用字典
之一。
如果未指定或指定了错误的值,则默认为 通用字典
。
string[] esperecyan\dictionary_php\Serializer#serialize(Dictionary $dictionary, bool|string $csvOnly = false)
以以下结构的关联数组返回序列化后的数据。
- [bytes] => 序列化后的数据二进制字符串
- [type] => MIME型(包括charset参数等)
- 名称 => 文件名
字典 $dictionary
字典。
bool|string $csvOnly = false
当字典为 泛用字典
或 Inteligenceω 答案
时,若仅返回CSV文件、txt文件而不是ZIP文件,则设置为true。可以通过设置类似于 https://example.ne.jp/dictionaries/1/files/%s
的字符串,并将 %s
替换为文件名,将文件名写入字典文件中。
异常 esperecyan\dictionary_php\exception\SerializeExceptionInterface
从 SerializeExceptionInterface#getMessage() 中,可以获取给用户显示的错误消息。以下所有异常都实现了 SerializeExceptionInterface。
日志记录
class esperecyan\dictionary_php\Dictionary
字典数据。
(string|string[]|float)[][][] esperecyan\dictionary_php\Dictionary#getWords()
以类似以下结构的多次元数组返回题目列表。
- [0] =>
- [text] => array(字符串)
- [image] => array(字符串)
- [image-source] =>
- [0] =>
- [lml] => CommonMark (字符串)
- [html] => HTML (字符串)
- [0] =>
- [audio] => array(字符串)
- [audio-source] =>
- [0] =>
- [lml] => CommonMark (字符串)
- [html] => HTML (字符串)
- [0] =>
- [video] => array(字符串, ……)
- [video-source] =>
- [0] =>
- [lml] => CommonMark (字符串)
- [html] => HTML (字符串)
- [0] =>
- [answer] => array(字符串, ……)
- [description] => array(字符串)
- [weight] => array(浮点数)
- [specifics] => array(字符串)
- [question] => array(字符串)
- [option] => array(字符串, ……)
- [type] => array(字符串)
- [1] => ……
- [2] => ……
- ……
(string|string[])[] esperecyan\dictionary_php\Dictionary#getMetadata()
以类似以下结构的多次元数组返回元数据列表。
- [@title] => 字符串
- [@summary] =>
- [lml] => CommonMark (字符串)
- [html] => HTML (字符串)
- [@regard] => 字符串
FilesystemIterator esperecyan\dictionary_php\Dictionary#getFiles()
返回字典中包含的文件。
esperecyan\dictionary_php\Dictionary#setFiles(FilesystemIterator $files)
设置字典中包含的文件。每个文件都必须是有效的,并且所有文件名必须与 Parser#parse() 的第三个参数 $filenames 给定的文件名相匹配。
class esperecyan\dictionary_php\Validator()
字典中包含的文件的验证器。
string[] esperecyan\dictionary_php\Validator#correct(string|SplFileInfo $file, string $filename)
与 Serializer#serialize() 的返回值具有相同结构的返回值。
string|SplFileInfo $file
提供文件为二进制字符串、SplFileInfo 或其派生类之一。
string $filename
文件名。
异常 esperecyan\dictionary_php\exception\SyntaxException
可以从 esperecyan\dictionary_php\exception\SyntaxException 的 getMessage()
方法中获取用于显示给用户的错误消息。
日志记录
esperecyan\dictionary_php\Validator 实现 PSR-3: Logger Interface 的 Psr\Log\LoggerAwareInterface。
贡献
请通过 Pull Request 或 Issue 提交。
语义化版本控制
此库采用 语义化版本控制。公共API如上所述。
许可
此脚本的许可证是 Mozilla Public License Version 2.0 (MPL-2.0)。