Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3779785
D2661.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
D2661.diff
View Options
diff --git a/omnitools/src/Strings/SorensenDiceCoefficient.php b/omnitools/src/Strings/SorensenDiceCoefficient.php
--- a/omnitools/src/Strings/SorensenDiceCoefficient.php
+++ b/omnitools/src/Strings/SorensenDiceCoefficient.php
@@ -5,27 +5,43 @@
use Keruald\OmniTools\Strings\Multibyte\OmniString;
+/**
+ * Computes the Sørensen–Dice coefficient, a statistic used to evaluate
+ * the similarity between two strings.
+ */
class SorensenDiceCoefficient {
/**
* @var string[]
*/
- private $x;
+ private array $x;
/**
* @var string[]
*/
- private $y;
+ private array $y;
///
/// Constructors
///
+ /**
+ * @param string $left The first string to compare
+ * @param string $right The second string to compare
+ */
public function __construct (string $left, string $right) {
$this->x = (new OmniString($left))->getBigrams();
$this->y = (new OmniString($right))->getBigrams();
}
+ /**
+ * Allows to directly compute the coefficient between two strings.
+ *
+ * @param string $left The first string to compare
+ * @param string $right The second string to compare
+ *
+ * @return float The Sørensen–Dice coefficient for the two specified strings.
+ */
public static function computeFor(string $left, string $right) : float {
$instance = new self($left, $right);
@@ -36,6 +52,9 @@
/// Sørensen formula
///
+ /**
+ * @return float The Sørensen–Dice coefficient.
+ */
public function compute() : float {
return 2 * $this->countIntersect()
/
diff --git a/omnitools/tests/Strings/SorensenDiceCoefficientTest.php b/omnitools/tests/Strings/SorensenDiceCoefficientTest.php
--- a/omnitools/tests/Strings/SorensenDiceCoefficientTest.php
+++ b/omnitools/tests/Strings/SorensenDiceCoefficientTest.php
@@ -14,4 +14,11 @@
$this->assertEquals(0.25, $actual->compute());
}
+ public function testComputeFor () : void {
+ $score = SorensenDiceCoefficient::computeFor('night', 'nacht');
+
+ $this->assertGreaterThan(0, $score);
+ $this->assertLessThan(1, $score);
+ }
+
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Tue, Nov 26, 09:58 (20 h, 26 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2264453
Default Alt Text
D2661.diff (2 KB)
Attached To
Mode
D2661: Improve doc and test coverage for Sørensen–Dice coefficient
Attached
Detach File
Event Timeline
Log In to Comment