PHP code example of ukkz / php-mecab-wrapper
1. Go to this page and download the library: Download ukkz/php-mecab-wrapper library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ukkz / php-mecab-wrapper example snippets
$mecab_sentence_class = new MeCab\Sentence("解析したい日本語文章", 辞書ディレクトリのパス);
$mecab_word_class_generator = $mecab_sentence_class->getWord();
foreach ($mecab_word_class_generator as $mecab_word_class) {
// 形態素ごとの処理など
echo $mecab_word_class . '/';
}
// 出力は "解析/し/たい/日本語/文章/" となります。
use MeCab\Sentence as MeCabSentence;
�を聞いて人を知らぬと云うことが随分ある。人ばかりではない。すべての物にある。';
$sample_sentence = new MeCabSentence($original_text);
// すべてカタカナにする
echo $sample_sentence->toKana() . "\n";
# "ナヲキイテヒトヲシラヌトイウコトガズイブンアル。ヒトバカリデハナイ。スベテノモノニアル。"
// 名詞だけ括弧でくくる
foreach ($sample_sentence->getWord() as $sample_word) {
if ($sample_word->class() === '名詞') {
echo '「' . $sample_word . '」';
} else {
echo $sample_word;
}
}
# 「名」を聞いて「人」を知らぬと云う「こと」が随分ある。「人」ばかりではない。「すべて」の「物」にある。