Skip to content

feature: add language configuration with sensible default - #45

Closed
benjam-es wants to merge 3 commits into
peckphp:mainfrom
benjam-es:feature/language
Closed

benjam-es wants to merge 3 commits into
peckphp:mainfrom
benjam-es:feature/language

Conversation

@benjam-es

Copy link
Copy Markdown
Contributor

What:

  • [ - ] New Feature

Description:

This enables us to specify the language that is used by Aspell. Where users are using another language, they can specify. Also, Aspell relies on the system locale if this is not set, so the current main branch fails for users without en_US locale

Related:

See issue #27

Signed-off-by: Ben James <in@benjam.es>
@benjam-es

Copy link
Copy Markdown
Contributor Author

Also see #15

@julio-cavallari

Copy link
Copy Markdown
Contributor

Have you tested it with languages ​​other than English?

I did some testing of the approach you used, but since Aspell doesn't support multiple languages ​​at the same time, when the user changes the language, words in English, even if correct, were shown as incorrect, which in most cases would be a problem, since the vast majority of projects have a mix of English and another language.

@c0nst4ntin c0nst4ntin left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great contribution, however maybe we should change the language to a string in the config to account for the limits by Aspell?

public function check(string $text): array
{
$misspellings = $this->filterWhitelistedWords(iterator_to_array($this->aspell->check($text)));
$misspellings = $this->filterWhitelistedWords(iterator_to_array($this->aspell->check($text, $this->config->languages)));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I stumbled across the fact that Aspell only supports one language:
https://github.com/tigitz/php-spellchecker/blob/dbbf94efc9761505a9fa9583537b5f9d1b53260d/src/Spellchecker/Aspell.php#L28

So if we let the user configure an array, it can cause an unexpected exception here!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - it says one language, I have seen that. The config is currently an array as that is what is expected by Aspell.

Agreed - we could make our config take only a string and wrap that in an array, but I went with this for now.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this was what I had in mind as well. Maybe that's something you could take a look at?

@c0nst4ntin

Copy link
Copy Markdown
Collaborator

@julio-cavallari Yes, I ran into the same problem as well when experimenting with this feature. Maybe we need to check words in multiple languages and see if there was a spelling mistake in one of the languages.

…ithin an array to Aspell

Signed-off-by: Ben James <in@benjam.es>

@c0nst4ntin c0nst4ntin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now! However as @julio-cavallari mentioned, changing the language to German in an English project causes some trouble.

Let's get this basic implementation merged and then we can see how to improve it

@GrandadEvans

GrandadEvans commented Jan 4, 2025 •

Copy link
Copy Markdown
Contributor

There is one part of this code that need to have in order for my tests to work.,

$misspellings = $this->filterWhitelistedWords(iterator_to_array($this->aspell->check($text, ['en_US'])));

I've not done anything with it the language issue is obviously a large and complicated one, but if this can be included it would be a boost to me (I wouldn't have to keep ignoring it in Git), and I am not sure how other non-us locales go..

@c0nst4ntin

Copy link
Copy Markdown
Collaborator

@benjam-es Is it maybe possible for you to resolve the conflicts? If they are minimal @nunomaduro can do it on stream. But preparing this would probably make things easier 👍🏼

@c0nst4ntin

Copy link
Copy Markdown
Collaborator

@benjam-es Could you maybe also prepare a new test which sets the language and then tests a file in a different language?

Signed-off-by: Ben James <in@benjam.es>
@benjam-es

Copy link
Copy Markdown
Contributor Author

Merged main and fixed conflicts. Will look at a test

@nunomaduro

Copy link
Copy Markdown
Contributor

Sorry, but at the moment I want to keep this simple. So, tabling this idea for now!

@nunomaduro nunomaduro closed this Jan 10, 2025
@benjam-es

Copy link
Copy Markdown
Contributor Author

@c0nst4ntin @nunomaduro I have added a test case - please try that text case without the PR to show the error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants