Skip to content

hunspell command line command not working for me #4

@RickKukiela

Description

@RickKukiela

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;`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions