I need a function written and don’t have time to do it so I am offering $100 to whoever can write the shortest and most simple example code that accomplishes my goals.
I use regular expressions (regex) a lot in my NLP projects. There is a bastardization of regex called spintax.
It is basically just {content|other content} chained to other {alternatives|strings}.
It usually comes nested as well. so it could be {phrases|some {other|similar} content} chained to similar {alternative {synonyms|strings}|parts of speach}
In real life, it looks like this:
"{{I need|I want|I would like} |}{help|support|assistance|help support|{customer support|customer care|customer service|support service|support services|consumer support|client care|client service|service}}{ {please|if possible|if you please|bear in mind|pls}|}"
This outputs variations of the sentence such as:
- I want help if possible
- I need support please
- I want help if you please
- I need assistance
I need a function where I can just paste this spintax code into a variable and have it spit out these variations.
I imagine it could be something like:
spinTax =
"{{I need|I want|I would like} |}{help|support|assistance|help support|{customer support|customer care|customer service|support service|support services|consumer support|client care|client service|service}}{ {please|if possible|if you please|bear in mind|pls}|}";
variationCount = 4;
spinFunc = (*Some cool function that spits out variations*)
spinClean = Union[spinFunc]; (*Clean up the list, remove duplicates, etc. Run function again until desired count of unique variations is reached*)
Out[10] = {"I want help if possible", "I need support please", "I want help if you please", "I need assistance"}
Here is my code in PHP that outputs in json. Maybe this can help.
# Show results.
if($output['success']=='true'){
class Spintax
{
public function process($text)
{
return preg_replace_callback(
'/\{(((?>[^\{\}]+)|(?R))*)\}/x',
array($this, 'replace'),
$text
);
}
public function replace($text)
{
$text = $this->process($text[1]);
$parts = explode('|', $text);
return $parts[array_rand($parts)];
}
}
$spintax = new Spintax();
$string = $output['output'];
$spunVariations = array();
for ($x = 0; $x <= $variationCount; $x++) {
$spunVariations[] = $spintax->process($string);
}
$spunVariations= array_keys(array_count_values($spunVariations));
$x = count($spunVariations, COUNT_RECURSIVE);
$spunVariations= array_keys(array_count_values($spunVariations));
$arr = $output['output'];
//echo json_encode($arr);
echo json_encode($spunVariations);
}
else{
echo json_encode("Error:". $output[error]);
}
}
else{
# There were errors.
echo json_encode("Error:". $output[error]);
}
This PHP example doesn’t have the function of ensuring that the resulting list has only unique values in it. It also doesn’t have any checks to make sure the resulting sentences are greatly different from each other.
