-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStringSearch.php
149 lines (112 loc) · 3.06 KB
/
StringSearch.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
<?php
require_once 'StringIndexer.php';
/**
* StringSearch
*
* @author Marshall
*/
class StringSearch {
private $stringIndex;
private $words;
private $wordPositions;
private $patternIndex;
public function __construct($string) {
$this->stringIndex = new StringIndexer();
$this->stringIndex->indexString($string);
}
/**
*
* @param string $string
* @return bool
*/
public function isStringPresent($string) {
$words = explode(' ', $string);
$wordPositions = array();
foreach($words as $word) {
$wordPositions[$word] = $this->stringIndex->getIndexArrayOfWord($word);
if(!$wordPositions[$word]) {
return false;
}
}
foreach($wordPositions[$words[0]] as $position) {
$continue = true;
for($i = 1; $i < count($wordPositions) && $continue; $i++) {
$wordIsPresent = false;
for($y = 0; $y < count($wordPositions[$words[$i]]); $y++) {
if($position + $i == $wordPositions[$words[$i]][$y]) {
$wordIsPresent = true;
}
}
if(!$wordIsPresent) {
$continue = false;
}
}
}
return true;
}
/**
*
* @param string $string string to match, %p is replaced with the pattern
* @param string $pattern regex pattern
* @return array matches to pattern
*/
public function searchContainingPattern($string, $pattern) {
$this->words = explode(' ', $string);
$this->patternIndex = array_search('%p', $this->words);
$this->getWordPositions();
$matchingIndexs = array();
for($z = 0; $z < count($this->wordPositions[$this->words[0]]); $z++) {
if($this->areAllWordsPresent($z)) {
$matchingIndexs[] = $z;
}
}
return $this->getMatchesToPattern($matchingIndexs, $pattern);
}
private function getWordPositions() {
$words = &$this->words;
$wordPositions = &$this->wordPositions;
$wordPositions = array();
for($t = 0; $t < count($words); $t++) {
$searchResult = array_search($t, $this->patternIndex);
if($t != $this->patternIndex/*$searchResult && is_bool($searchResult)*/) {
$wordPositions[$words[$t]] = $this->stringIndex->getIndexArrayOfWord($words[$t]);
if(!$wordPositions[$words[$t]]) {
return false;
}
}
}
}
private function areAllWordsPresent($z) {
$continue = true;
for($i = 1; $i < count($this->wordPositions) && $continue; $i++) {
if(!$i == $this->patternIndex) {
if(!$this->isWordPresent($i, $z)) {
$continue = false;
}
}
}
return $continue;
}
private function isWordPresent($i,$z) {
$wordIsPresent = false;
$positions = &$this->wordPositions;
$words = &$this->words;
for($y = 0; $y < count($positions[$words[$i]]); $y++) {
if($positions[$words[0]][$z] + $i == $positions[$words[$i]][$y]) {
$wordIsPresent = true;
}
}
return $wordIsPresent;
}
private function getMatchesToPattern($matchingIndexs, $pattern) {
$matches = array();
foreach($matchingIndexs as $index) {
$string = $this->stringIndex->getWordAtIndex($this->wordPositions[$this->words[0]][$index] + $this->patternIndex);
if(preg_match($pattern, $string)) {
$matches[] = $string;
}
}
return $matches;
}
}
?>