blib/lib/Lingua/JA/NormalizeText.pm

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