abadojack / whatlanggo

Natural language detection library for Go
MIT License
637 stars 64 forks source link

Struggling to detect English #3

Open peterbe opened 7 years ago

peterbe commented 7 years ago

I took your awesome lib and wrapped it in a little command line app. I also added a conversion table from ISO 639-3 to ISO 639-1.

▶ ./langdetect "Le candidat socialiste à l’élection présidentielle"
Language: fra Script: Latin
fr

Correct!

▶ ./langdetect "Mitt namn på svenska är Peter"
Language: swe Script: Latin
sv

Correct!

But....

▶ ./langdetect "testing in english"
Language: uig Script: Latin
ug

Not right.

▶ ./langdetect "wondering if it still works in English"
Language: nld Script: Latin
nl

Not right either.

Also, would it be possible to output a list of probabilities? That way my app, where I hope to use this, could throw warnings if the probabilities "aren't certain enough".

peterbe commented 7 years ago

By the way, my hack is here: https://github.com/peterbe/langdetect Careful, I haven't done go for a long time.

abadojack commented 7 years ago

I noticed this while I was testing and I haven't found a solution yet. I added it to the TODO list in this commit.

peterbe commented 7 years ago

For what it's worth, I tested something similar with a different project (https://pypi.python.org/pypi/guess_language-spirit) and it's based on a bunch of trigrams too. I can't remember the example but it suffered the same problem.

abadojack commented 7 years ago

I just checked it out and tested it with the same phrases you used.

>>> guess_language("Le candidat socialiste à l’élection présidentielle.")
'fr'

Correct!

>>> guess_language("Mitt namn på svenska är Peter")
'sv'

Correct!

>>> guess_language("testing in english")
'UNKNOWN'

Not correct.

>>> guess_language("wondering still if it works in english")
'af'

Still not correct.

Considering the trigrams used in that project are not similar to the ones I used ... wtf is wrong with English ?

peterbe commented 7 years ago

I tried franc too.

var franc = require('franc');

console.log(franc("Le candidat socialiste à l’élection présidentielle."))
console.log(franc("Mitt namn på svenska är Peter"))
console.log(franc("testing in english"))
console.log(franc("wondering still if it works in english"))

output;

▶ node test.js
fra
swe
uig
nld
azer commented 6 years ago

Short inputs like "happy" "hello" also returns incorrect results.

ernsheong commented 6 years ago

Looks like the longer the string, the better the result?

Hilarious:

func TestLangDetection(t *testing.T) {
    lang := whatlanggo.DetectLang("english english english english english english english")
    if lang != whatlanggo.Eng {
        t.Fatalf("Expected lang to be %v but was %v", whatlanggo.LangToString(whatlanggo.Eng), whatlanggo.LangToString(lang))
    }
}

--- FAIL: TestLangDetection (0.00s)
        search_test.go:12: Expected lang to be eng but was uzb
FAIL

Providing a Whitelist via DetectLangWithOptions helps.

Algorithm:

  1. http://greyblake.com/blog/2017/07/30/introduction-to-rust-whatlang-library-and-natural-language-identification-algorithms/
  2. http://odur.let.rug.nl/~vannoord/TextCat/textcat.pdf

Quote from the article:

Disadvantages:

May provide falsy results for short texts (smaller than 200-300 letters). Whatlang tries to compensate this with is_reliable attribute.

So there are 2 ways to fix/mitigate this:

  1. Provide confidence value
  2. As the article says, for lower amount of text we could try dictionary checks instead.
miku commented 6 years ago

First, thanks for making this library available, it helped me to get rid of a C dependency from cld2.

Just wanted to add a few additional data points concerning non-detection of English text:

greyblake commented 5 years ago

Hi. I am a creator of the original library in Rust. I just want to say, that you should not have high expectation, if input text is relatively small. The library is based on statistical profiles of languages (trigrams). The bigger input text, the better it represents its statistical profile.

There is nothing you can do with this issue.

Some possible solutions however:

@miku

Just wanted to add a few additional data points concerning non-detection of English text:

Rust version at the moment provides is_reliable boolean in result. When it's true it is guaranteed, that language is recognized correctly. Otherwise you should not trust the result. For all of your text samples it returns result is_reliable=false.

mmorells commented 5 years ago

15 Added confidence with the logic from the original library by @greyblake

Doesn't fix the problems with detection, but now Info{} has the confidence rating.