jtcg_locale_detector 1.0.1

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.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +37 -0
  3. data/PACKAGING_SUMMARY.md +195 -0
  4. data/README.md +226 -0
  5. data/bin/locale-detector +159 -0
  6. data/jtcg_locale_detector.gemspec +48 -0
  7. data/lib/locale_detector/client.rb +163 -0
  8. data/lib/locale_detector/detector.rb +46 -0
  9. data/lib/locale_detector/version.rb +3 -0
  10. data/lib/locale_detector.rb +25 -0
  11. data/locale_detector.gemspec +46 -0
  12. data/python/cli.py +220 -0
  13. data/python/requirements.txt +8 -0
  14. data/python/src/__init__.py +10 -0
  15. data/python/src/__pycache__/__init__.cpython-311.pyc +0 -0
  16. data/python/src/__pycache__/__init__.cpython-313.pyc +0 -0
  17. data/python/src/__pycache__/locale_data.cpython-311.pyc +0 -0
  18. data/python/src/__pycache__/locale_data.cpython-313.pyc +0 -0
  19. data/python/src/__pycache__/locale_detector.cpython-311.pyc +0 -0
  20. data/python/src/__pycache__/locale_detector.cpython-313.pyc +0 -0
  21. data/python/src/artifacts/fasttext/lid.176.bin +0 -0
  22. data/python/src/artifacts/fasttext/lid.176.ftz +0 -0
  23. data/python/src/download_fasttext.py +69 -0
  24. data/python/src/locale_data.py +178 -0
  25. data/python/src/locale_detector.py +534 -0
  26. data/python/src/locale_detector_c.c +403 -0
  27. data/python/src/locale_detector_c.h +37 -0
  28. data/python/src/locale_detector_cy.cpp +23126 -0
  29. data/python/src/locale_detector_cy.cpython-311-darwin.so +0 -0
  30. data/python/src/locale_detector_cy.cpython-313-darwin.so +0 -0
  31. data/python/src/locale_detector_cy.html +6460 -0
  32. data/python/src/locale_detector_cy.pyx +501 -0
  33. data/python/src/utils/__init__.py +1 -0
  34. data/python/src/utils/__pycache__/__init__.cpython-311.pyc +0 -0
  35. data/python/src/utils/__pycache__/__init__.cpython-313.pyc +0 -0
  36. data/python/src/utils/__pycache__/data_utils.cpython-311.pyc +0 -0
  37. data/python/src/utils/__pycache__/data_utils.cpython-313.pyc +0 -0
  38. data/python/src/utils/data_utils.py +50 -0
  39. data/python/src/utils/data_utils_cy.cpp +10086 -0
  40. data/python/src/utils/data_utils_cy.cpython-311-darwin.so +0 -0
  41. data/python/src/utils/data_utils_cy.cpython-313-darwin.so +0 -0
  42. data/python/src/utils/data_utils_cy.html +600 -0
  43. data/python/src/utils/data_utils_cy.pyx +94 -0
  44. data/python/src/zhon/__init__.py +7 -0
  45. data/python/src/zhon/__pycache__/__init__.cpython-311.pyc +0 -0
  46. data/python/src/zhon/__pycache__/hanzi.cpython-311.pyc +0 -0
  47. data/python/src/zhon/__pycache__/pinyin.cpython-311.pyc +0 -0
  48. data/python/src/zhon/__pycache__/zhuyin.cpython-311.pyc +0 -0
  49. data/python/src/zhon/cedict/__init__.py +14 -0
  50. data/python/src/zhon/cedict/__pycache__/__init__.cpython-311.pyc +0 -0
  51. data/python/src/zhon/cedict/__pycache__/all.cpython-311.pyc +0 -0
  52. data/python/src/zhon/cedict/__pycache__/simplified.cpython-311.pyc +0 -0
  53. data/python/src/zhon/cedict/__pycache__/traditional.cpython-311.pyc +0 -0
  54. data/python/src/zhon/cedict/all.py +4 -0
  55. data/python/src/zhon/cedict/simplified.py +4 -0
  56. data/python/src/zhon/cedict/traditional.py +4 -0
  57. data/python/src/zhon/hanzi.py +81 -0
  58. data/python/src/zhon/pinyin.py +187 -0
  59. data/python/src/zhon/zhuyin.py +46 -0
  60. metadata +198 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8aabdb6f28de3e21527b5a81fa9eac577fb16f988c132f3c528a8b4b88c6c061
4
+ data.tar.gz: 2f017a60989427af6030b4d857dc7ceb0f772f4ce5999bda2710779ce874c859
5
+ SHA512:
6
+ metadata.gz: 0a63302d720b0ddb6fc1ac6d1c162b7e948d80f47c32625a8bc20599c46c1683a0a8f63730b136f915af7999ebd0bf2610bc4c0e0ac489a14c63e88723fb265d
7
+ data.tar.gz: 6f6b3ea226dae8b0e37c5bfc88e758edea6ebd25a573149161b0de62e6952e1f777265503f83abd98f8616b93e801e176b87f58418a184541dde90ee6e5be1e4
data/CHANGELOG.md ADDED
@@ -0,0 +1,37 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [1.0.1] - 2025-06-01
9
+
10
+ ### Added
11
+ - Initial Ruby gem release
12
+ - Support for 176 languages detection
13
+ - Specialized Chinese Traditional/Simplified detection
14
+ - Command-line interface
15
+ - Batch processing capabilities
16
+ - Multiple detection modes (conversion, ratio, both)
17
+ - Multiple output formats (simple, json, detailed)
18
+ - Low memory mode support
19
+ - File detection support
20
+ - Comprehensive test suite
21
+ - RuboCop configuration
22
+ - Documentation and examples
23
+
24
+ ### Features
25
+ - FastText-based language identification
26
+ - OpenCC-based Chinese variant detection
27
+ - Character ratio analysis for Chinese
28
+ - Async batch processing
29
+ - Memory-optimized operations
30
+ - Cross-platform compatibility (macOS, Linux, Windows)
31
+
32
+ ### Dependencies
33
+ - Ruby 2.7.0+
34
+ - Python 3.11+
35
+ - FastText
36
+ - OpenCC
37
+ - zhon
@@ -0,0 +1,195 @@
1
+ # LocaleDetector Ruby Gem 打包總結
2
+
3
+ ## 概述
4
+
5
+ 已成功將 LocaleDetector 語言檢測專案打包成 Ruby gem,讓 Ruby 開發者能夠輕鬆使用這個強大的多語言檢測工具。
6
+
7
+ ## 專案結構
8
+
9
+ ```
10
+ ruby-gem/
11
+ ├── lib/ # Ruby 源代碼
12
+ │ ├── locale_detector.rb # 主模組
13
+ │ └── locale_detector/
14
+ │ ├── version.rb # 版本定義
15
+ │ ├── client.rb # 主要客戶端類
16
+ │ └── detector.rb # 檢測器輔助類
17
+ ├── python/ # Python 組件
18
+ │ ├── cli.py # Python CLI 腳本
19
+ │ ├── requirements.txt # Python 依賴
20
+ │ └── src/ # Python 源代碼
21
+ ├── bin/
22
+ │ └── locale-detector # Ruby CLI 工具
23
+ ├── spec/ # 測試文件
24
+ ├── examples/
25
+ │ └── basic_usage.rb # 使用範例
26
+ ├── locale_detector.gemspec # Gem 規格
27
+ ├── Gemfile # Ruby 依賴
28
+ ├── Rakefile # 構建任務
29
+ ├── README.md # 文檔
30
+ ├── CHANGELOG.md # 變更日誌
31
+ ├── install.sh # 安裝腳本
32
+ └── build.sh # 構建腳本
33
+ ```
34
+
35
+ ## 核心特性
36
+
37
+ ### 1. Ruby API
38
+ - **簡單檢測**: `LocaleDetector.detect(text)`
39
+ - **批次檢測**: `LocaleDetector.detect_batch(texts)`
40
+ - **檔案檢測**: `LocaleDetector.detect_file(path)`
41
+ - **詳細信息**: `client.detect_with_details(text)`
42
+
43
+ ### 2. 命令列工具
44
+ - 支援單一文本、檔案和批次檢測
45
+ - 多種輸出格式(simple, json, detailed)
46
+ - 不同檢測模式(conversion, ratio, both)
47
+ - 低記憶體模式
48
+
49
+ ### 3. 語言支援
50
+ - 176 種語言檢測
51
+ - 特別優化中文繁簡體檢測
52
+ - 支援 zh-TW, zh-CN, zh-HK, zh-MO, zh-SG
53
+
54
+ ## 技術實現
55
+
56
+ ### 架構設計
57
+ 本 Ruby gem 採用**混合架構**,結合 Ruby 的易用性和 Python 的機器學習生態:
58
+
59
+ 1. **Ruby 層**: 提供友好的 API 和命令列界面
60
+ 2. **Python 層**: 執行實際的語言檢測工作
61
+ 3. **IPC 通訊**: 通過 subprocess 調用 Python CLI
62
+
63
+ ### 核心組件
64
+
65
+ #### LocaleDetector::Client
66
+ - 管理 Python 環境檢查
67
+ - 處理命令構建和執行
68
+ - 解析 Python 輸出結果
69
+ - 錯誤處理和異常管理
70
+
71
+ #### LocaleDetector::Detector
72
+ - 提供靜態信息(支援語言、檢測模式等)
73
+ - 語言支援檢查
74
+ - 中文變體識別
75
+
76
+ ## 安裝和使用
77
+
78
+ ### 系統要求
79
+ - Ruby 2.7.0+
80
+ - Python 3.11+
81
+ - pip
82
+
83
+ ### 安裝步驟
84
+
85
+ 1. **安裝 gem**:
86
+ ```bash
87
+ gem install locale_detector-1.0.1.gem
88
+ ```
89
+
90
+ 2. **安裝 Python 依賴**:
91
+ ```bash
92
+ pip install -r python/requirements.txt
93
+ ```
94
+
95
+ ### 快速開始
96
+
97
+ ```ruby
98
+ require 'locale_detector'
99
+
100
+ # 基本檢測
101
+ result = LocaleDetector.detect("Hello world")
102
+ puts result["locale"] # => "en-US"
103
+
104
+ # 中文檢測
105
+ result = LocaleDetector.detect("繁體中文")
106
+ puts result["locale"] # => "zh-TW"
107
+
108
+ # 批次檢測
109
+ results = LocaleDetector.detect_batch(["Hello", "你好", "こんにちは"])
110
+ ```
111
+
112
+ ### 命令列使用
113
+
114
+ ```bash
115
+ # 基本檢測
116
+ locale-detector "Hello world"
117
+
118
+ # 從檔案檢測
119
+ locale-detector --file input.txt
120
+
121
+ # JSON 輸出
122
+ locale-detector "測試" --output-format json
123
+
124
+ # 詳細信息
125
+ locale-detector "測試" --details
126
+ ```
127
+
128
+ ## 測試和品質保證
129
+
130
+ ### 測試套件
131
+ - RSpec 測試框架
132
+ - 涵蓋基本檢測、批次處理、檔案檢測
133
+ - 錯誤處理測試
134
+ - 配置選項測試
135
+
136
+ ### 代碼品質
137
+ - RuboCop 代碼風格檢查
138
+ - 文檔覆蓋率
139
+ - 持續集成準備
140
+
141
+ ## 性能考量
142
+
143
+ ### 優點
144
+ - **快速啟動**: Ruby API 響應迅速
145
+ - **記憶體效率**: 支援低記憶體模式
146
+ - **批次處理**: 高效處理多個文本
147
+
148
+ ### 限制
149
+ - **進程開銷**: 每次檢測需啟動 Python 進程
150
+ - **依賴複雜**: 需要 Python 環境和機器學習依賴
151
+
152
+ ### 最佳化建議
153
+ - 批次處理多個文本而非單獨檢測
154
+ - 重用 Client 實例避免重複驗證
155
+ - 在生產環境中考慮快取策略
156
+
157
+ ## 部署選項
158
+
159
+ ### 1. 傳統部署
160
+ - 安裝 Ruby gem
161
+ - 確保 Python 環境可用
162
+ - 安裝 Python 依賴
163
+
164
+ ### 2. 容器化部署
165
+ - 創建包含 Ruby + Python 的 Docker 鏡像
166
+ - 預安裝所有依賴
167
+ - 適合微服務架構
168
+
169
+ ### 3. 無伺服器部署
170
+ - 適合 AWS Lambda、Google Cloud Functions
171
+ - 需要自定義運行時或容器
172
+
173
+ ## 未來改進方向
174
+
175
+ ### 短期改進
176
+ 1. **連接池**: 維持 Python 進程池減少啟動開銷
177
+ 2. **快取機制**: 本地快取常見檢測結果
178
+ 3. **異步支援**: Ruby async/await 支援
179
+
180
+ ### 長期計劃
181
+ 1. **純 Ruby 實現**: 移植核心演算法到 Ruby
182
+ 2. **C 擴展**: 編寫高效能 C 擴展
183
+ 3. **gRPC 服務**: 獨立語言檢測服務
184
+
185
+ ## 結論
186
+
187
+ LocaleDetector Ruby gem 成功地橋接了 Ruby 和 Python 生態系統,為 Ruby 開發者提供了強大的語言檢測功能。雖然存在一些性能開銷,但通過合理的使用方式(如批次處理)可以有效緩解。
188
+
189
+ 這個 gem 特別適合:
190
+ - 需要多語言支援的 Ruby 應用
191
+ - 中文繁簡體檢測需求
192
+ - 文檔處理和國際化項目
193
+ - 多語言內容管理系統
194
+
195
+ gem 包大小為 129MB,主要包含機器學習模型和 Python 依賴,在合理範圍內。
data/README.md ADDED
@@ -0,0 +1,226 @@
1
+ # LocaleDetector Ruby Gem
2
+
3
+ 多語言地區檢測工具的Ruby綁定,特別針對中文繁簡體檢測進行優化。
4
+
5
+ ## 特色功能
6
+
7
+ - 🌍 支援 176 種語言檢測
8
+ - 🇨🇳 特別優化中文繁體 (zh-TW) 和簡體 (zh-CN) 檢測
9
+ - ⚡ 高效能檢測
10
+ - 📊 多種檢測模式和輸出格式
11
+ - 🔧 命令列工具和 Ruby API
12
+ - 🐍 基於成熟的Python語言檢測引擎
13
+
14
+ ## 安裝
15
+
16
+ 將此行添加到應用程式的 Gemfile:
17
+
18
+ ```ruby
19
+ gem 'locale_detector'
20
+ ```
21
+
22
+ 然後執行:
23
+
24
+ ```bash
25
+ $ bundle install
26
+ ```
27
+
28
+ 或者手動安裝:
29
+
30
+ ```bash
31
+ $ gem install locale_detector
32
+ ```
33
+
34
+ ### 系統要求
35
+
36
+ - Ruby 2.7.0 或更高版本
37
+ - Python 3.11 或更高版本
38
+ - pip (用於安裝 Python 依賴)
39
+
40
+ ### Python 依賴安裝
41
+
42
+ 安裝 gem 後,需要安裝 Python 依賴:
43
+
44
+ ```bash
45
+ $ rake install_python_deps
46
+ ```
47
+
48
+ 或手動安裝:
49
+
50
+ ```bash
51
+ $ pip install -r python/requirements.txt
52
+ ```
53
+
54
+ ## 使用方法
55
+
56
+ ### Ruby API
57
+
58
+ #### 基本檢測
59
+
60
+ ```ruby
61
+ require 'locale_detector'
62
+
63
+ # 檢測單一文本
64
+ result = LocaleDetector.detect("Hello world")
65
+ puts result["locale"] # => "en-US"
66
+
67
+ # 檢測中文繁體
68
+ result = LocaleDetector.detect("繁體中文測試")
69
+ puts result["locale"] # => "zh-TW"
70
+
71
+ # 檢測中文簡體
72
+ result = LocaleDetector.detect("简体中文测试")
73
+ puts result["locale"] # => "zh-CN"
74
+ ```
75
+
76
+ #### 批次檢測
77
+
78
+ ```ruby
79
+ texts = ["Hello world", "繁體中文", "简体中文", "Bonjour le monde"]
80
+ results = LocaleDetector.detect_batch(texts)
81
+
82
+ results.each_with_index do |result, index|
83
+ puts "Text #{index + 1}: #{result['locale']}"
84
+ end
85
+ ```
86
+
87
+ #### 從檔案檢測
88
+
89
+ ```ruby
90
+ result = LocaleDetector.detect_file("path/to/text/file.txt")
91
+ puts result["locale"]
92
+ ```
93
+
94
+ #### 進階選項
95
+
96
+ ```ruby
97
+ # 使用不同的檢測模式
98
+ client = LocaleDetector::Client.new(
99
+ mode: "ratio", # 'conversion', 'ratio', 'both'
100
+ low_memory: false, # 使用低記憶體模式
101
+ output_format: "json" # 'simple', 'json', 'detailed'
102
+ )
103
+
104
+ result = client.detect("測試文本")
105
+
106
+ # 獲取詳細檢測信息
107
+ detailed_result = client.detect_with_details("測試文本")
108
+ puts detailed_result.inspect
109
+ ```
110
+
111
+ ### 命令列工具
112
+
113
+ 安裝 gem 後,您可以使用 `locale-detector` 命令:
114
+
115
+ ```bash
116
+ # 檢測單一文本
117
+ $ locale-detector "Hello world"
118
+ en-US
119
+
120
+ # 從檔案檢測
121
+ $ locale-detector --file input.txt
122
+
123
+ # 批次處理
124
+ $ locale-detector --batch file1.txt file2.txt
125
+
126
+ # 使用不同模式
127
+ $ locale-detector "中文文本" --mode ratio --details
128
+
129
+ # JSON 輸出
130
+ $ locale-detector "測試" --output-format json
131
+ {
132
+ "locale": "zh-TW"
133
+ }
134
+
135
+ # 詳細信息
136
+ $ locale-detector "測試" --output-format detailed --details
137
+ ```
138
+
139
+ ### 支援的選項
140
+
141
+ #### 檢測模式
142
+
143
+ - `conversion`: 快速模式,使用字符轉換比較
144
+ - `ratio`: 準確模式,使用字符比例分析(預設)
145
+ - `both`: 混合模式,結合兩種方法
146
+
147
+ #### 輸出格式
148
+
149
+ - `simple`: 簡單文本輸出(預設)
150
+ - `json`: JSON 格式輸出
151
+ - `detailed`: 詳細信息輸出
152
+
153
+ ## 支援的語言
154
+
155
+ 本工具支援 176 種語言的檢測,包括但不限於:
156
+
157
+ - 中文變體:`zh-TW` (繁體), `zh-CN` (簡體), `zh-HK`, `zh-MO`, `zh-SG`
158
+ - 歐洲語言:`en`, `es`, `fr`, `de`, `it`, `pt`, `ru`, `nl`, `sv`, `no`, `da`
159
+ - 亞洲語言:`ja`, `ko`, `th`, `vi`, `hi`, `ar`, `he`, `tr`
160
+ - 其他語言:查看 `LocaleDetector::Detector.supported_languages`
161
+
162
+ ## API 參考
163
+
164
+ ### LocaleDetector
165
+
166
+ #### 類方法
167
+
168
+ - `LocaleDetector.detect(text, **options)` - 檢測單一文本
169
+ - `LocaleDetector.detect_batch(texts, **options)` - 批次檢測
170
+ - `LocaleDetector.detect_file(file_path, **options)` - 從檔案檢測
171
+
172
+ ### LocaleDetector::Client
173
+
174
+ #### 實例方法
175
+
176
+ - `#detect(text)` - 檢測單一文本
177
+ - `#detect_batch(texts)` - 批次檢測
178
+ - `#detect_file(file_path)` - 從檔案檢測
179
+ - `#detect_with_details(text)` - 獲取詳細檢測信息
180
+
181
+ #### 選項
182
+
183
+ - `mode`: 檢測模式('conversion', 'ratio', 'both')
184
+ - `low_memory`: 使用低記憶體模式
185
+ - `output_format`: 輸出格式('simple', 'json', 'detailed')
186
+ - `encoding`: 檔案編碼(預設: 'utf-8')
187
+
188
+ ### LocaleDetector::Detector
189
+
190
+ #### 類方法
191
+
192
+ - `Detector.supported_languages` - 取得支援的語言列表
193
+ - `Detector.chinese_variants` - 取得中文變體信息
194
+ - `Detector.detection_modes` - 取得可用的檢測模式
195
+ - `Detector.language_supported?(language_code)` - 檢查是否支援某語言
196
+ - `Detector.chinese_variant?(locale)` - 檢查是否為中文變體
197
+
198
+ ## 開發
199
+
200
+ 獲取原始碼後,執行 `bin/setup` 安裝依賴。然後,執行 `rake spec` 來運行測試。您也可以執行 `bin/console` 來取得互動式提示符進行實驗。
201
+
202
+ 要將此 gem 安裝到本地機器,執行 `bundle exec rake install`。要發布新版本,更新 `version.rb` 中的版本號,然後執行 `bundle exec rake release`,這將創建一個 git 標籤並推送 `.gem` 檔案到 [rubygems.org](https://rubygems.org)。
203
+
204
+ ## 貢獻
205
+
206
+ Bug 報告和 pull request 歡迎在 GitHub 上提交:https://github.com/jtcg/locale-detector
207
+
208
+ ## 授權
209
+
210
+ ## 作者與貢獻者
211
+
212
+ - **原作者**: [原作者姓名](mailto:original.author@example.com) - 核心語言檢測引擎開發
213
+ - **Ruby Gem 打包**: [JTCG Team](mailto:mark@j-tcg.com) - Ruby 綁定與 gem 打包
214
+ - **維護者**: JTCG Team
215
+
216
+ ## 開源授權
217
+
218
+ 此 gem 可在 [MIT License](https://opensource.org/licenses/MIT) 條款下使用。
219
+
220
+ ## 致謝
221
+
222
+ 本工具基於以下開源專案:
223
+
224
+ - [FastText](https://fasttext.cc/) - Facebook 的語言檢測模型
225
+ - [OpenCC](https://github.com/BYVoid/OpenCC) - 中文繁簡轉換
226
+ - [zhon](https://github.com/tsroten/zhon) - 中文字符數據
@@ -0,0 +1,159 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "optparse"
4
+ require "json"
5
+ require_relative "../lib/locale_detector"
6
+
7
+ options = {
8
+ mode: "ratio",
9
+ low_memory: false,
10
+ output_format: "simple",
11
+ details: false,
12
+ encoding: "utf-8"
13
+ }
14
+
15
+ parser = OptionParser.new do |opts|
16
+ opts.banner = "Usage: locale-detector [OPTIONS] TEXT"
17
+ opts.separator ""
18
+ opts.separator "多語言地區檢測工具 - 特別優化中文繁簡體檢測"
19
+ opts.separator ""
20
+ opts.separator "Options:"
21
+
22
+ opts.on("-f", "--file FILE", "從檔案讀取文本進行檢測") do |file|
23
+ options[:file] = file
24
+ end
25
+
26
+ opts.on("--batch FILES", Array, "批次處理多個檔案") do |files|
27
+ options[:batch] = files
28
+ end
29
+
30
+ opts.on("--mode MODE", %w[conversion ratio both],
31
+ "中文檢測模式: conversion(快速), ratio(準確), both(混合) [預設: ratio]") do |mode|
32
+ options[:mode] = mode
33
+ end
34
+
35
+ opts.on("--low-memory", "使用低記憶體模式") do
36
+ options[:low_memory] = true
37
+ end
38
+
39
+ opts.on("--output-format FORMAT", %w[simple json detailed],
40
+ "輸出格式: simple(簡單), json(JSON格式), detailed(詳細信息) [預設: simple]") do |format|
41
+ options[:output_format] = format
42
+ end
43
+
44
+ opts.on("--details", "顯示檢測詳細信息") do
45
+ options[:details] = true
46
+ end
47
+
48
+ opts.on("--encoding ENCODING", "檔案編碼格式 [預設: utf-8]") do |encoding|
49
+ options[:encoding] = encoding
50
+ end
51
+
52
+ opts.on("-o", "--output FILE", "將結果輸出到檔案") do |output|
53
+ options[:output] = output
54
+ end
55
+
56
+ opts.on("-h", "--help", "顯示幫助信息") do
57
+ puts opts
58
+ exit
59
+ end
60
+
61
+ opts.on("--version", "顯示版本信息") do
62
+ puts "locale-detector #{LocaleDetector::VERSION}"
63
+ exit
64
+ end
65
+ end
66
+
67
+ begin
68
+ parser.parse!
69
+ rescue OptionParser::InvalidOption => e
70
+ puts "Error: #{e.message}"
71
+ puts parser
72
+ exit 1
73
+ end
74
+
75
+ def format_output(result, format)
76
+ case format
77
+ when "json"
78
+ JSON.pretty_generate(result)
79
+ when "detailed"
80
+ if result.is_a?(Hash)
81
+ result.map { |k, v| "#{k}: #{v}" }.join("\n")
82
+ else
83
+ result.to_s
84
+ end
85
+ else
86
+ if result.is_a?(Hash) && result["locale"]
87
+ result["locale"]
88
+ else
89
+ result.to_s
90
+ end
91
+ end
92
+ end
93
+
94
+ def handle_output(content, output_file)
95
+ if output_file
96
+ File.write(output_file, content)
97
+ puts "結果已輸出到: #{output_file}"
98
+ else
99
+ puts content
100
+ end
101
+ end
102
+
103
+ begin
104
+ client = LocaleDetector::Client.new(
105
+ mode: options[:mode],
106
+ low_memory: options[:low_memory],
107
+ output_format: options[:output_format],
108
+ encoding: options[:encoding]
109
+ )
110
+
111
+ if options[:batch]
112
+ # 批次處理
113
+ results = {}
114
+ options[:batch].each do |file|
115
+ result = if options[:details]
116
+ client.detect_with_details(File.read(file, encoding: options[:encoding]))
117
+ else
118
+ client.detect_file(file)
119
+ end
120
+ results[file] = result
121
+ rescue StandardError => e
122
+ results[file] = { error: e.message }
123
+ end
124
+ output = format_output(results, options[:output_format])
125
+ handle_output(output, options[:output])
126
+
127
+ elsif options[:file]
128
+ # 從檔案檢測
129
+ result = if options[:details]
130
+ client.detect_with_details(File.read(options[:file], encoding: options[:encoding]))
131
+ else
132
+ client.detect_file(options[:file])
133
+ end
134
+ output = format_output(result, options[:output_format])
135
+ handle_output(output, options[:output])
136
+
137
+ elsif !ARGV.empty?
138
+ # 檢測命令列參數中的文本
139
+ text = ARGV.join(" ")
140
+ result = if options[:details]
141
+ client.detect_with_details(text)
142
+ else
143
+ client.detect(text)
144
+ end
145
+ output = format_output(result, options[:output_format])
146
+ handle_output(output, options[:output])
147
+
148
+ else
149
+ puts "Error: 請提供要檢測的文本或檔案"
150
+ puts parser
151
+ exit 1
152
+ end
153
+ rescue LocaleDetector::Error => e
154
+ puts "Error: #{e.message}"
155
+ exit 1
156
+ rescue StandardError => e
157
+ puts "Unexpected error: #{e.message}"
158
+ exit 1
159
+ end
@@ -0,0 +1,48 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "jtcg_locale_detector"
3
+ spec.version = "1.0.1"
4
+
5
+ # 原作者(模組開發者)
6
+ spec.authors = ["Original mark", "JTCG Team"]
7
+ spec.email = ["mark@j-tcg.com", "enor@j-tcg.com"]
8
+
9
+ spec.summary = "多語言地區檢測工具,特別優化中文繁簡體檢測"
10
+ spec.description = "Multi-language locale detector with specialized Chinese variant detection. " \
11
+ "Uses FastText for initial language identification and multiple algorithms " \
12
+ "for Chinese variant detection. Ruby gem packaged by JTCG Team."
13
+ spec.homepage = "https://github.com/jtcg/locale-detector"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/jtcg/locale-detector"
19
+ spec.metadata["changelog_uri"] = "https://github.com/jtcg/locale-detector/blob/main/CHANGELOG.md"
20
+
21
+ # 作者角色說明
22
+ spec.metadata["packaged_by"] = "JTCG Team <enor@j-tcg.com>"
23
+ spec.metadata["original_author"] = "Original mark <mark@j-tcg.com>"
24
+ spec.metadata["maintainer"] = "JTCG Team"
25
+ spec.metadata["funding_uri"] = "https://github.com/sponsors/jtcg"
26
+
27
+ # Specify which files should be added to the gem when it is released.
28
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
29
+ Dir["{bin,lib,python}/**/*", "*.md", "*.txt", "*.gemspec"]
30
+ end
31
+
32
+ spec.bindir = "bin"
33
+ spec.executables = ["locale-detector"]
34
+ spec.require_paths = ["lib"]
35
+
36
+ # Dependencies
37
+ spec.add_dependency "ffi", "~> 1.15"
38
+ spec.add_dependency "json", "~> 2.6"
39
+
40
+ # Development dependencies
41
+ spec.add_development_dependency "bundler", ">= 1.17"
42
+ spec.add_development_dependency "rake", "~> 13.0"
43
+ spec.add_development_dependency "rspec", "~> 3.12"
44
+ spec.add_development_dependency "rubocop", "~> 1.50"
45
+
46
+ # Metadata
47
+ spec.metadata["rubygems_mfa_required"] = "true"
48
+ end