test of similar_text

parents
<?php
$correctTexts = [
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.",
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.",
"Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
];
$studentsText = "em ipsum dolor sit anet, consectetr adipiscing elit, sed do eiusmod tempor incddunt ut labore et dole magna aliqua. " .
"Ut enim ad minim veniam, quis nostrud exercitation ulamco laboris nisi ut aliquip ex ea commodo conseat. " .
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " .
"proident, unt in culpa qui officia deserunt mollit anim id est laborum. ";
class textComparision
{
static public function findMatchPartOfText($needle, $haystack) : string
{
// split text to pieces with length of $needle
$partLength = strlen($needle);
$parts = str_split($haystack, $partLength);
// evaluate each part
// $result consists of consistence percentage for each part
$results = [];
foreach ($parts as $part)
{
similar_text($needle, $part, $percents);
$results[] = ceil($percents * 100);
}
// get piece number with max percent of consistence
$partNumber = array_search(max($results), $results);
// defining bounds for search
// Extend search area from -50% to +50% of length for the part with max consistence
$leftBound = $partNumber * $partLength - ceil($partLength / 2);
$rightBound = $leftBound + $partLength;
// bounds normalization.
// If left or|and right bound is out of the range, then need to be normalized
if ($leftBound < 0) {
$leftBound = 0;
}
if ($rightBound >= strlen($haystack)) {
$rightBound = strlen($haystack) - 1;
}
// searching text in this range with max consistence
$results = [];
for($i = $leftBound; $i <= $rightBound; $i++)
{
similar_text(substr($haystack, $i, $partLength), $needle, $percent);
$results[$i] = $percent;
}
return substr($haystack, array_search(max($results), $results), $partLength);
}
}
echo "<h2>Students text</h2>" .
"<p>{$studentsText}</p>";
foreach ($correctTexts as $key => $correctText)
{
$userText = textComparision::findMatchPartOfText($correctText, $studentsText);
$color = $correctText === $userText ?
"green" :
"red";
echo "<p style='color: blue'>{$correctText}</p>" .
"<p style='color: {$color}'>{$userText}</p>";
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment