How to extract words without stop words, digits and special characters from text in PHP

1 Answer

0 votes
function extractWords($s) {
    $stopWords = array('i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 
    'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 
    'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 
    'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 'was', 
    'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 
    'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 
    'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 
    'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 
    'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 
    'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 
    'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 
    'will', 'just', 'don', 'should', 'now');
    
    $s = strtolower($s); 
    $s = trim($s); 
    // remove all chars except a-zA-Z
    $s = preg_replace('/[^a-zA-Z]/', ' ', $s); 
    // Remove multiple whitespace 
    $s = preg_replace('/\s\s+/i', ' ', $s); 

    preg_match_all('/\b.*?\b/i', $s, $words);
    $words = $words[0];

    // Get only words that are not stop words with length > 2
    foreach ($words as $key=>$val) {
        if ($val == '' || in_array(strtolower($val), $stopWords) || strlen($val) <= 2) {
            unset($words[$key]);
        }
    }   
    // Remove duplicate words and count word occurrences
    $thewords = array();
    if (is_array($words)) {
        foreach ($words as $key => $val) {
            if (isset($thewords[$val]) ) {
                $thewords[$val]++;
            } else {
                $thewords[$val] = 1;
            }
        }
    }
    arsort($thewords);

    return $thewords;
}

    
$text = "PHP is a    general-purpose   0    scripting language 1 especially suited 
         to web development.     It was originally 123 created by Danish-Canadian   
         programmer Rasmus Lerdorf in 1994;  the PHP reference 
         implementation      6 is now (produced) by The PHP Group.
         PHP code is usually processed on a [web server] 
         by a PHP interpreterr";
$words = extractWords($text);
echo implode(', ', array_keys($words));


/*
run:

php, web, rasmus, server, processed, usually, code, group, produced, now, implementation, 
reference, lerdorf, programmer, general, canadian, danish, created, originally, development, 
suited, especially, language, scripting, purpose, interpreterr

*/

 



answered Sep 29, 2020 by avibootz
edited Sep 29, 2020 by avibootz

Related questions

1 answer 180 views
1 answer 152 views
1 answer 243 views
1 answer 268 views
1 answer 143 views
143 views asked Oct 13, 2020 by avibootz
...