mbilbille/jpnforphp

A tiny PHP lib which provides nice functions & wrappers to interact with Japanese language.

Installs: 3 584

Dependents: 1

Stars: 25

Watchers: 4

Forks: 8

Open Issues: 4

Language: PHP

0.5.3 2015-03-05 08:57 UTC

README

A tiny PHP lib which provides nice functions & wrappers to interact with Japanese language. [Official Website]

Installation

The recommended way to install JpnForPhp is through Composer. Just create a composer.json file and run the php composer.phar install command to install it:

{
    "require": {
        "mbilbille/jpnforphp": "~0.5"
    }
}

Tests

To run the test suite, you need Composer:

    $ php composer.phar install --dev
    $ vendor/bin/phpunit

Features list

Provides the following components:

  • Helper: a set of function to help you interact with Japanese wordings (split, extract, clean, etc.).
  • Analyzer: various "inspecting" functions (length, count kana, etc.).
  • Transliterator: handle both transliterations roman to kana and kana to roman (support all mainstream romanization systems).

Usage

Helper component:

use JpnForPhp\Helper\Helper;

Helper::split('素晴らしいでしょう'); // array('素','晴','ら','し','い','で','す')
Helper::subString('素晴らしいでしょう', 2, 4); // 'らし'
Helper::extractKanji('素晴らしいでしょう'); // array('素晴')
Helper::extractHiragana('素晴らしいでしょう', 1, true); // array('ら','し','い','で','しょ','う')

Analyzer component

use JpnForPhp\Analyzer\Analyzer;

Analyzer::length('素晴らしいです'); // 7
Analyzer::inspect('素晴らしいです'); // array('length'=>7,'kanji'=>2,'hiragana' =>5,'katakana'=>0)
Analyzer::countHiragana('素晴らしいです'); // 5
Analyzer::hasKanji('素晴らしいです'); // TRUE

Transliterator component:

use JpnForPhp\Transliterator\Romaji;
use JpnForPhp\Transliterator\Kana;

$hepburn = new Romaji();
$kunrei = new Romaji('kunrei');
$hiragana = new Kana('hiragana');
$katakana = new Kana('katakana');

$hepburn->transliterate('ローマジ で かいて'); // rōmaji de kaite
$kunrei->transliterate('ローマジ で かいて'); // rômazi de kaite
$hiragana->transliterate('kana de kaite'); // かな で かいて
$katakana->transliterate('kana de kaite'); // カナ デ カイテ

Starting from the version 0.5, all the transliteration workflow is defined in .yaml file.

Romaji.php and Kana.php provides a wild range of functions which can be used to define your own transliteration system. Here is a sample .yaml file

id: mySystem
name:
    english: "My romanization system"
    japanese: "マイローマ字"
workflow:
    - function: transliterateDefaultCharacters
      parameters:
            mapping:
                あ: a
                い: i
                // [...]
                ぽ: po
                ゔ: pu
    - function: transliterateSokuon
      parameters:
            default: true
            hepburn: false
    - function: transliterateChoonpu
      parameters:
        macrons:
            a: aa
            i: ii
            u: uu
            e: ee
            o: oo

JpnForPhp supports the following standard transliteration system:

.yaml files for those transliteration systems are available here:

Upcoming

Check out the develop branch to get all the latest code and change (http://github.com/mbilbille/jpnforphp/tree/develop)

License

JpnForPHP was created by Matthieu Bilbille and released under the MIT License.

Integration

  • JPNlizer integrates JpnForPhp into Drupal - sandbox project [Deprecated]
  • JpnForPhpBundle: integrates JpnForPhp as a Symfony2 Bundle - JpnForPhpBundle (by @albertofem)

Sponsored by Openjisho.com.