esperecyan/dictionary-php

提供API,用于对传统字典格式和“主要用于单词答题游戏中的通用字典格式”进行语法解析和序列化。

v0.9.2 2020-01-26 00:41 UTC

README

提供API,用于解析以下游戏的字典,并实现相互转换。

如果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等。

要求

公共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\SyntaxExceptiongetMessage() 方法中获取用于显示给用户的错误消息。

日志记录

esperecyan\dictionary_php\Parser 实现了 PSR-3: Logger InterfacePsr\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 (字符串)
    • [audio] => array(字符串)
    • [audio-source] =>
      • [0] =>
        • [lml] => CommonMark (字符串)
        • [html] => HTML (字符串)
    • [video] => array(字符串, ……)
    • [video-source] =>
      • [0] =>
        • [lml] => CommonMark (字符串)
        • [html] => HTML (字符串)
    • [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\SyntaxExceptiongetMessage() 方法中获取用于显示给用户的错误消息。

日志记录

esperecyan\dictionary_php\Validator 实现 PSR-3: Logger InterfacePsr\Log\LoggerAwareInterface

贡献

请通过 Pull Request 或 Issue 提交。

语义化版本控制

此库采用 语义化版本控制。公共API如上所述。

许可

此脚本的许可证是 Mozilla Public License Version 2.0 (MPL-2.0)。