root/trunk/lib/myTools.class.php

Revision 64, 2.1 kB (checked in by fabien, 3 years ago)

day 23 modifications

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 class myTools
4 {
5   public static function stripText($text)
6   {
7     $text = strtolower($text);
8
9     // strip all non word chars
10     $text = preg_replace('/\W/', ' ', $text);
11
12     // replace all white space sections with a dash
13     $text = preg_replace('/\ +/', '-', $text);
14
15     // trim dashes
16     $text = preg_replace('/\-$/', '', $text);
17     $text = preg_replace('/^\-/', '', $text);
18
19     return $text;
20   }
21
22   public static function stemPhrase($phrase)
23   {
24     // split into words
25     $words = str_word_count(strtolower($phrase), 1);
26
27     // ignore stop words
28     $words = myTools::removeStopWordsFromArray($words);
29
30     // stem words
31     $stemmed_words = array();
32     foreach ($words as $word)
33     {
34       // ignore 1 and 2 letter words
35       if (strlen($word) <= 2)
36       {
37         continue;
38       }
39
40       // stem word (stemming is specific for each language)
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 ?>
Note: See TracBrowser for help on using the browser.