kanji-translator 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 +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +103 -0
- data/Rakefile +12 -0
- data/lib/kanji/translator/core_ext/string.rb +21 -0
- data/lib/kanji/translator/version.rb +7 -0
- data/lib/kanji/translator.rb +197 -0
- data/sig/kanji/translator.rbs +6 -0
- metadata +70 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c4f92f7987bc8ed9600c1b5849f56c0538f6af423afd53249ae70bc83e53a383
|
|
4
|
+
data.tar.gz: 226e287702852d0d05563b4a38aba069caef02e52c56c76d4011ef22e928c43c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7e09f7183a129996b4fbfeff7e56b86f51e7b6b53ef7db428bb3c45b4e1bea4acb644251e996dbb68fe05c39785b202d76d7d3aca1d50b9c6299ce15ec1c74a7
|
|
7
|
+
data.tar.gz: a99f5a503d9b9a1a715d3d9405a86306c110ebc13b0c6bb5c17c494453460f04c05130176a4662138c8d2e24dfa119fd0080f61943c0364e5dd2cab963d24d54
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Hiromu Kodani
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# Kanji::Translator
|
|
2
|
+
|
|
3
|
+
漢字を「ひらがな」「カタカナ」「ローマ字(ヘボン式・簡易)」に変換し、スラッグも作れるシンプルなRubyライブラリです。読みは外部サイト(yomikatawa.com)の結果を取得して解析します。
|
|
4
|
+
|
|
5
|
+
> 注意: 本ライブラリはyomikatawa.comの非公式クライアントです。サイト構造の変更やレート制限の影響を受ける可能性があります。
|
|
6
|
+
|
|
7
|
+
## インストール
|
|
8
|
+
|
|
9
|
+
Ruby 3.2以上が必要です。
|
|
10
|
+
|
|
11
|
+
Rubygems公開後:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
bundle add kanji-translator
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
未公開期間にGitHubから使う場合:
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
# Gemfile
|
|
21
|
+
gem "kanji-translator", git: "https://github.com/dw-kodani/kanji-translator"
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## クイックスタート
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
require "kanji/translator"
|
|
28
|
+
|
|
29
|
+
Kanji::Translator.to_hira("漢字") #=> "かんじ"
|
|
30
|
+
Kanji::Translator.to_kata("漢字") #=> "カンジ"
|
|
31
|
+
Kanji::Translator.to_roma("漢字") #=> "kanji"
|
|
32
|
+
Kanji::Translator.to_slug("学校案内") #=> "gakkou-annai"
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
文字列メソッド(任意):
|
|
36
|
+
|
|
37
|
+
```ruby
|
|
38
|
+
require "kanji/translator/core_ext/string"
|
|
39
|
+
|
|
40
|
+
"漢字".to_hira #=> "かんじ"
|
|
41
|
+
"学校案内".to_slug #=> "gakkou-annai"
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## API
|
|
45
|
+
|
|
46
|
+
すべてのメソッドはネットワークを使う可能性があります(読み取得のため)。タイムアウトやリトライはオプションで調整できます。
|
|
47
|
+
|
|
48
|
+
- `Kanji::Translator.to_hira(text, timeout: 5, retries: 2, backoff: 0.5, user_agent: "kanji-translator/#{VERSION}")`
|
|
49
|
+
- 読み(ひらがな)を返します。
|
|
50
|
+
- `Kanji::Translator.to_kata(text, **opts)`
|
|
51
|
+
- ひらがな読みをカタカナに変換して返します。
|
|
52
|
+
- `Kanji::Translator.to_roma(text, **opts)`
|
|
53
|
+
- 簡易ヘボン式のローマ字(ASCII、小文字)で返します。拗音/促音(ゃゅょ/っ)に対応。長音記号「ー」は無視します(例: おう→ou)。
|
|
54
|
+
- `Kanji::Translator.to_slug(text, separator: "-", downcase: true, collapse: true, **opts)`
|
|
55
|
+
- `to_roma` の結果をスラッグ化します。
|
|
56
|
+
- 非英数字を `separator` に置換、連続区切りを圧縮、前後の区切りをトリムします。
|
|
57
|
+
|
|
58
|
+
例(オプション):
|
|
59
|
+
|
|
60
|
+
```ruby
|
|
61
|
+
Kanji::Translator.to_hira("漢字", timeout: 3, retries: 1)
|
|
62
|
+
Kanji::Translator.to_slug("東京タワー 2010") #=> "toukyou-tawa-2010"
|
|
63
|
+
Kanji::Translator.to_slug("Foo Bar", separator: "_") #=> "foo_bar"
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### 例外
|
|
67
|
+
|
|
68
|
+
- `Kanji::Translator::TimeoutError` — 接続/読み取りタイムアウト
|
|
69
|
+
- `Kanji::Translator::HTTPError` — 429/5xxなどのHTTPエラー(一定回数リトライ後に発生)
|
|
70
|
+
- `Kanji::Translator::Error` — パース失敗などの基底例外
|
|
71
|
+
|
|
72
|
+
429(レート制限)や5xxは指数バックオフで自動リトライします。回数や待機は `retries`/`backoff` で調整可能です。
|
|
73
|
+
|
|
74
|
+
## 開発
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
bin/setup # 依存をインストール
|
|
78
|
+
bundle exec rake # RSpec + RuboCop を実行
|
|
79
|
+
bin/console # IRBで試す
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
- Ruby: 3.2+(CIは 3.2/3.3/3.4)
|
|
83
|
+
- テスト: WebMockでHTTPをスタブしています(ネットワーク不要)。VCRの設定は入っていますが現状未使用です。
|
|
84
|
+
- コードスタイル: RuboCop(新Copは自動有効化)。自動修正は `-a`(安全)/`-A`(積極的)を使用。
|
|
85
|
+
|
|
86
|
+
## リリース
|
|
87
|
+
|
|
88
|
+
1. バージョン更新: `lib/kanji/translator/version.rb`
|
|
89
|
+
2. 変更履歴: `CHANGELOG.md` を更新
|
|
90
|
+
3. リリース: `bundle exec rake release`
|
|
91
|
+
- Gitタグ作成 → push → Rubygemsへpush(MFA必須)
|
|
92
|
+
|
|
93
|
+
## ライセンス
|
|
94
|
+
|
|
95
|
+
MIT License。詳細は `LICENSE.txt` を参照してください。
|
|
96
|
+
|
|
97
|
+
## 貢献 / コード・オブ・コンダクト
|
|
98
|
+
|
|
99
|
+
Issue/Pull Requestは歓迎します。`CODE_OF_CONDUCT.md` に同意の上でご参加ください。連絡先はリポジトリのIssueをご利用ください。
|
|
100
|
+
|
|
101
|
+
## 謝辞
|
|
102
|
+
|
|
103
|
+
- 読み取得元: https://yomikatawa.com (非公式)
|
data/Rakefile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "kanji/translator"
|
|
4
|
+
|
|
5
|
+
class String
|
|
6
|
+
def to_hira(**)
|
|
7
|
+
Kanji::Translator.to_hira(self, **)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def to_kata(**)
|
|
11
|
+
Kanji::Translator.to_kata(self, **)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def to_roma(**)
|
|
15
|
+
Kanji::Translator.to_roma(self, **)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def to_slug(**)
|
|
19
|
+
Kanji::Translator.to_slug(self, **)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "translator/version"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
require "nokogiri"
|
|
7
|
+
|
|
8
|
+
module Kanji
|
|
9
|
+
module Translator
|
|
10
|
+
class Error < StandardError; end
|
|
11
|
+
class HTTPError < Error; end
|
|
12
|
+
class TimeoutError < Error; end
|
|
13
|
+
|
|
14
|
+
USER_AGENT = "kanji-translator/#{VERSION}".freeze
|
|
15
|
+
HOST = "yomikatawa.com"
|
|
16
|
+
|
|
17
|
+
def self.to_hira(text, timeout: 5, retries: 2, backoff: 0.5, user_agent: USER_AGENT)
|
|
18
|
+
raise ArgumentError, "text must be a String" unless text.is_a?(String)
|
|
19
|
+
|
|
20
|
+
body = fetch_page(text, timeout: timeout, retries: retries, backoff: backoff, user_agent: user_agent)
|
|
21
|
+
parse_hiragana(body)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def self.to_kata(text, **)
|
|
25
|
+
hira = to_hira(text, **)
|
|
26
|
+
hiragana_to_katakana(hira)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.to_roma(text, **)
|
|
30
|
+
hira = to_hira(text, **)
|
|
31
|
+
hiragana_to_romaji(hira)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.to_slug(text, separator: "-", downcase: true, collapse: true, **)
|
|
35
|
+
roma = to_roma(text, **)
|
|
36
|
+
s = downcase ? roma.downcase : roma.dup
|
|
37
|
+
sep = separator
|
|
38
|
+
# Replace non-alphanumeric with separator
|
|
39
|
+
s = s.gsub(/[^a-z0-9]+/, sep)
|
|
40
|
+
# Collapse duplicate separators
|
|
41
|
+
s = s.gsub(/#{Regexp.escape(sep)}{2,}/, sep) if collapse && !sep.empty?
|
|
42
|
+
# Trim leading/trailing separators
|
|
43
|
+
s = s.gsub(/^#{Regexp.escape(sep)}|#{Regexp.escape(sep)}$/, "") unless sep.empty?
|
|
44
|
+
s
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def self.fetch_page(text, timeout:, retries:, backoff:, user_agent: USER_AGENT)
|
|
48
|
+
encoded = URI.encode_www_form_component(text)
|
|
49
|
+
path = "/kanji/#{encoded}"
|
|
50
|
+
attempt = 0
|
|
51
|
+
|
|
52
|
+
begin
|
|
53
|
+
attempt += 1
|
|
54
|
+
http = Net::HTTP.new(HOST, 443)
|
|
55
|
+
http.use_ssl = true
|
|
56
|
+
http.open_timeout = timeout
|
|
57
|
+
http.read_timeout = timeout
|
|
58
|
+
|
|
59
|
+
req = Net::HTTP::Get.new(path)
|
|
60
|
+
req["User-Agent"] = user_agent
|
|
61
|
+
|
|
62
|
+
resp = http.request(req)
|
|
63
|
+
|
|
64
|
+
case resp.code.to_i
|
|
65
|
+
when 200
|
|
66
|
+
resp.body
|
|
67
|
+
when 429
|
|
68
|
+
raise HTTPError, "rate limited: 429"
|
|
69
|
+
when 500..599
|
|
70
|
+
raise HTTPError, "server error: #{resp.code}"
|
|
71
|
+
else
|
|
72
|
+
raise HTTPError, "unexpected response: #{resp.code}"
|
|
73
|
+
end
|
|
74
|
+
rescue Timeout::Error => e
|
|
75
|
+
raise TimeoutError, e.message if attempt > retries + 1
|
|
76
|
+
|
|
77
|
+
sleep(backoff_for(attempt, base: backoff))
|
|
78
|
+
retry
|
|
79
|
+
rescue HTTPError => e
|
|
80
|
+
if e.message.include?("429") && attempt <= retries
|
|
81
|
+
# Respect Retry-After if present (best-effort)
|
|
82
|
+
# WebMock tests usually set 0, production may set seconds
|
|
83
|
+
retry_after = 0.0
|
|
84
|
+
sleep(retry_after)
|
|
85
|
+
sleep(backoff_for(attempt, base: backoff))
|
|
86
|
+
retry
|
|
87
|
+
elsif e.message.include?("server error") && attempt <= retries
|
|
88
|
+
sleep(backoff_for(attempt, base: backoff))
|
|
89
|
+
retry
|
|
90
|
+
else
|
|
91
|
+
raise
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def self.parse_hiragana(html)
|
|
97
|
+
doc = Nokogiri::HTML(html)
|
|
98
|
+
cell = doc.at_css("#yomikata tbody tr td")
|
|
99
|
+
text = cell&.inner_text&.strip
|
|
100
|
+
raise Error, "failed to parse reading" if text.nil? || text.empty?
|
|
101
|
+
|
|
102
|
+
text
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def self.hiragana_to_katakana(hira)
|
|
106
|
+
hira.tr("ぁ-ゔゝゞー", "ァ-ヴヽヾー")
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
DIGRAPHS = {
|
|
110
|
+
"きゃ" => "kya", "きゅ" => "kyu", "きぇ" => "kye", "きょ" => "kyo",
|
|
111
|
+
"ぎゃ" => "gya", "ぎゅ" => "gyu", "ぎぇ" => "gye", "ぎょ" => "gyo",
|
|
112
|
+
"しゃ" => "sha", "しゅ" => "shu", "しぇ" => "she", "しょ" => "sho",
|
|
113
|
+
"じゃ" => "ja", "じゅ" => "ju", "じぇ" => "je", "じょ" => "jo",
|
|
114
|
+
"ちゃ" => "cha", "ちゅ" => "chu", "ちぇ" => "che", "ちょ" => "cho",
|
|
115
|
+
"にゃ" => "nya", "にゅ" => "nyu", "にぇ" => "nye", "にょ" => "nyo",
|
|
116
|
+
"ひゃ" => "hya", "ひゅ" => "hyu", "ひぇ" => "hye", "ひょ" => "hyo",
|
|
117
|
+
"びゃ" => "bya", "びゅ" => "byu", "びぇ" => "bye", "びょ" => "byo",
|
|
118
|
+
"ぴゃ" => "pya", "ぴゅ" => "pyu", "ぴぇ" => "pye", "ぴょ" => "pyo",
|
|
119
|
+
"みゃ" => "mya", "みゅ" => "myu", "みぇ" => "mye", "みょ" => "myo",
|
|
120
|
+
"りゃ" => "rya", "りゅ" => "ryu", "りぇ" => "rye", "りょ" => "ryo",
|
|
121
|
+
"ゔぁ" => "va", "ゔぃ" => "vi", "ゔぇ" => "ve", "ゔぉ" => "vo",
|
|
122
|
+
"ふぁ" => "fa", "ふぃ" => "fi", "ふぇ" => "fe", "ふぉ" => "fo"
|
|
123
|
+
}.freeze
|
|
124
|
+
|
|
125
|
+
BASIC = {
|
|
126
|
+
"あ" => "a", "い" => "i", "う" => "u", "え" => "e", "お" => "o",
|
|
127
|
+
"か" => "ka", "き" => "ki", "く" => "ku", "け" => "ke", "こ" => "ko",
|
|
128
|
+
"さ" => "sa", "し" => "shi", "す" => "su", "せ" => "se", "そ" => "so",
|
|
129
|
+
"た" => "ta", "ち" => "chi", "つ" => "tsu", "て" => "te", "と" => "to",
|
|
130
|
+
"な" => "na", "に" => "ni", "ぬ" => "nu", "ね" => "ne", "の" => "no",
|
|
131
|
+
"は" => "ha", "ひ" => "hi", "ふ" => "fu", "へ" => "he", "ほ" => "ho",
|
|
132
|
+
"ま" => "ma", "み" => "mi", "む" => "mu", "め" => "me", "も" => "mo",
|
|
133
|
+
"や" => "ya", "ゆ" => "yu", "よ" => "yo",
|
|
134
|
+
"ら" => "ra", "り" => "ri", "る" => "ru", "れ" => "re", "ろ" => "ro",
|
|
135
|
+
"わ" => "wa", "ゐ" => "wi", "ゑ" => "we", "を" => "o",
|
|
136
|
+
"ん" => "n",
|
|
137
|
+
"が" => "ga", "ぎ" => "gi", "ぐ" => "gu", "げ" => "ge", "ご" => "go",
|
|
138
|
+
"ざ" => "za", "じ" => "ji", "ず" => "zu", "ぜ" => "ze", "ぞ" => "zo",
|
|
139
|
+
"だ" => "da", "ぢ" => "ji", "づ" => "zu", "で" => "de", "ど" => "do",
|
|
140
|
+
"ば" => "ba", "び" => "bi", "ぶ" => "bu", "べ" => "be", "ぼ" => "bo",
|
|
141
|
+
"ぱ" => "pa", "ぴ" => "pi", "ぷ" => "pu", "ぺ" => "pe", "ぽ" => "po",
|
|
142
|
+
"ぁ" => "a", "ぃ" => "i", "ぅ" => "u", "ぇ" => "e", "ぉ" => "o",
|
|
143
|
+
"ゔ" => "vu", "ゎ" => "wa", "ー" => "-"
|
|
144
|
+
}.freeze
|
|
145
|
+
|
|
146
|
+
def self.hiragana_to_romaji(hira)
|
|
147
|
+
# Handle digraphs first
|
|
148
|
+
s = hira.dup
|
|
149
|
+
DIGRAPHS.each { |k, v| s.gsub!(k, v) }
|
|
150
|
+
|
|
151
|
+
result = String.new
|
|
152
|
+
chars = s.chars
|
|
153
|
+
i = 0
|
|
154
|
+
while i < chars.length
|
|
155
|
+
ch = chars[i]
|
|
156
|
+
nx = chars[i + 1]
|
|
157
|
+
|
|
158
|
+
# sokuon (small tsu)
|
|
159
|
+
if ch == "っ" && nx
|
|
160
|
+
cons = case nx
|
|
161
|
+
when "c" then "t" # chu -> c but doubled as t (match 'c' + 'h')
|
|
162
|
+
when /^[a-z]/ then nx[0]
|
|
163
|
+
end
|
|
164
|
+
# If next piece already converted (e.g., from digraph), take first consonant of romaji
|
|
165
|
+
if cons.nil?
|
|
166
|
+
# Look ahead: convert nx to romaji to get first consonant
|
|
167
|
+
nx_romaji = BASIC[nx] || nx
|
|
168
|
+
cons = nx_romaji[0]
|
|
169
|
+
end
|
|
170
|
+
result << cons if cons
|
|
171
|
+
i += 1
|
|
172
|
+
next
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
if ch == "ー"
|
|
176
|
+
# Prolonged sound mark – skip simple handling
|
|
177
|
+
i += 1
|
|
178
|
+
next
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
romaji = BASIC[ch]
|
|
182
|
+
result << (romaji || ch)
|
|
183
|
+
i += 1
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# Simple normalization: "nn" before vowels -> "n" (basic handling)
|
|
187
|
+
result.gsub(/n(?=[aiueoy])/, "n")
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def self.backoff_for(attempt, base: 0.5)
|
|
191
|
+
# Exponential backoff with small jitter
|
|
192
|
+
sleep_s = base * (2**(attempt - 1))
|
|
193
|
+
jitter = rand * 0.05
|
|
194
|
+
sleep_s + jitter
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: kanji-translator
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Hiromu Kodani
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: nokogiri
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '1.16'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '1.16'
|
|
26
|
+
description: |-
|
|
27
|
+
Fetches readings for Japanese Kanji from yomikatawa.com and converts them
|
|
28
|
+
to hiragana, katakana, or Hepburn-style romaji. Includes timeout/retry
|
|
29
|
+
handling and a simple API.
|
|
30
|
+
email:
|
|
31
|
+
- kodani@dreaw.jp
|
|
32
|
+
executables: []
|
|
33
|
+
extensions: []
|
|
34
|
+
extra_rdoc_files: []
|
|
35
|
+
files:
|
|
36
|
+
- CHANGELOG.md
|
|
37
|
+
- CODE_OF_CONDUCT.md
|
|
38
|
+
- LICENSE.txt
|
|
39
|
+
- README.md
|
|
40
|
+
- Rakefile
|
|
41
|
+
- lib/kanji/translator.rb
|
|
42
|
+
- lib/kanji/translator/core_ext/string.rb
|
|
43
|
+
- lib/kanji/translator/version.rb
|
|
44
|
+
- sig/kanji/translator.rbs
|
|
45
|
+
homepage: https://github.com/dw-kodani/kanji-translator
|
|
46
|
+
licenses:
|
|
47
|
+
- MIT
|
|
48
|
+
metadata:
|
|
49
|
+
homepage_uri: https://github.com/dw-kodani/kanji-translator
|
|
50
|
+
source_code_uri: https://github.com/dw-kodani/kanji-translator
|
|
51
|
+
changelog_uri: https://github.com/dw-kodani/kanji-translator/blob/master/CHANGELOG.md
|
|
52
|
+
rubygems_mfa_required: 'true'
|
|
53
|
+
rdoc_options: []
|
|
54
|
+
require_paths:
|
|
55
|
+
- lib
|
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: 3.2.0
|
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
|
+
requirements:
|
|
63
|
+
- - ">="
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: '0'
|
|
66
|
+
requirements: []
|
|
67
|
+
rubygems_version: 3.6.9
|
|
68
|
+
specification_version: 4
|
|
69
|
+
summary: Convert Kanji to Hiragana, Katakana, and Romaji
|
|
70
|
+
test_files: []
|