kampyo 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b829ea3056d65633cdf1aad8c16d6e4e264f8d65cd4189dfea536cf3832d865d
4
+ data.tar.gz: 056461105f347a5561f5fece1417dbb4f74fe4a8f9dc353218f1615c4b0fb204
5
+ SHA512:
6
+ metadata.gz: 1708723267aebe5a8dff6894ca8890476210d535da9afcae3373ba6395b7ed1b4b1f95a8f11714f9e224352e2ad32b6773bf20d1aa72e430eb8f9214cc67cc10
7
+ data.tar.gz: 1d6ae9002cf28401085617897bf8359a9bd512211ad62258335091e38da94cd0ed3039690178132ae8c32545d2bca7b9ac00fe502fe1335a6718520c72d839d5
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-03-02
4
+
5
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 arthur87
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.base.md ADDED
@@ -0,0 +1,30 @@
1
+ <!---------------------------->
2
+ <!-- multilingual suffix: en, ja -->
3
+ <!-- no suffix: en -->
4
+ <!---------------------------->
5
+
6
+ <!-- $ mmg README.base.md -->
7
+
8
+ # kampyo
9
+
10
+ kampyo is a library for conveniently manipulating Cabocha and Mecab.
11
+
12
+ kampyo has its own analysis function.
13
+
14
+ Unique features already implemented are subject and predicate, and stylistic inference.
15
+
16
+ # Guide
17
+
18
+ Using Cabocha.
19
+
20
+ ```
21
+ text = Kampyo::Text.new
22
+ text.cabocha_parser("今日は雨です")
23
+ ```
24
+
25
+ Guess the subject, predicate and sentence system.
26
+
27
+ ```
28
+ text = Kampyo::Text.new
29
+ text.analysis(text.cabocha_parser("今日は雨です"))
30
+ ```
data/README.ja.md ADDED
@@ -0,0 +1,25 @@
1
+
2
+
3
+ # kampyo
4
+
5
+ kampyo is a library for conveniently manipulating Cabocha and Mecab.
6
+
7
+ kampyo has its own analysis function.
8
+
9
+ Unique features already implemented are subject and predicate, and stylistic inference.
10
+
11
+ # Guide
12
+
13
+ Using Cabocha.
14
+
15
+ ```
16
+ text = Kampyo::Text.new
17
+ text.cabocha_parser("今日は雨です")
18
+ ```
19
+
20
+ Guess the subject, predicate and sentence system.
21
+
22
+ ```
23
+ text = Kampyo::Text.new
24
+ text.analysis(text.cabocha_parser("今日は雨です"))
25
+ ```
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+
2
+
3
+ # kampyo
4
+
5
+ kampyo is a library for conveniently manipulating Cabocha and Mecab.
6
+
7
+ kampyo has its own analysis function.
8
+
9
+ Unique features already implemented are subject and predicate, and stylistic inference.
10
+
11
+ # Guide
12
+
13
+ Using Cabocha.
14
+
15
+ ```
16
+ text = Kampyo::Text.new
17
+ text.cabocha_parser("今日は雨です")
18
+ ```
19
+
20
+ Guess the subject, predicate and sentence system.
21
+
22
+ ```
23
+ text = Kampyo::Text.new
24
+ text.analysis(text.cabocha_parser("今日は雨です"))
25
+ ```
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: %i[]
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class String
4
+ def to_utf8
5
+ force_encoding('UTF-8')
6
+ end
7
+ end
@@ -0,0 +1,202 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'kampyo'
4
+ require 'kampyo/string'
5
+ require 'cabocha'
6
+ require 'mecab'
7
+
8
+ module Kampyo
9
+ class Text
10
+ def initialize; end
11
+
12
+ def cabocha_parser(input)
13
+ # <sentence>
14
+ # <chunk id="0" link="1" rel="D" score="0.000000" head="0" func="1">
15
+ # <tok id="0" feature="名詞,副詞可能,*,*,*,*,今日,キョウ,キョー" ne="B-DATE">今日</tok>
16
+ # <tok id="1" feature="助詞,係助詞,*,*,*,*,は,ハ,ワ" ne="O">は</tok>
17
+ # </chunk>
18
+ # <chunk id="1" link="-1" rel="D" score="0.000000" head="2" func="3">
19
+ # <tok id="2" feature="名詞,一般,*,*,*,*,雨,アメ,アメ" ne="O">雨</tok>
20
+ # <tok id="3" feature="助動詞,*,*,*,特殊・デス,基本形,です,デス,デス" ne="O">です</tok>
21
+ # </chunk>
22
+ # </sentence>
23
+
24
+ parser = CaboCha::Parser.new
25
+ tree = parser.parse(input)
26
+
27
+ chunks = []
28
+ tokens = []
29
+ token_position = 0
30
+ (0..tree.chunk_size - 1).each do |i|
31
+ chunk = tree.chunk(i)
32
+ token_size = chunk.token_size
33
+
34
+ chunks << {
35
+ id: chunks.size + 1,
36
+ link: chunk.link >= 0 ? chunk.link + 1 : -1,
37
+ score: chunk.score
38
+ }
39
+
40
+ (token_position..token_position + token_size - 1).each do |j|
41
+ token = tree.token(j)
42
+
43
+ surface = token.surface.to_utf8
44
+ feature0 = token.feature_list(0).to_utf8
45
+ feature1 = token.feature_list(1).to_utf8
46
+ feature6 = token.feature_list(6).to_utf8
47
+ feature7 = token.feature_list(7).to_utf8
48
+
49
+ tokens << {
50
+ id: tokens.size + 1,
51
+ chunk: i + 1,
52
+ surface: surface,
53
+ feature1: feature0,
54
+ feature2: feature1,
55
+ baseform: feature6,
56
+ reading: feature7,
57
+ ext_reading: ext_reading(feature7)
58
+ }
59
+ end
60
+
61
+ token_position += token_size
62
+ end
63
+
64
+ { chunks: chunks, tokens: tokens }
65
+ end
66
+
67
+ def mecab_parser(input)
68
+ result = []
69
+ parser = MeCab::Tagger.new
70
+ node = parser.parseToNode(input)
71
+ while node
72
+ features = node.feature.split(',')
73
+ if features[0] != 'BOS/EOS'
74
+ result << {
75
+ id: result.size + 1,
76
+ chunk: 0,
77
+ surface: node.surface,
78
+ feature1: features[0],
79
+ feature2: features[1],
80
+ baseform: features[6],
81
+ reading: features[7],
82
+ ext_reading: ext_reading(features[7]),
83
+ cost: node.cost,
84
+ wcost: node.wcost,
85
+ right_context: node.rcAttr,
86
+ left_context: node.lcAttr
87
+ }
88
+ end
89
+ node = node.next
90
+ end
91
+
92
+ result
93
+ end
94
+
95
+ def ext_reading(feature)
96
+ (feature =~ /\A[\p{katakana}|ー]+\z/).nil? ? feature : nil
97
+ end
98
+
99
+ def analysis(cabocha)
100
+ chunks = cabocha[:chunks]
101
+ tokens = cabocha[:tokens]
102
+
103
+ subject_token = nil
104
+ predicate_token = nil
105
+ tod = '*'
106
+
107
+ # 述語の候補
108
+ predicate_chunk = chunks.find { |item| item[:link] == -1 }
109
+ predicate_tokens = tokens.select { |item| item[:chunk] == predicate_chunk[:id] }
110
+
111
+ # 主語の候補
112
+ subject_chunk = chunks.find do |item|
113
+ item[:link] == predicate_chunk[:id]
114
+ end
115
+ unless subject_chunk.nil?
116
+ subject_tokens = tokens.select do |item|
117
+ item[:chunk] == subject_chunk[:id] &&
118
+ item[:feature1] == '名詞' &&
119
+ %w[一般 固有名詞 サ変接続 接尾 数 副詞可能].include?(item[:feature2])
120
+ end
121
+
122
+ subject_tokens.each do |token|
123
+ next_token = tokens.find do |item|
124
+ item[:id] == token[:id] + 1 &&
125
+ item[:chunk] == token[:chunk] &&
126
+ %w[は って も が].include?(item[:baseform])
127
+ end
128
+
129
+ next if next_token.nil?
130
+
131
+ # 主語として確定する
132
+ subject_token = token
133
+ next
134
+ end
135
+ end
136
+
137
+ predicate_tokens.each do |token|
138
+ if %w[形容詞 動詞 名詞].include?(token[:feature1]) &&
139
+ %w[一般 自立 サ変接続 接尾].include?(token[:feature2]) &&
140
+ token[:baseform] != '*'
141
+ # 述語として確定する
142
+
143
+ predicate_token = token
144
+ end
145
+
146
+ next unless %w[動詞 終助詞 助動詞 助詞].include?(token[:feature1])
147
+
148
+ # 文体系を確定する
149
+ tods = {
150
+ 'れる' => '受身・尊敬・可能・自発',
151
+ 'られる' => '受身・尊敬・可能・自発',
152
+ 'せる' => '使役',
153
+ 'させる' => '使役',
154
+ 'ない' => '打消',
155
+ 'ぬ' => '打消',
156
+ 'ん' => '打消',
157
+ 'う' => '推量・意志・勧誘',
158
+ 'よう' => '推量・意志・勧誘',
159
+ 'まい' => '打消推量・打消意志',
160
+ 'たい' => '希望',
161
+ 'たがる' => '希望',
162
+ 'た' => '過去・完了・存在・確認',
163
+ 'ます' => '丁寧',
164
+ 'そうだ' => '様態・伝聞',
165
+ 'らしい' => '推定',
166
+ 'ようだ' => '比況・例示・推定',
167
+ 'だ' => '断定',
168
+ 'です' => '断定',
169
+ 'な' => '禁止・感動',
170
+ 'か' => '疑問・反語・感動',
171
+ 'の' => '断定・質問',
172
+ 'よ' => '強意・呼びかけ',
173
+ 'ぞ' => '強意',
174
+ 'も' => '確信',
175
+ 'ね' => '感動・念押し',
176
+ 'わ' => '感動・強意',
177
+ 'さ' => '断定'
178
+ }
179
+
180
+ tod = tods[token[:baseform]]
181
+ end
182
+
183
+ last_token = predicate_tokens.last
184
+
185
+ if predicate_token.nil? && ['助動詞'].include?(last_token[:feature1])
186
+ # 述語が確定していないとき最後の助動詞を述語として確定する
187
+ predicate_token = last_token
188
+ end
189
+
190
+ if ['?', '?'].include?(last_token[:surface])
191
+ # 述語の最後の形態素が?のとき
192
+ tod = '疑問・反語・感動'
193
+ end
194
+
195
+ {
196
+ subject: subject_token,
197
+ predicate: predicate_token,
198
+ tod: tod
199
+ }
200
+ end
201
+ end
202
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Kampyo
4
+ VERSION = '0.1.0'
5
+ end
data/lib/kampyo.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'kampyo/version'
4
+
5
+ module Kampyo
6
+ class Error < StandardError; end
7
+ # Your code goes here...
8
+ end
data/sig/kampyo.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Kampyo
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kampyo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - arthur87
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-03-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: cabocha
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mecab
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: kampyo is a library for conveniently manipulating Cabocha and Mecab.
56
+ email:
57
+ - arthur87@users.noreply.github.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - CHANGELOG.md
63
+ - LICENSE
64
+ - README.base.md
65
+ - README.ja.md
66
+ - README.md
67
+ - Rakefile
68
+ - lib/kampyo.rb
69
+ - lib/kampyo/string.rb
70
+ - lib/kampyo/text.rb
71
+ - lib/kampyo/version.rb
72
+ - sig/kampyo.rbs
73
+ homepage: https://github.com/arthur87/kampyo
74
+ licenses: []
75
+ metadata:
76
+ homepage_uri: https://github.com/arthur87/kampyo
77
+ source_code_uri: https://github.com/arthur87/kampyo
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 3.0.0
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubygems_version: 3.3.3
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: kampyo is a library for conveniently manipulating Cabocha and Mecab.
97
+ test_files: []