Skip to content

Commit 8eefda1

Browse files
committed
refresh composer and add phpcs and phpunit
1 parent 9b5d83f commit 8eefda1

14 files changed

Lines changed: 377 additions & 341 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
/nbproject/private/
1+
/nbproject/private/
2+
/vendor/

README.md

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
# php-language-processing
22

3-
[![Latest Stable Version](https://poser.pugx.org/webd/language/v/stable)](https://packagist.org/packages/webd/language) [![Total Downloads](https://poser.pugx.org/webd/language/downloads)](https://packagist.org/packages/webd/language)
3+
[![Latest Stable Version](https://poser.pugx.org/webd/language/v/stable)](https://packagist.org/packages/webd/language)
4+
[![Total Downloads](https://poser.pugx.org/webd/language/downloads)](https://packagist.org/packages/webd/language)
45

56
A PHP library for language processing. Includes string distance function
67
(Levenshtein, Jaro-Winkler, LCS-distance...), stemming, hashing etc.
78

8-
Installation using Composer
9-
---------------------------
9+
## Installation
1010

11-
in composer.json :
12-
```
13-
"require": {
14-
"webd/language": "dev-master"
15-
}
11+
```bash
12+
composer require webd/language
1613
```
1714

18-
Then
19-
```
20-
composer install
21-
```
2215

23-
Usage
24-
-----
16+
17+
## Usage
2518

2619
```php
2720
use webd\language\StringDistance;

composer.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,15 @@
33
"description": "A library for language processing. Includes string distance function (Levenshtein, Jaro-Winkler,...), stemming, etc.",
44
"autoload": {
55
"psr-0": {"": "src/"}
6+
},
7+
"require-dev": {
8+
"squizlabs/php_codesniffer": "^4.0",
9+
"slevomat/coding-standard": "^8.28",
10+
"phpunit/phpunit": "^13.0"
11+
},
12+
"config": {
13+
"allow-plugins": {
14+
"dealerdirect/phpcodesniffer-composer-installer": true
15+
}
616
}
7-
}
17+
}

nbproject/project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ auxiliary.org-netbeans-modules-php-phpunit.phpUnit_2e_path=
1010
auxiliary.org-netbeans-modules-php-phpunit.test_2e_groups_2e_ask=false
1111
auxiliary.org-netbeans-modules-php-phpunit.test_2e_run_2e_all=false
1212
include.path=${php.global.include.path}
13-
php.version=PHP_53
13+
php.version=PHP_74
1414
source.encoding=UTF-8
1515
src.dir=.
1616
tags.asp=false

phpcs.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="PHP_CodeSniffer">
3+
<description>The coding standard for our project.</description>
4+
<rule ref="PSR2"/>
5+
6+
<file>src</file>
7+
<file>tests</file>
8+
9+
<!-- Show progression -->
10+
<arg value="p"/>
11+
12+
<!-- Add rules from slevomat
13+
https://cylab.be/blog/169/detect-unnecessary-use-statements-with-php-codesniffer -->
14+
<config name="installed_paths" value="../../slevomat/coding-standard"/>
15+
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses"/>
16+
</ruleset>

src/webd/language/LCS.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
namespace webd\language;
33

44
/**
5-
* The longest common subsequence (LCS) problem consists in finding the
6-
* longest subsequence common to two (or more) sequences. It differs from
7-
* problems of finding common substrings: unlike substrings, subsequences are
8-
* not required to occupy consecutive positions within the original sequences.
9-
*
5+
* The longest common subsequence (LCS) problem consists in finding the
6+
* longest subsequence common to two (or more) sequences. It differs from
7+
* problems of finding common substrings: unlike substrings, subsequences are
8+
* not required to occupy consecutive positions within the original sequences.
9+
*
1010
* Used by the diff utility, by Git for reconciling multiple changes, etc.
1111
*/
1212
class LCS
@@ -15,7 +15,8 @@ class LCS
1515
private $X = "";
1616
private $Y = "";
1717

18-
public function __construct($str1, $str2) {
18+
public function __construct($str1, $str2)
19+
{
1920

2021
$this->X = $str1;
2122
$this->Y = $str2;
@@ -26,7 +27,7 @@ public function __construct($str1, $str2) {
2627
$this->C = array();
2728

2829
for ($i = 0; $i <= $m; $i++) {
29-
$this->C[$i][0] = 0;
30+
$this->C[$i][0] = 0;
3031
}
3132

3233
for ($j = 0; $j <= $n; $j++) {
@@ -37,7 +38,6 @@ public function __construct($str1, $str2) {
3738
for ($j = 1; $j <= $n; $j++) {
3839
if ($str1[$i-1] == $str2[$j-1]) {
3940
$this->C[$i][$j] = $this->C[$i-1][$j-1] + 1;
40-
4141
} else {
4242
$this->C[$i][$j] = max($this->C[$i][$j-1], $this->C[$i-1][$j]);
4343
}
@@ -52,31 +52,36 @@ public function __construct($str1, $str2) {
5252
// C[i,j] := max(C[i,j-1], C[i-1,j])
5353
}
5454

55-
public function length() {
55+
public function length()
56+
{
5657
return $this->C[strlen($this->X)][strlen($this->Y)];
5758
}
5859

59-
public function __toString() {
60+
public function __toString()
61+
{
6062
return $this->value();
6163
}
6264

63-
public function value() {
65+
public function value()
66+
{
6467
return $this->backtrack(strlen($this->X), strlen($this->Y));
6568
}
6669

6770
/**
68-
* Edit distance when only insertion and deletion is allowed (no
71+
* Edit distance when only insertion and deletion is allowed (no
6972
* substitution)
7073
* = strlen(str1) + strlen(str2) - 2 * length(LCS(str1, str2))
7174
* @param type $string1
7275
* @param type $string2
7376
*/
74-
public function distance() {
77+
public function distance()
78+
{
7579
return strlen($this->X) + strlen($this->Y) - 2 * $this->length();
7680
}
7781

7882

79-
private function backtrack($i, $j) {
83+
private function backtrack($i, $j)
84+
{
8085
if ($i == 0 || $j == 0) {
8186
return "";
8287
}
@@ -104,7 +109,7 @@ private function backtrack($i, $j) {
104109
}
105110

106111
// /**
107-
// * Edit distance when only insertion and deletion is allowed (no
112+
// * Edit distance when only insertion and deletion is allowed (no
108113
// * substitution)
109114
// * = strlen(str1) + strlen(str2) - 2 * length(LCS(str1, str2))
110115
// * @param type $string1
@@ -113,19 +118,19 @@ private function backtrack($i, $j) {
113118
// public static function distance($str1, $str2) {
114119
// return strlen($str1) + strlen($str2) - 2 * self::length($str1, $str2);
115120
// }
116-
//
121+
//
117122
// public static function lcs($str1, $str2) {
118123
// $lcs = new LCS($str1, $str2);
119124
// return $lcs->backtrack(strlen($str1), strlen($str2));
120125
// }
121126
// /**
122-
// *
127+
// *
123128
// * @param type $string1
124129
// * @param type $string2
125130
// */
126131
// public static function length($string1, $string2) {
127132
// $lcs = new LCS($str1, $str2);
128133
// return $lcs->C;
129-
//
134+
//
130135
// }
131-
}
136+
}

0 commit comments

Comments
 (0)