landrok / language-detector

A fast and reliable PHP library for detecting languages
MIT License
118 stars 18 forks source link

Simply doesn't work #8

Closed patricksavalle closed 2 years ago

patricksavalle commented 2 years ago

I installed the package with composer, using this code it will crash:

    static public function checkLanguage(stdClass $link)
    {
        $text = (string)(new Text($link->markdown))->parseDown()->stripTags();
        $link->language = (new LanguageDetector)->evaluate($text)->getLanguage()->getCode();
    }

Error:

[Tue Nov 16 18:17:27 2021] array_key_exists(): Argument #2 ($array) must be of type array, null given @ C:\Users\patri\Projects\Dev\api.zaplog\vendor\webmozart\assert\src\Assert.php(1680)

Looks like it can't find the language files.

landrok commented 2 years ago

Hello @patricksavalle

You're true. The key point here is that $text may be null.

Maybe you could do something like:

$link->language = ! is_null($text) ? (string) LanguageDetector::detect($text) : null;

Hope this helps landrok

patricksavalle commented 2 years ago

@landrok Thanks for the quick answer, much appreciated, but the text is not null.

static public function checkLanguage(stdClass $link)
{
    $text = (string)(new Text($link->markdown))->parseDown()->stripTags();
    error_log("text---> ". $text);
    $link->language = ! is_null($text) ? (string) LanguageDetector::detect($text) : null;
}

image

It looks like it can't find the language files in the ctor.

landrok commented 2 years ago

Could you please :

patricksavalle commented 2 years ago

The text:

Hij voegde er lachend aan toe: “Nee, ik ben even flauw aan het doen.”

Dat was hij niet.

Hij meende het uit de grond van zijn ha… waar bij ons een hart zit.

En hij heeft er op een schandalige manier vanaf die dag misbruik van gemaakt.

De psychopaat.

Nú is het moment dat jullie eens een keer kunnen terugslaan.

Hij dénkt jullie bij de ballen te hebben door ‘2G’.

Hij dénkt jullie te kunnen dwingen je te laten vaccineren door jullie de mogelijkheid te ontnemen om een coronapas te krijgen na een negatieve test.

Terwijl jullie de énigen zijn die aantoonbaar géén corona hebben op het moment dat jullie ergens binnen komen met jullie ‘G’ van ‘Getest’, staan uitgerekend jullie binnenkort voor een gesloten deur.

Dénkt hij.

Maar jullie zijn niet voor één gat te vangen.

Want jullie kunnen, nu de G van ‘Getest’ gaat vervallen, heus nog een coronapas krijgen zónder de G van ‘Gevaccineerd’.

Trace:

[Tue Nov 16 23:31:13 2021] #0 C:\Users\patri\Projects\Dev\api.zaplog\vendor\landrok\language-detector\src\LanguageDetector\Language.php(65): Webmozart\Assert\Assert::keyExists(NULL, 'freq')
#1 C:\Users\patri\Projects\Dev\api.zaplog\vendor\landrok\language-detector\src\LanguageDetector\Language.php(81): LanguageDetector\Language->load()
#2 C:\Users\patri\Projects\Dev\api.zaplog\vendor\landrok\language-detector\src\LanguageDetector\LanguageDetector.php(204): LanguageDetector\Language->getFreq()
#3 [internal function]: LanguageDetector\LanguageDetector->LanguageDetector\{closure}(Object(LanguageDetector\Language), 'pt')
#9 C:\Users\patri\Projects\Dev\api.zaplog\vendor\patricksavalle\slim-request-params\RequestResponseArgsObject.php(38): Zaplog\Api->Zaplog\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(stdClass))
#10 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\Route.php(281): SlimRequestParams\RequestResponseArgsObject->__invoke(Object(Closure), Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#11 C:\Users\patri\Projects\Dev\api.zaplog\vendor\patricksavalle\slim-request-params\QueryParameters.php(22): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#12 [internal function]: SlimRequestParams\QueryParameters->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\Route))
#13 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(SlimRequestParams\QueryParameters), Array)
#14 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\Route))
#15 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\Route))
#16 C:\Users\patri\Projects\Dev\api.zaplog\vendor\patricksavalle\slim-request-params\BodyParameters.php(23): Slim\Route->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#17 [internal function]: SlimRequestParams\BodyParameters->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#18 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(SlimRequestParams\BodyParameters), Array)
#19 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#20 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#21 C:\Users\patri\Projects\Dev\api.zaplog\vendor\patricksavalle\slim-rest-api\Middleware\Authentication.php(96): Slim\Route->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#22 [internal function]: SlimRestApi\Middleware\Authentication->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#23 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(Zaplog\Middleware\Authentication), Array)
#24 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#25 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#26 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\Route->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#27 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\Route.php(268): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#28 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\App.php(503): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))
#29 C:\Users\patri\Projects\Dev\api.zaplog\vendor\patricksavalle\slim-rest-api\SlimRestApi.php(44): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#30 [internal function]: SlimRestApi\SlimRestApi->SlimRestApi\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Zaplog\Api))
#31 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(Closure), Array)
#32 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Zaplog\Api))
#33 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Zaplog\Api))
#34 C:\Users\patri\Projects\Dev\api.zaplog\vendor\pavlakis\slim-cli\src\CliRequest.php(87): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#35 [internal function]: pavlakis\cli\CliRequest->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#36 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(pavlakis\cli\CliRequest), Array)
#37 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#38 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#39 C:\Users\patri\Projects\Dev\api.zaplog\vendor\palanik\corsslim\CorsSlim.php(122): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#40 [internal function]: CorsSlim\CorsSlim->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#41 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\DeferredCallable.php(57): call_user_func_array(Object(CorsSlim\CorsSlim), Array)
#42 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#43 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(70): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#44 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(117): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#45 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\App.php(392): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#46 C:\Users\patri\Projects\Dev\api.zaplog\vendor\slim\slim\Slim\App.php(297): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#47 C:\Users\patri\Projects\Dev\api.zaplog\Api.php(710): Slim\App->run()
#48 {main}
patricksavalle commented 2 years ago

@landrok I did a little debugging. The languages files are read. It goes into Evaluate with a valid text and than crashes somewhere here:

private function calculate(array $chunks): callable
patricksavalle commented 2 years ago

@landrok I fixed it.

For some reasons some language files where empty. Removed the folder, composer update.

Very sorry for your inconvenience!!!