Last active
December 20, 2015 12:09
-
-
Save mgng/6128903 to your computer and use it in GitHub Desktop.
PHPで簡易分かち書き(形態素分解)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$src = 'こんにちは、私の名前はホゲホゲです。ツイッターやってます!https://twitter.com/hogehoge'; | |
$pattern = '/' . implode( '|', array( | |
'[\x{2E80}-\x{2FDF}\x{3005}\x{3007}\x{3021}-\x{3029}\x{3038}-\x{303B}\x{3400}-\x{4DBF}\x{4E00}-\x{9FFF}\x{F900}-\x{FAFF}\x{20000}-\x{2FFFF}]+', | |
'[\x{3041}-\x{309F}\x{31F0}-\x{31FF}\x{1B000}\x{1B001}]+', | |
'[\x{30A0}-\x{30FF}\x{FF65}-\x{FF9F}]+', | |
'[\x{FF10}-\x{FF19}\x{FF21}-\x{FF3A}\x{FF41}-\x{FF5A}]+', | |
'[\x{0020}-\x{007D}\x{203E}\s\r\n\t]+', | |
'[^\x{0020}-\x{007D}\x{203E}\s\r\n\t\x{FF10}-\x{FF19}\x{FF21}-\x{FF3A}\x{FF41}-\x{FF5A}\x{30A0}-\x{30FF}\x{FF65}-\x{FF9F}\x{3041}-\x{309F}\x{31F0}-\x{31FF}\x{1B000}\x{1B001}\x{2E80}-\x{2FDF}\x{3005}\x{3007}\x{3021}-\x{3029}\x{3038}-\x{303B}\x{3400}-\x{4DBF}\x{4E00}-\x{9FFF}\x{F900}-\x{FAFF}\x{20000}-\x{2FFFF}]+', | |
) ) . '/us'; | |
preg_match_all( $pattern, $src, $matchs ); | |
print_r( $matchs ); | |
/* | |
Array | |
( | |
[0] => Array | |
( | |
[0] => こんにちは | |
[1] => 、 | |
[2] => 私 | |
[3] => の | |
[4] => 名前 | |
[5] => は | |
[6] => ホゲホゲ | |
[7] => です | |
[8] => 。 | |
[9] => ツイッター | |
[10] => やってます | |
[11] => ! | |
[12] => https://twitter.com/hogehoge | |
) | |
) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment