| 1 | package Lingua::JA::NormalizeText; | ||||||||||
| 2 | |||||||||||
| 3 | 37 | ▶ | use 5.008_001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 37 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 37 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 37 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 37 | ▶ | use Carp (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 37 | ▶ | use Exporter qw/import/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 37 | ▶ | use Sub::Install (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 37 | ▶ | use Unicode::Normalize (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 37 | ▶ | use HTML::Entities (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 37 | ▶ | use HTML::Scrubber (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 37 | ▶ | use Lingua::JA::Regular::Unicode (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 37 | ▶ | use Lingua::JA::Dakuon (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 37 | ▶ | use Lingua::JA::Moji (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 37 | our $VERSION = '0.33_01'; | |||||||||
| 19 | 37 | our @EXPORT = qw(); | |||||||||
| 20 | 37 | our @EXPORT_OK = qw(nfkc nfkd nfc nfd decode_entities strip_html | |||||||||
| 21 | alnum_z2h alnum_h2z space_z2h space_h2z katakana_z2h katakana_h2z | ||||||||||
| 22 | katakana2hiragana hiragana2katakana wave2tilde tilde2wave | ||||||||||
| 23 | wavetilde2long wave2long tilde2long fullminus2long dashes2long | ||||||||||
| 24 | drawing_lines2long unify_long_repeats nl2space unify_long_spaces | ||||||||||
| 25 | unify_whitespaces unify_nl trim ltrim rtrim old2new_kana old2new_kanji | ||||||||||
| 26 | tab2space remove_controls remove_spaces dakuon_normalize | ||||||||||
| 27 | handakuon_normalize all_dakuon_normalize | ||||||||||
| 28 | square2katakana circled2kana circled2kanji | ||||||||||
| 29 | remove_DFC); | ||||||||||
| 30 | |||||||||||
| 31 | 37 | our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_OK ] ); | |||||||||
| 32 | |||||||||||
| 33 | 37 | my %AVAILABLE_OPTS; | |||||||||
| 34 | 37 | @AVAILABLE_OPTS{ (qw/lc uc/, @EXPORT_OK) } = (); | |||||||||
| 35 | |||||||||||
| 36 | 37 | our $SCRUBBER = HTML::Scrubber->new; | |||||||||
| 37 | |||||||||||
| 38 | # This does not work on Perl 5.8.8 | ||||||||||
| 39 | # - couldn't find subroutine named lc in package CORE | ||||||||||
| 40 | # - Undefined subroutine &CORE::lc called | ||||||||||
| 41 | #Sub::Install::install_sub({ code => 'lc', from => 'CORE', as => 'lc' }); | ||||||||||
| 42 | #Sub::Install::install_sub({ code => 'uc', from => 'CORE', as => 'uc' }); | ||||||||||
| 43 | #Sub::Install::install_sub({ code => \&CORE::lc, as => 'lc' }); | ||||||||||
| 44 | #Sub::Install::install_sub({ code => \&CORE::uc, as => 'uc' }); | ||||||||||
| 45 | |||||||||||
| 46 | 37 | Sub::Install::install_sub({ code => 'NFKC', from => 'Unicode::Normalize', as => 'nfkc' }); | |||||||||
| 47 | 37 | Sub::Install::install_sub({ code => 'NFKD', from => 'Unicode::Normalize', as => 'nfkd' }); | |||||||||
| 48 | 37 | Sub::Install::install_sub({ code => 'NFC', from => 'Unicode::Normalize', as => 'nfc' }); | |||||||||
| 49 | 37 | Sub::Install::install_sub({ code => 'NFD', from => 'Unicode::Normalize', as => 'nfd' }); | |||||||||
| 50 | 37 | Sub::Install::install_sub({ code => 'decode_entities', from => 'HTML::Entities', as => 'decode_entities' }); | |||||||||
| 51 | 37 | Sub::Install::install_sub({ code => 'alnum_z2h', from => 'Lingua::JA::Regular::Unicode', as => 'alnum_z2h' }); | |||||||||
| 52 | 37 | Sub::Install::install_sub({ code => 'alnum_h2z', from => 'Lingua::JA::Regular::Unicode', as => 'alnum_h2z' }); | |||||||||
| 53 | 37 | Sub::Install::install_sub({ code => 'space_z2h', from => 'Lingua::JA::Regular::Unicode', as => 'space_z2h' }); | |||||||||
| 54 | 37 | Sub::Install::install_sub({ code => 'space_h2z', from => 'Lingua::JA::Regular::Unicode', as => 'space_h2z' }); | |||||||||
| 55 | 37 | Sub::Install::install_sub({ code => 'katakana_z2h', from => 'Lingua::JA::Regular::Unicode', as => 'katakana_z2h' }); | |||||||||
| 56 | 37 | Sub::Install::install_sub({ code => 'katakana_h2z', from => 'Lingua::JA::Regular::Unicode', as => 'katakana_h2z' }); | |||||||||
| 57 | 37 | Sub::Install::install_sub({ code => 'katakana2hiragana', from => 'Lingua::JA::Regular::Unicode', as => 'katakana2hiragana' }); | |||||||||
| 58 | 37 | Sub::Install::install_sub({ code => 'hiragana2katakana', from => 'Lingua::JA::Regular::Unicode', as => 'hiragana2katakana' }); | |||||||||
| 59 | 37 | Sub::Install::install_sub({ code => 'dakuon_normalize', from => 'Lingua::JA::Dakuon', as => 'dakuon_normalize' }); | |||||||||
| 60 | 37 | Sub::Install::install_sub({ code => 'handakuon_normalize', from => 'Lingua::JA::Dakuon', as => 'handakuon_normalize' }); | |||||||||
| 61 | 37 | Sub::Install::install_sub({ code => 'all_dakuon_normalize', from => 'Lingua::JA::Dakuon', as => 'all_dakuon_normalize' }); | |||||||||
| 62 | 37 | Sub::Install::install_sub({ code => 'square2katakana', from => 'Lingua::JA::Moji', as => 'square2katakana' }); | |||||||||
| 63 | 37 | Sub::Install::install_sub({ code => 'circled2kana', from => 'Lingua::JA::Moji', as => 'circled2kana' }); | |||||||||
| 64 | 37 | Sub::Install::install_sub({ code => 'circled2kanji', from => 'Lingua::JA::Moji', as => 'circled2kanji' }); | |||||||||
| 65 | |||||||||||
| 66 | sub new | ||||||||||
| 67 | { | ||||||||||
| 68 | 50 | ▶ | my $class = shift; | ||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 69 | |||||||||||
| 70 | 50 | ▶ | my @opts = (ref $_[0] eq 'ARRAY' ? @{$_[0]} : @_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | |||||||||||
| 72 | 50 | ▶ | Carp::croak("at least one option required") unless scalar @opts; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | |||||||||||
| 74 | 49 | my $self = bless {}, $class; | |||||||||
| 75 | |||||||||||
| 76 | 49 | $self->{converters} = []; | |||||||||
| 77 | |||||||||||
| 78 | 49 | my @unavailable_opts; | |||||||||
| 79 | |||||||||||
| 80 | 49 | for my $opt (@opts) | |||||||||
| 81 | { | ||||||||||
| 82 | 115 | ▶ | if (ref $opt ne 'CODE') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | { | ||||||||||
| 84 | 114 | ▶ | if ( exists $AVAILABLE_OPTS{$opt} ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | { | ||||||||||
| 86 | 111 | push( @{ $self->{converters} }, $opt ); | |||||||||
| 87 | } | ||||||||||
| 88 | 3 | else { push(@unavailable_opts, $opt); } | |||||||||
| 89 | } | ||||||||||
| 90 | else | ||||||||||
| 91 | { | ||||||||||
| 92 | # external function | ||||||||||
| 93 | 1 | push( @{ $self->{converters} }, $opt ); | |||||||||
| 94 | } | ||||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 49 | ▶ | Carp::croak( "unknown option(s): " . join(', ', @unavailable_opts) ) if scalar @unavailable_opts; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | |||||||||||
| 99 | 47 | return $self; | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | sub normalize | ||||||||||
| 103 | { | ||||||||||
| 104 | 220 | ▶ | my ($self, $text) = @_; | ||||||||
|
Subroutine
normalize: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 105 | |||||||||||
| 106 | 220 | ▶ | return undef unless defined $text; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | |||||||||||
| 108 | 37 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 109 | 214 | $text = $_->($text) for @{ $self->{converters} }; | |||||||||
| 110 | |||||||||||
| 111 | 214 | return $text; | |||||||||
| 112 | } | ||||||||||
| 113 | |||||||||||
| 114 | 5 | ▶ | sub lc { return defined $_[0] ? CORE::lc $_[0] : undef; } | ||||||||
|
Subroutine
lc: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||
| 115 | 4 | ▶ | sub uc { return defined $_[0] ? CORE::uc $_[0] : undef; } | ||||||||
|
Subroutine
uc: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||
| 116 | |||||||||||
| 117 | 10 | ▶ | sub strip_html { $SCRUBBER->scrub(shift); } | ||||||||
|
Subroutine
strip_html: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 118 | |||||||||||
| 119 | 13 | ▶ | sub wave2tilde { local $_ = shift; return undef unless defined $_; tr/\x{301C}\x{3030}/\x{FF5E}/; $_; } | ||||||||
|
Subroutine
wave2tilde: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 120 | 13 | ▶ | sub tilde2wave { local $_ = shift; return undef unless defined $_; tr/\x{FF5E}/\x{301C}/; $_; } | ||||||||
|
Subroutine
tilde2wave: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 121 | 7 | ▶ | sub wavetilde2long { local $_ = shift; return undef unless defined $_; tr/\x{301C}\x{3030}\x{FF5E}/\x{30FC}/; $_; } | ||||||||
|
Subroutine
wavetilde2long: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 122 | 7 | ▶ | sub wave2long { local $_ = shift; return undef unless defined $_; tr/\x{301C}\x{3030}/\x{30FC}/; $_; } | ||||||||
|
Subroutine
wave2long: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 123 | 7 | ▶ | sub tilde2long { local $_ = shift; return undef unless defined $_; tr/\x{FF5E}/\x{30FC}/; $_; } | ||||||||
|
Subroutine
tilde2long: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 124 | 7 | ▶ | sub fullminus2long { local $_ = shift; return undef unless defined $_; tr/\x{FF0D}/\x{30FC}/; $_; } | ||||||||
|
Subroutine
fullminus2long: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 125 | 8 | ▶ | sub dashes2long { local $_ = shift; return undef unless defined $_; tr/\x{2012}\x{2013}\x{2014}\x{2015}/\x{30FC}/; $_; } | ||||||||
|
Subroutine
dashes2long: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 126 | 7 | ▶ | sub drawing_lines2long { local $_ = shift; return undef unless defined $_; tr/\x{2500}\x{2501}\x{254C}\x{254D}\x{2574}\x{2576}\x{2578}\x{257A}/\x{30FC}/; $_; } | ||||||||
|
Subroutine
drawing_lines2long: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||
| 127 | 7 | ▶ | sub unify_long_repeats { local $_ = shift; return undef unless defined $_; tr/\x{30FC}/\x{30FC}/s; $_; } | ||||||||
|
Subroutine
unify_long_repeats: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 128 | 15 | ▶ | sub unify_long_spaces { local $_ = shift; return undef unless defined $_; tr/\x{0020}/\x{0020}/s; tr/\x{3000}/\x{3000}/s; s/[\x{0020}\x{3000}]{2,}/\x{0020}/g; $_; } | ||||||||
|
Subroutine
unify_long_spaces: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 129 | 83 | ▶ | sub unify_whitespaces { local $_ = shift; return undef unless defined $_; tr/\x{000B}\x{000C}\x{0085}\x{00A0}\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}/\x{0020}/; $_; } | ||||||||
|
Subroutine
unify_whitespaces: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 130 | 7 | ▶ | sub trim { local $_ = shift; return undef unless defined $_; s/^\s+//; s/\s+$//; $_; } | ||||||||
|
Subroutine
trim: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 131 | 11 | ▶ | sub ltrim { local $_ = shift; return undef unless defined $_; s/^\s+//; $_; } | ||||||||
|
Subroutine
ltrim: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 132 | 11 | ▶ | sub rtrim { local $_ = shift; return undef unless defined $_; s/\s+$//; $_; } | ||||||||
|
Subroutine
rtrim: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 133 | 11 | ▶ | sub nl2space { local $_ = shift; return undef unless defined $_; s/\x{000D}\x{000A}/\x{0020}/g; tr/\x{000D}\x{000A}/\x{0020}/; $_; } | ||||||||
|
Subroutine
nl2space: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 134 | 7 | ▶ | sub unify_nl { local $_ = shift; return undef unless defined $_; s/\x{000D}\x{000A}/\n/g; tr/\x{000D}\x{000A}/\n/; $_; } | ||||||||
|
Subroutine
unify_nl: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 135 | 8 | ▶ | sub tab2space { local $_ = shift; return undef unless defined $_; tr/\x{0009}/\x{0020}/; $_; } | ||||||||
|
Subroutine
tab2space: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 136 | 7 | ▶ | sub old2new_kana { local $_ = shift; return undef unless defined $_; tr/ゐヰゑヱ/いイえエ/; s/ヸ/イ\x{3099}/g; s/ヹ/エ\x{3099}/g; $_; } | ||||||||
|
Subroutine
old2new_kana: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 137 | 274 | ▶ | sub remove_controls { local $_ = shift; return undef unless defined $_; tr/\x{0000}-\x{0008}\x{000B}\x{000C}\x{000E}-\x{001F}\x{007F}-\x{009F}//d; $_; } | ||||||||
|
Subroutine
remove_controls: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 138 | 7 | ▶ | sub remove_spaces { local $_ = shift; return undef unless defined $_; tr/\x{0020}\x{3000}//d; $_; } | ||||||||
|
Subroutine
remove_spaces: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 139 | 9 | ▶ | sub remove_DFC { local $_ = shift; return undef unless defined $_; tr/\x{061C}\x{2066}-\x{2069}\x{200E}\x{200F}\x{202A}-\x{202E}//d; $_; } | ||||||||
|
Subroutine
remove_DFC: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||
| 140 | |||||||||||
| 141 | sub old2new_kanji | ||||||||||
| 142 | { | ||||||||||
| 143 | 8 | ▶ | local $_ = shift; | ||||||||
|
Subroutine
old2new_kanji: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 144 | 8 | ▶ | return undef unless defined $_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 145 | 6 | tr/亞惡壓圍爲醫壹逸稻飮隱營榮衞驛謁圓緣艷鹽奧應橫歐毆黃溫穩假價禍畫會壞悔懷海繪慨槪擴殼覺學嶽樂喝渴褐勸卷寬歡漢罐觀關陷顏器既歸氣祈龜僞戲犧舊據擧虛峽挾狹鄕響曉勤謹區驅勳薰徑惠揭溪經繼莖螢輕鷄藝擊缺儉劍圈檢權獻硏縣險顯驗嚴效廣恆鑛號國穀黑濟碎齋劑櫻册殺雜參慘棧蠶贊殘祉絲視齒兒辭濕實舍寫煮社者釋壽收臭從澁獸縱祝肅處暑緖署諸敍奬將涉燒祥稱證乘剩壤孃條淨狀疊讓釀囑觸寢愼眞神盡圖粹醉隨髓數樞瀨聲靜齊攝竊節專戰淺潛纖踐錢禪曾祖僧雙壯層搜插巢爭瘦總莊裝騷增憎臟藏贈卽屬續墮體對帶滯臺瀧擇澤單嘆擔膽團彈斷癡遲晝蟲鑄著廳徵懲聽敕鎭塚遞鐵轉點傳都黨盜燈當鬭德獨讀突屆繩難貳惱腦霸廢拜梅賣麥發髮拔繁晚蠻卑碑祕濱賓頻敏甁侮福拂佛倂塀竝變邊勉辨瓣辯舖步穗寶襃豐墨沒飜每萬滿免麵默餠戾彌藥譯豫餘與譽搖樣謠來賴亂欄覽隆龍虜兩獵綠壘淚類勵禮隸靈齡曆歷戀練鍊爐勞廊朗樓郞錄灣堯巖晉槇渚猪琢瑤祐祿禎穰聰遙/亜悪圧囲為医壱逸稲飲隠営栄衛駅謁円縁艶塩奥応横欧殴黄温穏仮価禍画会壊悔懐海絵慨概拡殻覚学岳楽喝渇褐勧巻寛歓漢缶観関陥顔器既帰気祈亀偽戯犠旧拠挙虚峡挟狭郷響暁勤謹区駆勲薫径恵掲渓経継茎蛍軽鶏芸撃欠倹剣圏検権献研県険顕験厳効広恒鉱号国穀黒済砕斎剤桜冊殺雑参惨桟蚕賛残祉糸視歯児辞湿実舎写煮社者釈寿収臭従渋獣縦祝粛処暑緒署諸叙奨将渉焼祥称証乗剰壌嬢条浄状畳譲醸嘱触寝慎真神尽図粋酔随髄数枢瀬声静斉摂窃節専戦浅潜繊践銭禅曽祖僧双壮層捜挿巣争痩総荘装騒増憎臓蔵贈即属続堕体対帯滞台滝択沢単嘆担胆団弾断痴遅昼虫鋳著庁徴懲聴勅鎮塚逓鉄転点伝都党盗灯当闘徳独読突届縄難弐悩脳覇廃拝梅売麦発髪抜繁晩蛮卑碑秘浜賓頻敏瓶侮福払仏併塀並変辺勉弁弁弁舗歩穂宝褒豊墨没翻毎万満免麺黙餅戻弥薬訳予余与誉揺様謡来頼乱欄覧隆竜虜両猟緑塁涙類励礼隷霊齢暦歴恋練錬炉労廊朗楼郎録湾尭巌晋槙渚猪琢瑶祐禄禎穣聡遥/; | |||||||||
| 146 | 6 | return $_; | |||||||||
| 147 | } | ||||||||||
| 148 | |||||||||||
| 149 | 37 | 1; | |||||||||
| 150 | |||||||||||
| 151 | __END__ | ||||||||||