I would like to generate a title automatically based on the most repeated word in a text using PHP. Example: if the the word "PHP" is repeated the most in a text The title would be : "The text is about PHP".... and so on. I have no clue what to do or from where to start.
Can anyone help me with this?
if i must complete your homework assignment for you, i require full attribution within the paper and the link to this question also in said paper.
i also require that you actually read, understand and attempt to run this code to enable you to understand it.
//get all the test from the file
$text_from_file = file_get_contents("filename.txt");
//get all the words within that text
$words = str_word_count($text_from_file , 1);
//count up all the unique words within the array
$unique = array_count_values($words);
//sort by most to least frequent
arsort($unique); //arsort required to keep keys and values together
//since we dont know the key values here, we need to use foreach
foreach($unique as $key => $val) {
echo("The most common word is " . $key . " which occurs " . $val . " times");
break; //always break after the first echo
}
using
print_r( array_count_values(str_word_count($text, 1)) );
will give you a count of all words. You can then choose the top one when sorted ?
rsort
will give you a sorted array from high to low
<?php
function mostRepeated($string = false, $words_num = 5) {
$string = strtolower($string);
// extend this array
$omit_words = array('the', 'a', 'an', 'in', 'at', 'by', 'of', 'was', 'is', 'he', 'she');
$words = explode(' ', $string);
foreach($words as $k => $v) {
if(in_array($word, $omit_words)) unset($words[$k]);
}
$count = array_count_values($words);
arsort($count);
$result = array();
foreach($count as $k => $v) {
$result[] = $k;
}
return $result;
}
$text = 'PHP foo Bar php foO pHp';
$most_repeated_words_array = mostRepeated($text, 3);
print_r($most_repeated_words_array);
?>
output:
Array
(
[0] => php
[1] => foo
[2] => bar
)