-
Notifications
You must be signed in to change notification settings - Fork 0
/
Winnowing.php
180 lines (158 loc) · 4.88 KB
/
Winnowing.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
class Winnowing {
private $word1 = '';
private $word2 = '';
//input properties
private $prime_number = 3;
private $n_gram_value = 2;
private $n_window_value = 4;
//output properties
private $arr_n_gram1;
private $arr_n_gram2;
private $arr_rolling_hash1;
private $arr_rolling_hash2;
private $arr_window1;
private $arr_window2;
private $arr_fingerprints1;
private $arr_fingerprints2;
public function SetPrimeNumber($value){
$this->prime_number = $value;
}
public function SetNGramValue($value){
$this->n_gram_value = $value;
}
public function SetNWindowValue($value){
$this->n_window_value = $value;
}
public function GetNGramFirst(){
return $this->arr_n_gram1;
}
public function GetNGramSecond(){
return $this->arr_n_gram2;
}
public function GetRollingHashFirst(){
return $this->arr_rolling_hash1;
}
public function GetRollingHashSecond(){
return $this->arr_rolling_hash2;
}
public function GetWindowFirst(){
return $this->arr_window1;
}
public function GetWindowSecond(){
return $this->arr_window2;
}
public function GetFingerprintsFirst(){
return $this->arr_fingerprints1;
}
public function GetFingerprintsSecond(){
return $this->arr_fingerprints2;
}
public function GetJaccardCoefficient($prosen = true){
if($prosen)
return round( ($this->jaccard_coefficient * 100), 2);
else
return $this->jaccard_coefficient;
}
/* function __construct($w1, $w2){
$this->word1 = $w1;
$this->word2 = $w2;
} */
public function setJudul($j1, $j2)
{
$this->word1 = $j1;
$this->word2 = $j2;
}
public function process(){
if (($this->word1 == '') || ($this->word2 == '')) exit;
//langkah 1 : buang semua huruf yang bukan kelompok [a-z A-Z 0-9] dan ubah menjadi huruf kecil semua (lowercase)
$this->word1 = strtolower(str_replace(' ','',preg_replace("/[^a-zA-Z0-9\s-]/", "", $this->word1)));
$this->word2 = strtolower(str_replace(' ','',preg_replace("/[^a-zA-Z0-9\s-]/", "", $this->word2)));
//langkah 2 : buat N-Gram
$this->arr_n_gram1 = $this->n_gram($this->word1, $this->n_gram_value);
$this->arr_n_gram2 = $this->n_gram($this->word2, $this->n_gram_value);
//langkah 3 : rolling hash untuk masing-masing n gram
$this->arr_rolling_hash1 = $this->rolling_hash($this->arr_n_gram1);
$this->arr_rolling_hash2 = $this->rolling_hash($this->arr_n_gram2);
//langkah 4 : buat windowing untuk masing-masing tabel hash
$this->arr_window1 = $this->windowing($this->arr_rolling_hash1, $this->n_window_value);
$this->arr_window2 = $this->windowing($this->arr_rolling_hash2, $this->n_window_value);
//langkah 5 : cari nilai minimum masing-masing window table (fingerprints)
$this->arr_fingerprints1 = $this->fingerprints($this->arr_window1);
$this->arr_fingerprints2 = $this->fingerprints($this->arr_window2);
//langkah 6 : hitung koefisien plagiarisme memanfaatkan persamaan Jaccard Coefficient
$this->jaccard_coefficient = $this->jaccard_coefficient($this->arr_fingerprints1, $this->arr_fingerprints2);
}
private function n_gram($word, $n) {
$ngrams = array();
$length = strlen($word);
for($i = 0; $i < $length; $i++) {
if($i > ($n - 2)) {
$ng = '';
for($j = $n-1; $j >= 0; $j--) {
$ng .= $word[$i-$j];
}
$ngrams[] = $ng;
}
}
return $ngrams;
}
private function char2hash($string) {
if (strlen($string) == 1) {
return ord($string);
} else {
$result = 0;
$length = strlen($string);
for ($i = 0; $i < $length; $i++) {
$result += ord(substr($string, $i, 1)) * pow($this->prime_number, $length-$i);
}
return $result;
}
}
private function rolling_hash($ngram){
$roll_hash = array();
foreach($ngram as $ng){
$roll_hash[] = $this->char2hash($ng);
}
return $roll_hash;
}
private function windowing($rolling_hash, $n){
$ngram = array();
$length = count($rolling_hash);
$x = 0;
for($i = 0; $i < $length; $i++){
if($i > ($n - 2)) {
$ngram[$x] = array();
$y = 0;
for($j = $n-1; $j >= 0; $j--){
$ngram[$x][$y] = $rolling_hash[$i-$j];
$y++;
}
$x++;
}
}
//echo $x.' '.$y;
return $ngram;
}
private function fingerprints($window_table){
$fingers = array();
for($i = 0; $i < count($window_table); $i++){
$min = $window_table[$i][0];
for($j = 1 ; $j < $this->n_window_value; $j++){
if($min > $window_table[$i][$j])
$min = $window_table[$i][$j];
}
$fingers[] = $min;
}
return $fingers;
}
private function jaccard_coefficient($fingerprint1, $fingerprint2){
$arr_intersect = array_intersect( $fingerprint1, $fingerprint2 );
$arr_union = array_merge( $fingerprint1, $fingerprint2 );
$count_intersect_fingers = count($arr_intersect);
$count_union_fingers = count( $arr_union );
$coefficient = $count_intersect_fingers /
($count_union_fingers - $count_intersect_fingers );
return $coefficient;
}
}