Closed hoanguel closed 5 years ago
Hey,
Problem
You're trying to use LanguageDetector as a service provider.
It implies that LanguageDetector\LanguageDetector::__construct()
receives an object whereas it's not the goal. Construct parameter MUST be a valid path (a string) or a null value.
Solution
You should not declare LanguageDetector as a service.
First solution, you can directly use the library wherever you need it.
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
// declare
use LanguageDetector\LanguageDetector;
class MyController extends Controller
{
/**
* Display the detected language
*
* @param string $text
*/
public function detect($text)
{
// use
echo LanguageDetector::detect($text);
}
}
Second solution, you can implement a Laravel Service provider with your logic and use the library inside.
It depends on your needs.
Hope this help!
Wow, thank you for your super fast response :) Got it and I have corrected it :D Working like a charm! However, I now have another "Klingon" issue. The sample text to detect is "Who am I?" and the detected language printed out is i-Klingon. I am a big fan of Star Trek and often practice Klingon with Sheldon Cooper but this, hmmm, doesn't seem right?
You're welcome! Nice culture ;)
Well, what we could call "the klingon issue" is due to ngram algorithm.
Its reliability depends on text length.
use LanguageDetector\LanguageDetector;
$texts = [
"Who am I?",
"What's that?",
"What the fuck!",
"What is the sense of life?",
"Who am I? What is the sense of life?",
"The needs of the many outweigh the needs of the few, or the one.",
];
foreach ($texts as $i => $text) {
echo sprintf(
"(%d chars) - %s : %s\n",
strlen($text),
LanguageDetector::detect($text),
$text
);
}
would outputs:
(9 chars) - i-klingon : Who am I?
(12 chars) - so : What's that?
(14 chars) - en : What the fuck!
(26 chars) - en : What is the sense of life?
(36 chars) - en : Who am I? What is the sense of life?
(64 chars) - en : The needs of the many outweigh the needs of the few, or the one.
As you can see, you should not rely on detection on a string of less than 15 characters.
Got it, thanks a bunch :+1:
Hi,
Using Laravel 5.7, I just install this package via composer then add this to providers config:
LanguageDetector\LanguageDetector::class,
When running with the provided example, I got this error message:
In LanguageDetector.php line 56: rtrim() expects parameter 1 to be string, object given
Is there any hints/advices you could give for me to fix it, please? Thank you!