Jump to content

help needed to assign score to words

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Rahul Dev

Rahul Dev

    Newbie

  • Members
  • Pip
  • 5 posts
Hello guys, i need some help in assigning a score to a word according to its occurrence in a group of sentences.

$words="Hello how are you";

$word_array = explode(" ", $words);


$paragraph="Hello! I am fine. How about you? You look good.";

$sencetences = strtok($paragraph, ".!?");


now i'm trying to assign a score to each word in $words e.g Hello=1, how=1, are=0, you=2.

Edited by Rahul Dev, 17 December 2010 - 10:40 PM.


#2
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
This function takes in a string, and returns an array that looks like this :
$array[word] = count

<?php

    function countIt($string)

    {

        $string = preg_replace('/ss+/i', '', $string);

        $string = trim($string);

		  // only take alphanumerical characters, but keep the spaces and dashes too…

        $string = preg_replace('/[^a-zA-Z0-9 -]/', '', $string); 

        $string = strtolower($string); // make it lowercase

        preg_match_all('/\b.*?\b/i', $string, $matchWords);

        $matchWords = $matchWords[0];

        $wordCountArr = array();

            if ( is_array($matchWords) ) {

                foreach ( $matchWords as $key => $val ) {

                    $val = strtolower($val);

                    if ( isset($wordCountArr[$val]) ) {

                        $wordCountArr[$val]++;

                    } else {

                    $wordCountArr[$val] = 1;

                        }

                }

            }

            arsort($wordCountArr);

            return $wordCountArr;

        }

?> 


:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it