go-dateparser icon indicating copy to clipboard operation
go-dateparser copied to clipboard

Parsing a simple time always fails with "detector failed to find the suitable language"

Open milochristiansen opened this issue 3 years ago • 1 comments

I am writing a simple timeclock application for my own use, and ran into an interesting issue that I figured I should report.

Every time I try to parse a simple time (such as 1:20PM) it fails with "detector failed to find the suitable language". This lead to a lot of attempts to force the language to see if I could get another error or make it work. (Spoiler, SearchWithLanguage did the job.)

The first thing I tried was putting the language in the config:

_, times, err := dateparser.Search(&dateparser.Configuration{
	CurrentTime: time.Now().Local(),
	Languages: []string{"en"},
	DefaultLanguages: []string{"en"},
	Locales: []string{"en-US"},
}, whole)

At first, I only tried DefaultLanguages, but the other keys were quickly added when that didn't work.

When that failed, I tried adding a custom DetectLanguagesFunction that did nothing but return "en". This also failed.

After all that failed to get rid of the language detection error, I tried SearchWithLanguage, which finally worked.

All this messing about leads me to think there is something wrong with the language detection code, maybe it is improperly ignoring the config somehow? I would have expected at least one of the things I tried (before just nuking it from orbit with SearchWithLanguage) to have worked.

milochristiansen avatar Jan 25 '23 19:01 milochristiansen

After looking around, apparently the main reason for this issue is the Search function ignore DefaultLanguages config.

When the string is as short as 1:20PM, most language detector will fail because it doesn't have enough data to detect languages. Normally when that happen DefaultLanguages will be used as fallback. However I've made mistake which make DefaultLanguages ignored by Search.

It should be fixed by d877098.

RadhiFadlillah avatar Jul 04 '24 14:07 RadhiFadlillah