-
Notifications
You must be signed in to change notification settings - Fork 1
/
Model.pm6
141 lines (96 loc) · 3.19 KB
/
Model.pm6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use v6;
unit class MeCab::Model:auth<zef:titsuki>:ver<0.0.19> is repr('CPointer');
use NativeCall;
use MeCab::Tagger;
use MeCab::Lattice;
my constant $library = %?RESOURCES<libraries/mecab>.Str;
my sub mecab_model_destroy(MeCab::Model) is native($library) { * }
my sub mecab_model_new(int32, CArray[Str]) returns MeCab::Model is native($library) { * }
my sub mecab_model_new2(Str) returns MeCab::Model is native($library) { * }
my sub mecab_model_new_tagger(MeCab::Model) returns MeCab::Tagger is native($library) { * }
my sub mecab_model_new_lattice(MeCab::Model) returns MeCab::Lattice is native($library) { * }
multi submethod new {
mecab_model_new2("-C")
}
multi submethod new(Str $argv) {
mecab_model_new2($argv);
}
multi submethod new(
Str :$rcfile,
Str :$dicdir,
Str :$userdic,
) {
my @args;
@args.push('-C'); # allocate-sentence
if $rcfile.defined {
$rcfile.IO.f or die "$rcfile doesn't exist.";
$dicdir.defined or die ":rcfile requires :dicdir.";
$dicdir.IO.d or die "$dicdir doesn't exist.";
@args.push(sprintf('-r %s', $rcfile));
}
if $dicdir.defined {
$dicdir.IO.d or die "$dicdir doesn't exist.";
@args.push(sprintf('-d %s', $dicdir));
}
if $userdic.defined {
$userdic.IO.f or die "$userdic doesn't exist.";
@args.push(sprintf('-u %s', $userdic))
}
mecab_model_new2(@args.join(' '));
}
method create-tagger {
mecab_model_new_tagger(self)
}
method create-lattice {
mecab_model_new_lattice(self)
}
submethod DESTROY {
mecab_model_destroy(self)
}
=begin pod
=head1 NAME
MeCab::Model - A Raku MeCab::Model class
=head1 SYNOPSIS
use MeCab;
use MeCab::Lattice;
use MeCab::Tagger;
use MeCab::Model;
my MeCab::Model $model .= new;
my MeCab::Tagger $tagger = $model.create-tagger;
my MeCab::Lattice $lattice = $model.create-lattice;
$lattice.add-request-type(MECAB_NBEST);
$lattice.sentence("今日も");
if $tagger.parse($lattice) {
say $lattice.nbest-tostr(2);
}
# OUTPUT«
# 今日 名詞,副詞可能,*,*,*,*,今日,キョウ,キョー
# も 助詞,係助詞,*,*,*,*,も,モ,モ
# EOS
# 今日 名詞,副詞可能,*,*,*,*,今日,コンニチ,コンニチ
# も 助詞,係助詞,*,*,*,*,も,モ,モ
# EOS
# »
=head1 DESCRIPTION
MeCab::Model is a Raku MeCab::Model class.
=head2 METHODS
=head3 new
Defined as:
multi method new() returns MeCab::Model
multi method new(Str $extra-argv) returns MeCab::Model
Creates a new MeCab::Model object.
=head3 create-tagger
Defined as:
method create-tagger() returns MeCab::Tagger
Creates a new MeCab::Tagger object.
=head3 create-lattice
Defined as:
method create-lattice() returns MeCab::Lattice
Creates a new MeCab::Lattice object.
=head1 AUTHOR
titsuki <titsuki@cpan.org>
=head1 COPYRIGHT AND LICENSE
Copyright 2016 titsuki
libmecab ( http://taku910.github.io/mecab/ ) by Taku Kudo is licensed under the GPL, LGPL or BSD Licenses.
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.
=end pod