【发布时间】:2014-07-19 15:02:59
【问题描述】:
我有一个内容聚合网站。我想处理帖子标题以生成最受欢迎的帖子主题列表。主题可以是“软件开发”,但重要的一点是“软件”和“开发”这两个词在帖子标题中不必直接相邻。
这个想法是生成一个可能的集合列表(一组基于同一主题的帖子)
我已经开始编写代码,并且到目前为止已经设法生成了一个最常用单词的列表。现在我需要生成一个新列表,按帖子标题中多个单词的出现次数排序,我希望这能让我得到我最终的热门主题列表。
寻求帮助以完成此任务。在我的代码中添加了 cmets 来描述我到目前为止所做的事情。
另外,如果有人知道更好的方法,请告诉我!
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$this->info('Starting...');
// get last 1000 posts
$posts = Post::orderBy('created_at', 'desc')->take(1000)->get();
// create an array of post titles
$titles_arr = array_map(function($n){
return $n['title'];
}, $posts->toArray());
// create a big string of all the post titles
$titles_str = implode(' ', $titles_arr);
// create an array of the above string
$words = explode(' ', strtolower($titles_str));
// words to ignore
$stopwords = array('a', 'about', 'above', 'across', 'after', 'afterwards', 'again', 'against', 'all', 'almost', 'alone', 'along', 'already', 'also','although','always','am','among', 'amongst', 'amoungst', 'amount', 'an', 'and', 'another', 'any','anyhow','anyone','anything','anyway', 'anywhere', 'are', 'around', 'as', 'at', 'back','be','became', 'because','become','becomes', 'becoming', 'been', 'before', 'beforehand', 'behind', 'being', 'below', 'beside', 'besides', 'between', 'beyond', 'bill', 'both', 'bottom','but', 'by', 'call', 'can', 'cannot', 'cant', 'co', 'con', 'could', 'couldnt', 'cry', 'de', 'describe', 'detail', 'do', 'done', 'down', 'due', 'during', 'each', 'eg', 'eight', 'either', 'eleven','else', 'elsewhere', 'empty', 'enough', 'etc', 'even', 'ever', 'every', 'everyone', 'everything', 'everywhere', 'except', 'few', 'fifteen', 'fify', 'fill', 'find', 'fire', 'first', 'five', 'for', 'former', 'formerly', 'forty', 'found', 'four', 'from', 'front', 'full', 'further', 'get', 'give', 'go', 'had', 'has', 'hasnt', 'have', 'he', 'hence', 'her', 'here', 'hereafter', 'hereby', 'herein', 'hereupon', 'hers', 'herself', 'him', 'himself', 'his', 'how', 'however', 'hundred', 'ie', 'if', 'in', 'inc', 'indeed', 'interest', 'into', 'is', 'it', 'its', 'itself', 'keep', 'last', 'latter', 'latterly', 'least', 'less', 'ltd', 'made', 'many', 'may', 'me', 'meanwhile', 'might', 'mill', 'mine', 'more', 'moreover', 'most', 'mostly', 'move', 'much', 'must', 'my', 'myself', 'name', 'namely', 'neither', 'never', 'nevertheless', 'next', 'nine', 'no', 'nobody', 'none', 'noone', 'nor', 'not', 'nothing', 'now', 'nowhere', 'of', 'off', 'often', 'on', 'once', 'one', 'only', 'onto', 'or', 'other', 'others', 'otherwise', 'our', 'ours', 'ourselves', 'out', 'over', 'own','part', 'per', 'perhaps', 'please', 'put', 'rather', 're', 'same', 'see', 'seem', 'seemed', 'seeming', 'seems', 'serious', 'several', 'she', 'should', 'show', 'side', 'since', 'sincere', 'six', 'sixty', 'so', 'some', 'somehow', 'someone', 'something', 'sometime', 'sometimes', 'somewhere', 'still', 'such', 'system', 'take', 'ten', 'than', 'that', 'the', 'their', 'them', 'themselves', 'then', 'thence', 'there', 'thereafter', 'thereby', 'therefore', 'therein', 'thereupon', 'these', 'they', 'thickv', 'thin', 'third', 'this', 'those', 'though', 'three', 'through', 'throughout', 'thru', 'thus', 'to', 'together', 'too', 'top', 'toward', 'towards', 'twelve', 'twenty', 'two', 'un', 'under', 'until', 'up', 'upon', 'us', 'very', 'via', 'was', 'we', 'well', 'were', 'what', 'whatever', 'when', 'whence', 'whenever', 'where', 'whereafter', 'whereas', 'whereby', 'wherein', 'whereupon', 'wherever', 'whether', 'which', 'while', 'whither', 'who', 'whoever', 'whole', 'whom', 'whose', 'why', 'will', 'with', 'within', 'without', 'would', 'yet', 'you', 'your', 'yours', 'yourself', 'yourselves', 'the');
// $words after stop words are filtered out
$final_words = array_filter($words, function($n) use($stopwords){
return (!in_array(strtolower($n), $stopwords));
});
// count occurence of array values
$reduce = array_count_values($final_words);
arsort($reduce); // sort
// take first 1000 popular words
$top_1000 = array_slice($reduce, 0, 1000);
// I have the top 1000 used words. Now need to find which ones are present together the most (ordered)
$matched_total = array();
// for each post title
foreach ($titles_arr as $title) {
// split it into array
$words = explode(' ', strtolower($title));
$matched = array();
echo $title . "\n";
foreach ($words as $word) {
// if in $top_1000 print and add to $matched array
if(array_key_exists($word, $top_1000)) {
echo $word . "\n";
$matched[] = $word;
}
}
// if contains popular words at to matched_total
if(count($matched) > 0)
$matched_total[] = $matched;
}
// have the words that match for each post, now need to get which ones appear together the most
$this->info('Finished.');
}
【问题讨论】:
标签: php algorithm data-mining data-processing