项目作者: vhyza

项目描述 :
Ruby bindings for Chromium Compact Language Detector
高级语言: C++
项目地址: git://github.com/vhyza/language_detection.git
创建时间: 2012-09-27T19:58:58Z
项目社区:https://github.com/vhyza/language_detection

开源协议:MIT License

下载


LanguageDetection

Ruby bindings for Chromium Compact Language Detector (source). This gem is using source codes from chromium-compact-language-detector port.

Installation

Add this line to your application’s Gemfile:

  1. gem 'language_detection'

And then execute:

  1. $ bundle

Or install it yourself as:

  1. $ gem install language_detection

Usage

  1. >> require 'language_detection'
  2. => true
  3. >> language = LanguageDetection.perform("This is some example text for language detection")
  4. => #<LanguageDetection::Language:0x007fae0404f628 @name="english", @code="en", @reliable=true, @text_bytes=51, @details=[#<LanguageDetection::Language:0x007fae0404eb10 @name="english", @code="en", @details=[], @percent=100, @score=49.43273905996759>]>
  5. >> language.name
  6. => "english"
  7. >> language.code
  8. => "en"
  9. >> language.reliable
  10. => true
  11. >> language.details # contains up to 3 languages sorted by score
  12. => [#<LanguageDetection::Language:0x007fae0404eb10 @name="english", @code="en", @details=[], @percent=100, @score=49.43273905996759>]
  13. >> language.details.first.percent
  14. => 100
  15. >> language.details.first.score
  16. => 49.43273905996759

the other way is to include LanguageDetection module in your class

  1. class Article
  2. include LanguageDetection
  3. attr_accessor :title, :content
  4. def initialize(params = {})
  5. @title = params[:title]
  6. @content = params[:content]
  7. end
  8. def to_s
  9. "#{title}\n#{content}"
  10. end
  11. end

which provides Article#language method using Article#to_s method as parameter

  1. >> article = Article.new :title => "Web development that doesn't hurt", :content => "Tens of thousands of Rails applications are already live..."
  2. >> article.language
  3. => #<LanguageDetection::Language:0x007fae049dd8e8 @name="english", @code="en", @reliable=true, @text_bytes=93, @details=[#<LanguageDetection::Language:0x007fae049dd118 @name="english", @code="en", @details=[], @percent=100, @score=80.22690437601297>]>

or you can add String#language method by require 'language_detection/string'

  1. >> require 'language_detection'
  2. => true
  3. >> require 'language_detection/string'
  4. => true
  5. >> "Web development that doesn't hurt".language
  6. => #<LanguageDetection::Language:0x007fae049cfec8 @name="english", @code="en", @reliable=true, @text_bytes=36, @details=[#<LanguageDetection::Language:0x007fae049cf7e8 @name="english", @code="en", @details=[], @percent=100, @score=39.70826580226905>]>

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request