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

hunspell command line command not working for me #4

Open
RickKukiela opened this issue Aug 2, 2018 · 3 comments
Open

hunspell command line command not working for me #4

RickKukiela opened this issue Aug 2, 2018 · 3 comments

Comments

@RickKukiela
Copy link

return shell_exec(sprintf("LANG=%s; echo '%s' | hunspell -d %s", $this->encoding, $input, $this->language));

The referenced line does not appear to work on my windows machine or my centOS7 server. The output from the command is empty. I've had to hack my version of this function to do this, and please don't mind the 'hackyness' as I'm not super fluent with the whole command line interactivity stuff.

Oh by the way I also added support for a dictionary file. The reasoning is that the site I'm using this for has lots of brand name stuff that comes up as misspellings so I wrote a script to weight every word on the site and created a hunspell dictionary file out of the big / common ones. It works well, though I didnt put a lot of thought into keeping the code neat and clean as I didn't really expect anyone to see this lol.

As to a run down of what I'm doing, is I'm loading the dictionary file into memory if it exists, Then I save it as a temp file with the $input var appended (not sure why anymore I did this a while back - however since I'm not providing the $Input var to the command line that must be related to the reason or is otherwise how hunspell is getting the $input). After that I create an empty temp file to dump the output of the shell command. After the command runs I get the output from the temp file, delete both files and return the output.

Do you approve of this? Do you want me to do a PR and clean it up or if you want to implement it your self that would be cool. I'd also be interested on your take as to why there is no output from the provided command. I think it has something to do with the path vars and my environment or something.

Obviously the dictionary file support would have to be added to the actual options rather than hard coded.

` $dictionary_file = $_SERVER["DOCUMENT_ROOT"] . ".dictionary.txt";

	if(file_exists($dictionary_file)) {
		$custom_dictionary = file_get_contents($dictionary_file);
	} else {
		$custom_dictionary = "";
	}

	$tmp = tempnam(sys_get_temp_dir(), 'hunspell');
	file_put_contents($tmp, $custom_dictionary . "\n" . $input);

	$tmp2 = tempnam(sys_get_temp_dir(), 'hunspell');
	if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
		$cmd = 'C:\\hunspell\\bin\\hunspell.exe -a -d en_US ' . $tmp . " > " . $tmp2;
	} else {
		$cmd = 'hunspell -a -d en_US ' . $tmp . " > " . $tmp2;
	}

	shell_exec($cmd);

	unlink($tmp);

	$out = file_get_contents($tmp2);
	unlink($tmp2);

	$out = explode("\n", trim($out));
	array_shift($out);
	$out = implode(" ", $out);

	return $out;`
@RickKukiela
Copy link
Author

Also I thought I would leave my hunspell versions for good measure.

This is the same on both machines:

$ hunspell -v
@(#) International Ispell Version 3.2.06 (but really Hunspell 1.3.2)

@RickKukiela
Copy link
Author

I ended up coming up with a much cleaner solution and it turns out that I dont even need the dictionary file with the latest codebase. I forked this, and fixed it up so it works on windows and bash but I'm not sure what shell you were doing this on. The way you were setting the environment was not working on my windows machine or my server. I'll submit a PR, but I recommend adding a condition for your shell back in for both the command method calls.

Also a side note, the findCommand and stemCommand functions could be merged to take a boolean $stems var that when set would just add a -s to the command. I could have done this but I forgot and I needed to get this up quick for a push to my code base.

@johnzuk
Copy link
Owner

johnzuk commented Aug 3, 2018

Hi, thank you for you PR!

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

No branches or pull requests

2 participants