| 1 | package Text::Mecabist::Node; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Moo; | ||
| 5 | |||
| 6 | with 'Text::Mecabist::Iter'; | ||
| 7 | with 'Text::Mecabist::Helper'; | ||
| 8 | |||
| 9 | # mecab vars | ||
| 10 | has [qw/ id length rlength rcattr lcattr stat isbest alpha beta prob wcost cost /] => ( | ||
| 11 | is => 'ro' | ||
| 12 | ); | ||
| 13 | |||
| 14 | # mecab var decoded | ||
| 15 | has [qw/ surface feature format /] => ( | ||
| 16 | is => 'ro', | ||
| 17 | default => "", | ||
| 18 | ); | ||
| 19 | |||
| 20 | # 品詞,品詞細分類1,品詞細分類2,品詞細分類3,活用型,活用形,原形,読み,発音 (,その他) | ||
| 21 | has [qw/ pos pos1 pos2 pos3 inflection_type inflection_form lemma reading pronunciation extra /] => ( | ||
| 22 | is => 'ro' | ||
| 23 | ); | ||
| 24 | |||
| 25 | # extra (up to 5 items) | ||
| 26 | has [qw/ extra1 extra2 extra3 extra4 extra5 /] => ( | ||
| 27 | is => 'ro' | ||
| 28 | ); | ||
| 29 | |||
| 30 | # copied from surface | ||
| 31 | has text => ( | ||
| 32 | is => 'rw', | ||
| 33 | default => "", | ||
| 34 | ); | ||
| 35 | |||
| 36 | sub BUILDARGS { | ||
| 37 | my $class = shift; | ||
| 38 | |||
| 39 | my $type = ref $_[0]; | ||
| 40 | if ($type ne 'Text::MeCab::Node') { | ||
| 41 | return $type eq 'HASH' ? shift : { @_ }; | ||
| 42 | } | ||
| 43 | |||
| 44 | my ($node, $parser) = @_; | ||
| 45 | my %args = ( | ||
| 46 | id => $node->id, | ||
| 47 | length => $node->length, | ||
| 48 | rlength => $node->rlength, | ||
| 49 | rcattr => $node->rcattr, | ||
| 50 | lcattr => $node->lcattr, | ||
| 51 | stat => $node->stat, | ||
| 52 | isbest => $node->isbest, | ||
| 53 | alpha => $node->alpha, | ||
| 54 | beta => $node->beta, | ||
| 55 | prob => $node->prob, | ||
| 56 | wcost => $node->wcost, | ||
| 57 | cost => $node->cost, | ||
| 58 | |||
| 59 | feature => $parser->encoding->decode($node->feature), | ||
| 60 | format => $parser->encoding->decode($node->format($parser->mecab)), | ||
| 61 | ); | ||
| 62 | |||
| 63 | @args{qw/ pos pos1 pos2 pos3 inflection_type inflection_form lemma reading pronunciation extra /} | ||
| 64 | = split(/,/, $args{feature}, 10); | ||
| 65 | |||
| 66 | if ($args{extra}) { | ||
| 67 | @args{qw/ extra1 extra2 extra3 extra4 extra5 /} | ||
| 68 | = split(',', $args{extra}); | ||
| 69 | } | ||
| 70 | |||
| 71 | if (defined $node->surface) { | ||
| 72 | $args{surface} = $parser->encoding->decode($node->surface); | ||
| 73 | } | ||
| 74 | |||
| 75 | $args{text} = $args{surface} // ""; | ||
| 76 | |||
| 77 | return \%args; | ||
| 78 | } | ||
| 79 | |||
| 80 | 1; |