| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
class myTools |
|---|
| 4 |
{ |
|---|
| 5 |
public static function stripText($text) |
|---|
| 6 |
{ |
|---|
| 7 |
$text = strtolower($text); |
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
$text = preg_replace('/\W/', ' ', $text); |
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
$text = preg_replace('/\ +/', '-', $text); |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
$text = preg_replace('/\-$/', '', $text); |
|---|
| 17 |
$text = preg_replace('/^\-/', '', $text); |
|---|
| 18 |
|
|---|
| 19 |
return $text; |
|---|
| 20 |
} |
|---|
| 21 |
|
|---|
| 22 |
public static function stemPhrase($phrase) |
|---|
| 23 |
{ |
|---|
| 24 |
|
|---|
| 25 |
$words = str_word_count(strtolower($phrase), 1); |
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
$words = myTools::removeStopWordsFromArray($words); |
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
$stemmed_words = array(); |
|---|
| 32 |
foreach ($words as $word) |
|---|
| 33 |
{ |
|---|
| 34 |
|
|---|
| 35 |
if (strlen($word) <= 2) |
|---|
| 36 |
{ |
|---|
| 37 |
continue; |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
$stemmed_words[] = PorterStemmer::stem($word, true); |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
return $stemmed_words; |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
public static function removeStopWordsFromArray($words) |
|---|
| 48 |
{ |
|---|
| 49 |
$stop_words = array( |
|---|
| 50 |
'i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', |
|---|
| 51 |
'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', |
|---|
| 52 |
'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', |
|---|
| 53 |
'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', |
|---|
| 54 |
'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', |
|---|
| 55 |
'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', |
|---|
| 56 |
'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', |
|---|
| 57 |
'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', |
|---|
| 58 |
'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', |
|---|
| 59 |
'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', |
|---|
| 60 |
'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', |
|---|
| 61 |
'than', 'too', 'very', |
|---|
| 62 |
); |
|---|
| 63 |
|
|---|
| 64 |
return array_diff($words, $stop_words); |
|---|
| 65 |
} |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
?> |
|---|