Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to implement? #32

Closed
winstonrenatan opened this issue Apr 13, 2020 · 4 comments
Closed

How to implement? #32

winstonrenatan opened this issue Apr 13, 2020 · 4 comments
Labels
Android issues occurring on Android systems bug Something isn't working question Further information is requested
Milestone

Comments

@winstonrenatan
Copy link

Hello I am quite new in programming with android and I am currently working on Text To Speech App. I would like to implement Lingua so the TTS app can switch to the specified language according to the Lingua results. But I found some difficulties in implementing it. This is the process that I have done:

  1. Adding implementation("com.github.pemistahl:lingua:0.6.1") to my Gradle
  2. Putting import to my .kt file
import com.github.pemistahl.lingua.api.*
import com.github.pemistahl.lingua.api.Language.*
  1. Putting the line in my code
val detector: LanguageDetector = LanguageDetectorBuilder.fromLanguages(ENGLISH, FRENCH, GERMAN, SPANISH).build()
val detectedLanguage: Language = detector.detectLanguageOf(text = "languages are awesome")
  1. I then would like to output the detectedLanguage in a toast.. but then everytime I clicked on the button that directs me to point number 3 the app closes down.

I would like to ask if can we detectLanguageOf from a variable. for example,

var checkLang =  "languages are awesome"
val detectedLanguage: Language = detector.detectLanguageOf(checkLang)

and is detectedLanguage a string?
Thank you for your help! this library is really awesome!

@pemistahl
Copy link
Owner

Hi @winstonrenatan, thank you for trying my library and for getting in touch with me.

Please correct me in case I'm wrong but it seems that you are pretty new to programming in general. I get the impression that you should work through the basic Kotlin tutorials first. The variable detectedLanguage is not a string but an instance of Language which is an enum. Of course, you can put the text in its own variable first before feeding it into detectLanguageOf().

Apart from that, I don't understand your specific problem that you have with Android. What is a toast in this context? Can your provide a stacktrace of an error you get?

@winstonrenatan
Copy link
Author

Hi @pemistahl, thank you for your warm welcome and help.

It is true that i am quite new in programming both in general and even kotlin basics. I actually just want simply to get the language of the text as a string, then output it in a toast where the user can see it. But I still don't understand how things actually works :(
Can you please guide me through, what should I do to obtain and print out the value of detectedLanguage, like what do I need to get the value of it in order to be printed out.
I have reached this point in my project, where I put the library in as a button.

languageButton.setOnClickListener{
       val detector: LanguageDetector = LanguageDetectorBuilder.fromLanguages(ENGLISH, FRENCH, GERMAN, SPANISH).build()
       val detectedLanguage: Language = detector.detectLanguageOf(text = "languages are awesome")
       Toast.makeText(this, detectedLanguage.toString(), Toast.LENGTH_SHORT).show()
}

But when I do click on the button that I created, the application terminate and show me this error

2020-04-17 22:04:44.702 5173-5173/com.github.winstonrenatan.skrispiv11 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.github.winstonrenatan.skrispiv11, PID: 5173
    java.lang.ExceptionInInitializerError
        at com.github.pemistahl.lingua.api.LanguageDetectorBuilder.build(LanguageDetectorBuilder.kt:29)
        at com.github.winstonrenatan.skrispiv11.MainActivity$onCreate$2.onClick(MainActivity.kt:79)
        at android.view.View.performClick(View.java:6291)
        at android.view.View$PerformClick.run(View.java:24931)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:101)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7529)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
     Caused by: java.util.regex.PatternSyntaxException: U_ILLEGAL_ARGUMENT_ERROR
    ^[\p{IsHiragana}\p{IsKatakana}\p{IsHan}]+$
        at java.util.regex.Pattern.compileImpl(Native Method)
        at java.util.regex.Pattern.compile(Pattern.java:1340)
        at java.util.regex.Pattern.<init>(Pattern.java:1324)
        at java.util.regex.Pattern.compile(Pattern.java:946)
        at kotlin.text.Regex.<init>(Regex.kt:89)
        at com.github.pemistahl.lingua.api.LanguageDetector.<clinit>(LanguageDetector.kt:381)
        	... 11 more

Thank you for your help I really do appreciate it so much, I'm sorry if I bothers and confused you through this questions and sorry for the late reply :(

@pemistahl
Copy link
Owner

Hello @winstonrenatan, I apologize myself now for this late response. My two-months-old little daughter is keeping me very busy at the moment.

Your stacktrace is very informative. There is a bug in the file LanguageDetector.kt in line 381. Character classes prefixed with Is- are not supported on Android.

Can you please clone this project's repository, replace the line with the following code, then build the library yourself and see whether your problem is gone? I'm not able to test on Android myself. If it works, I will provide a bug fix release in the near future. Thank you.

private val JAPANESE_CHARACTER_SET = try {
    Regex("^[\\p{Hiragana}\\p{Katakana}\\p{Han}]+$")
} catch (e: PatternSyntaxException) {
    Regex("^[\\p{IsHiragana}\\p{IsKatakana}\\p{IsHan}]+$")
}

@pemistahl pemistahl added Android issues occurring on Android systems bug Something isn't working question Further information is requested labels May 25, 2020
@pemistahl pemistahl added this to the Lingua 1.0.0 milestone May 25, 2020
@pemistahl
Copy link
Owner

I'm closing this issue now because commit 0078c01 most likely fixes this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Android issues occurring on Android systems bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants