php – 从文本块中提取相关标记/关键字
我想要一个特定的实现,以便用户提供一个文本块,如:
我想要做的是自动选择相关的关键字并创建标签/关键字,因此对于上面的文本,相关的标签应该是:mysql,php,json,jquery,版本控制,oop,web2.0,javascript 我怎样才能在PHP / Javascript等中做到这一点?一个headstart真的很有帮助.
一个非常天真的方法是从文本中删除常见的
stopwords,留下更多有意义的单词,如“标准”,“JSON”等.但是你仍会得到很多噪音,所以你可以考虑像
OpenCalais那样的服务对您的文本进行相当复杂的分析.
更新: 好的,我之前回答中的链接指向了实现,但是你要求一个,所以这里有一个简单的: function stopWords($text,$stopwords) { // Remove line breaks and spaces from stopwords $stopwords = array_map(function($x){return trim(strtolower($x));},$stopwords); // Replace all non-word chars with comma $pattern = '/[0-9W]/'; $text = preg_replace($pattern,',$text); // Create an array from $text $text_array = explode(",",$text); // remove whitespace and lowercase words in $text $text_array = array_map(function($x){return trim(strtolower($x));},$text_array); foreach ($text_array as $term) { if (!in_array($term,$stopwords)) { $keywords[] = $term; } }; return array_filter($keywords); } $stopwords = file('stop_words.txt'); $text = "Requirements - Working knowledge,on LAMP Environment using Linux,MySQL 5 and PHP 5,- Knowledge of Web 2.0 Standards - Comfortable with JSON - Hands on Experience on working with Frameworks,OOPs - Cross Browser Javascripting,JQuery etc. - Knowledge of Version Control Software such as sub-version will be preferable."; print_r(stopWords($text,$stopwords)); 你可以在这个Gist中看到这个,以及stop_word.txt的内容. 在示例文本上运行上面的内容会生成以下数组: Array ( [0] => requirements [4] => linux [6] => apache [10] => mysql [13] => php [25] => json [28] => frameworks [30] => zend [34] => browser [35] => javascripting [37] => jquery [38] => etc [42] => software [43] => preferable ) 所以,就像我说的那样,这有些天真并且可以使用更多的优化(加上它很慢),但它会从文本中提取更相关的关键字.您还需要对停用词进行一些微调.捕获像Web 2.0这样的术语将非常困难,所以我认为你最好使用像OpenCalais这样可以理解文本并返回实体和引用列表的严肃服务. DocumentCloud依靠这项服务从文件中收集信息. 此外,对于客户端实现,您可以使用JavaScript执行几乎相同的操作,并且可能更清晰(尽管对于客户端来说可能会很慢). (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |