LocaleDetector Ruby Gem
多語言地區檢測工具的Ruby綁定,特別針對中文繁簡體檢測進行優化。
特色功能
- 🌍 支援 176 種語言檢測
- 🇨🇳 特別優化中文繁體 (zh-TW) 和簡體 (zh-CN) 檢測
- ⚡ 高效能檢測
- 📊 多種檢測模式和輸出格式
- 🔧 命令列工具和 Ruby API
- 🐍 基於成熟的Python語言檢測引擎
安裝
將此行添加到應用程式的 Gemfile:
gem 'locale_detector'
然後執行:
$ bundle install
或者手動安裝:
$ gem install locale_detector
系統要求
- Ruby 2.7.0 或更高版本
- Python 3.11 或更高版本
- pip (用於安裝 Python 依賴)
Python 依賴安裝
安裝 gem 後,需要安裝 Python 依賴:
$ rake install_python_deps
或手動安裝:
$ pip install -r python/requirements.txt
使用方法
Ruby API
基本檢測
require 'locale_detector'
# 檢測單一文本
result = LocaleDetector.detect("Hello world")
puts result["locale"] # => "en-US"
# 檢測中文繁體
result = LocaleDetector.detect("繁體中文測試")
puts result["locale"] # => "zh-TW"
# 檢測中文簡體
result = LocaleDetector.detect("简体中文测试")
puts result["locale"] # => "zh-CN"
批次檢測
texts = ["Hello world", "繁體中文", "简体中文", "Bonjour le monde"]
results = LocaleDetector.detect_batch(texts)
results.each_with_index do |result, index|
puts "Text #{index + 1}: #{result['locale']}"
end
從檔案檢測
result = LocaleDetector.detect_file("path/to/text/file.txt")
puts result["locale"]
進階選項
# 使用不同的檢測模式
client = LocaleDetector::Client.new(
mode: "ratio", # 'conversion', 'ratio', 'both'
low_memory: false, # 使用低記憶體模式
output_format: "json" # 'simple', 'json', 'detailed'
)
result = client.detect("測試文本")
# 獲取詳細檢測信息
detailed_result = client.detect_with_details("測試文本")
puts detailed_result.inspect
命令列工具
安裝 gem 後,您可以使用 locale-detector 命令:
# 檢測單一文本
$ locale-detector "Hello world"
en-US
# 從檔案檢測
$ locale-detector --file input.txt
# 批次處理
$ locale-detector --batch file1.txt file2.txt
# 使用不同模式
$ locale-detector "中文文本" --mode ratio --details
# JSON 輸出
$ locale-detector "測試" --output-format json
{
"locale": "zh-TW"
}
# 詳細信息
$ locale-detector "測試" --output-format detailed --details
支援的選項
檢測模式
conversion: 快速模式,使用字符轉換比較ratio: 準確模式,使用字符比例分析(預設)both: 混合模式,結合兩種方法
輸出格式
simple: 簡單文本輸出(預設)json: JSON 格式輸出detailed: 詳細信息輸出
支援的語言
本工具支援 176 種語言的檢測,包括但不限於:
- 中文變體:
zh-TW(繁體),zh-CN(簡體),zh-HK,zh-MO,zh-SG - 歐洲語言:
en,es,fr,de,it,pt,ru,nl,sv,no,da - 亞洲語言:
ja,ko,th,vi,hi,ar,he,tr - 其他語言:查看
LocaleDetector::Detector.supported_languages
API 參考
LocaleDetector
類方法
LocaleDetector.detect(text, **options)- 檢測單一文本LocaleDetector.detect_batch(texts, **options)- 批次檢測LocaleDetector.detect_file(file_path, **options)- 從檔案檢測
LocaleDetector::Client
實例方法
#detect(text)- 檢測單一文本#detect_batch(texts)- 批次檢測#detect_file(file_path)- 從檔案檢測#detect_with_details(text)- 獲取詳細檢測信息
選項
mode: 檢測模式('conversion', 'ratio', 'both')low_memory: 使用低記憶體模式output_format: 輸出格式('simple', 'json', 'detailed')encoding: 檔案編碼(預設: 'utf-8')
LocaleDetector::Detector
類方法
Detector.supported_languages- 取得支援的語言列表Detector.chinese_variants- 取得中文變體信息Detector.detection_modes- 取得可用的檢測模式Detector.language_supported?(language_code)- 檢查是否支援某語言Detector.chinese_variant?(locale)- 檢查是否為中文變體
開發
獲取原始碼後,執行 bin/setup 安裝依賴。然後,執行 rake spec 來運行測試。您也可以執行 bin/console 來取得互動式提示符進行實驗。
要將此 gem 安裝到本地機器,執行 bundle exec rake install。要發布新版本,更新 version.rb 中的版本號,然後執行 bundle exec rake release,這將創建一個 git 標籤並推送 .gem 檔案到 rubygems.org。
貢獻
Bug 報告和 pull request 歡迎在 GitHub 上提交:https://github.com/jtcg/locale-detector
授權
作者與貢獻者
開源授權
此 gem 可在 MIT License 條款下使用。
致謝
本工具基於以下開源專案: