Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
test
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Николайчук Дима
test
Commits
0757ad36
Commit
0757ad36
authored
Sep 23, 2019
by
Николайчук Дима
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test of similar_text
parents
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
80 additions
and
0 deletions
+80
-0
test.php
+80
-0
No files found.
test.php
0 → 100644
View file @
0757ad36
<?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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment