Giter Club home page Giter Club logo

whatlanguage's Introduction

whatlanguage

by Peter Cooper

Text language detection. Quick, fast, memory efficient, and all in pure Ruby. Uses Bloom filters for aforementioned speed and memory benefits. It works well on texts of over 10 words in length (e.g. blog posts or comments) and very poorly on short or Twitter-esque text, so be aware.

Works with Dutch, English, Farsi, French, German, Italian, Pinyin, Swedish, Portuguese, Russian, Arabic, Finnish, Greek, Hebrew, Hungarian, Korean, Norwegian, Polish and Spanish out of the box.

Important note

This library was first built in 2007 and has received only a few minor updates over the years. There are now more efficient and effective algorithms for doing language detection which I am investigating for a future WhatLanguage.

This library has been updated to be distributed and to work on modern Ruby implementations but other than that, has had no significant improvements.

Synopsis

Full Example

require 'whatlanguage'

texts = []
texts << %q{Deux autres personnes ont été arrêtées durant la nuit}
texts << %q{The links between the attempted car bombings in Glasgow and London are becoming clearer}
texts << %q{En estado de máxima alertaen su nivel de crítico}
texts << %q{Returns the object in enum with the maximum value.}
texts << %q{Propose des données au sujet de la langue espagnole.}
texts << %q{La palabra "mezquita" se usa en español para referirse a todo tipo de edificios dedicados.}
texts << %q{اللغة التي هي هذه؟}
texts << %q{Mitä kieltä tämä on?}
texts << %q{Ποια γλώσσα είναι αυτή;}
texts << %q{באיזו שפה זה?}
texts << %q{Milyen nyelv ez?}
texts << %q{이 어떤 언어인가?}
texts << %q{Hvilket språk er dette?}
texts << %q{W jakim języku to jest?}

texts.each { |text| puts "#{text[0..18]}... is in #{text.language.to_s.capitalize}" }

Initialize WhatLanguage with all filters

wl = WhatLanguage.new(:all)

Return language with best score

wl.language(text)

Return hash with scores for all relevant languages

wl.process_text(text)

Convenience methods on String

"This is a test".language   # => :english
"This is a test".language_iso   # => :en

Initialize WhatLanguage with certain languages

wl = WhatLanguage.new(:english, :german, :french)

Requirements

None, minor libraries (BloominSimple and BitField) included with this release.

Installation

gem install whatlanguage

To test, go into irb, then:

require 'whatlanguage'
"Je suis un homme".language

## Credits

Contributions from Konrad Reiche, Salimane Adjao Moustapha, and others appreciated.

License

MIT License

Copyright (c) 2007-2016 Peter Cooper

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

whatlanguage's People

Contributors

andrewpcone avatar bf4 avatar boazadato avatar daniels avatar henrik avatar johnjohndoe avatar juancolacelli avatar konradreiche avatar lasseebert avatar michaelklishin avatar p-lambert avatar pepijn avatar peterc avatar salimane avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

whatlanguage's Issues

ISO 639-1 code instead of English string

Would you accept a pull request which would output the language in a standardised ISO 639-1 code instead of an English string ("fr" instead of "french" or "de" instead of "german") ?

We would keep the original behavior by default of course.

whatlanguage not exacly recognition return result

hi guys
my sample:
wl = WhatLanguage.new(:english, :farsi, :arabic)

wl.language('سلام خوبی')
=> :farsi

wl.language('سلام خوبی؟')
=> :arabic

this is farsi/arabic words but not recognition farsi character

wl.language('للللنننن')
=> nil

and not recognition english/farsi/arabic number

wl.language('۹۹۹')
=> nil

wl.language('23')
=> nil

Inconsistent results (Korean)

require 'whatlanguage'
=> true
"펀 치 히 어 로".language
=> :spanish
"이 어떤 언어인가?".language
=> :korean
"이 어떤 언어인가 히 어 로?".language
=> :korean
"이 히 어 로?".language
=> :russian
"펀치히어로".language
=> :italian
"한국드라마".language
=> :russian
"한 국 드 라 마".language
=> :korean

License missing from gemspec

RubyGems.org doesn't report a license for your gem. This is because it is not specified in the gemspec of your last release.

via e.g.

spec.license = 'MIT'
# or
spec.licenses = ['MIT', 'GPL-2']

Including a license in your gemspec is an easy way for rubygems.org and other tools to check how your gem is licensed. As you can imagine, scanning your repository for a LICENSE file or parsing the README, and then attempting to identify the license or licenses is much more difficult and more error prone. So, even for projects that already specify a license, including a license in your gemspec is a good practice. See, for example, how rubygems.org uses the gemspec to display the rails gem license.

There is even a License Finder gem to help companies/individuals ensure all gems they use meet their licensing needs. This tool depends on license information being available in the gemspec. This is an important enough issue that even Bundler now generates gems with a default 'MIT' license.

I hope you'll consider specifying a license in your gemspec. If not, please just close the issue with a nice message. In either case, I'll follow up. Thanks for your time!

Appendix:

If you need help choosing a license (sorry, I haven't checked your readme or looked for a license file), GitHub has created a license picker tool. Code without a license specified defaults to 'All rights reserved'-- denying others all rights to use of the code.
Here's a list of the license names I've found and their frequencies

p.s. In case you're wondering how I found you and why I made this issue, it's because I'm collecting stats on gems (I was originally looking for download data) and decided to collect license metadata,too, and make issues for gemspecs not specifying a license as a public service :). See the previous link or my blog post about this project for more information.

Fails to recognize simple English words/phrases

Hi Peter- This project looks exciting, however we can't get the most basic tests working when trying simple English words or phrases:

require 'whatlanguage'
=> true
"test".language
=> :dutch
"hello".language
=> :arabic
"how are you".language
=> :arabic

Examples fail in RoR

Your examples, as written fail in RoR.

"Je suis un homme".language returns undefined method `language'
wl.language("Je suis un homme") works fine and returns :french

.language String method does not work on Ruby 2.4.0

Instead Whatlanguage throws undefined method `language' for "foo":String:

NoMethodError: undefined method `language' for "Deux autres personnes ont été arrêtées durant la nuit":String
from (irb):26:in `block in irb_binding'
from (irb):26:in `each'
from (irb):26
from ~/.rvm/rubies/ruby-2.4.0/bin/irb:11:in `<top (required)>'
from ~/.rvm/gems/ruby-2.4.0/gems/bundler/cli/exec.rb:75:in `load'
from ~/.rvm/gems/ruby-2.4.0/gems/bundler/cli/exec.rb:75:in `kernel_load'
from ~/.rvm/gems/ruby-2.4.0/gems/bundler/cli/exec.rb:28:in `run'
from ~/.rvm/gems/ruby-2.4.0/gems/bundler/cli.rb:424:in `exec'
from ~/.rvm/gems/ruby-2.4.0/gems/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
from ~/.rvm/gems/ruby-2.4.0/gems/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
from ~/.rvm/gems/ruby-2.4.0/gems/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
from ~/.rvm/gems/ruby-2.4.0/gems/bundler/cli.rb:27:in `dispatch'
from ~/.rvm/gems/ruby-2.4.0/gems/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
from ~/.rvm/gems/ruby-2.4.0/gems/bundler/cli.rb:18:in `start'
from ~/.rvm/gems/ruby-2.4.0/gems/bundler/exe/bundle:30:in `block in <top (required)>'
from ~/.rvm/gems/ruby-2.4.0/gems/bundler/lib/bundler/friendly_errors.rb:122:in `with_friendly_errors'
from ~/.rvm/gems/ruby-2.4.0/gems/bundler/exe/bundle:22:in `<top (required)>'
from /usr/local/bin/bundle:22:in `load'
from /usr/local/bin/bundle:22:in `<main>'
from ~/.rvm/gems/ruby-2.4.0/bin/ruby_executable_hooks:15:in `eval'
from ~/.rvm/gems/ruby-2.4.0/bin/ruby_executable_hooks:15:in `<main>'

Could not install with ruby 2.3.0

Got this error while trying to install using command:
$ gem install whatlanguage

ERROR:  Error installing whatlanguage:
    invalid gem: package is corrupt, 
        exception while verifying: undefined method `size' for nil:NilClass (NoMethodError) 
        in /home/prakash/.rvm/gems/ruby-2.3.0/cache/whatlanguage-1.0.5.gem

Remove README

… such that README.txt is shown on main Github project page :)
(The README is currently preferred over README.txt)

Add gem to rubygems.org

Any reason why it is not there yet? It seems to me that is the de facto standard place for fetching gems.

Library doesn't seem to take character sets into account

I'm trying to distinguish between a couple of European languages and Turkish/Arabic/Aramaic. Whatlanguage does a fair job of the European languages, but beyond that falls apart at the seams.

main » wl.language("Bilgi Teknolojileri Kurumu (BTK) tarafından 29 Nisan 2017 tarihinde.")
=> :russian

It cannot be Russian, as that would be written with a different set of characters.

main » wl.language("البرنامج ليس ذكي جدا")
=> :arabic

ًWorks fine, even though I wasn't very nice to it. But as evidenced in #41, there are issues there, too. The reporter of #41 doesn't make it very explicit, but hits the same spot, especially with the numbers. (His first and second strings are easily recognizable as Farsi, not Arabic, by way of their second, i.e. the left, word not being part of the Arabic dictionary but very commonly used in Farsi).

main » wl.language("ܣܰܪܐ: ܓܷܕ ܣܳܚܝܢܰܐ، ܓܷܕ ܡܫܰܡܣܝܢܰܐ، ܓܷܕ ܫܳܬܝܢܰܐ ܩܰܚܘܰܐ ܘܦܰܠܓܶܗ ܕܝܰܘܡܐ ܠܰܦ ܐܝ ܣܰܥܰܐ ܬܪܰܥܣܰܪ ܘܦܰܠܓܶܗ ܓܷܕ ܡܰܥܪܝܢܰܐ. ܗܰܘܟ݂ܰܐ ܓܷܕ ܫܳܦܰܥ ܐܘ ܝܰܘܡܰܝܕ݂ܰܢ.")
=> :russian

Makes one wonder if Russian is a last-resort fallback. Again, though, it cannot possibly be Russian, because it's a completely different character set, namely that of Aramaic.

I'd also like to point out #27 again at this point. I do so with a sad face. In addition I would like to point out that these matters have been noticed elsewhere as well: "[...] this project still has a way [sic] to go [...]", posted to StackExchange on July 9, 2014.

single words don't match well at all!

Not sure if this is a issue, or just something to be aware of, but matching single words like "irish" return as :german, or "shelves" comes back as :portuguese.

In my case I am trying to filter out non english search words and tokenizing them to single words. I'll play with it as full queries. I'd love something that would keep words and toss symbols, or things with :, ? etc. Guess could write that as well!

procedure to include more languages

I want to add some more languages of my own choosing. It was mentioned in your blog that this task is not difficult. From what I understand, there is a build_language_from_wordlist file which builds a language from a wordlist file. Is there a way to generate the wordlist file for a particular language ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.