一个非常幼稚的方法是从文本中删除常见的停用词,留下更有意义的词,如“标准”、“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-9\W]/';
$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, Apache 2, MySQL 5 and PHP 5, - Knowledge of Web 2.0 Standards - Comfortable with JSON - Hands on Experience on working with Frameworks, Zend, OOPs - Cross Browser Javascripting, JQuery etc. - Knowledge of Version Control Software such as sub-version will be preferable.";
print_r(stopWords($text, $stopwords));
你可以看到这个,以及stop_word.txt
这个Gist中的内容。
在您的示例文本上运行上述代码会生成以下数组:
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 做几乎相同的事情,并且可能更干净(尽管它可能对客户端来说很慢。)