从字符串中提取关键字:Javascript
Extract Keywords from String: Javascript
让我们考虑我有一个字符串&想要为SEO提取不常见的关键词。$text = "This is some text. This is some text. Vending Machines are great.";
&将定义一个常用词数组以忽略提取列表中的关键字,如$commonWords = ['i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www'];
预期输出:Result=[some,text,machines,vending]
若有人能帮助我们编写从字符串中提取关键字的通用逻辑或过程,我将不胜感激?
这会有所帮助(它支持多种语言):
https://github.com/michaeldelorenzo/keyword-extractor
var sentence = "President Obama woke up Monday facing a Congressional defeat that many in both parties believed could hobble his presidency."
// Extract the keywords
var extraction_result = keyword_extractor.extract(sentence,{
language:"english",
remove_digits: true,
return_changed_case:true,
remove_duplicates: false
});
有些人喜欢这个
var $commonWords = ['i','a','about','an','and','are','as','at','be','by','com','de','en','for','from','how','in','is','it','la','of','on','or','that','the','this','to','was','what','when','where','who','will','with','und','the','www'];
var $text = "This is some text. This is some text. Vending Machines are great.";
// Convert to lowercase
$text = $text.toLowerCase();
// replace unnesessary chars. leave only chars, numbers and space
$text = $text.replace(/[^'w'd ]/g, '');
var result = $text.split(' ');
// remove $commonWords
result = result.filter(function (word) {
return $commonWords.indexOf(word) === -1;
});
// Unique words
result = result.unique();
console.log(result);
var string = "This is some text. This is some text. Vending Machines are great.";
var substrings = ['your','words', 'here'],
var results = array();
for (var i = substrings.length - 1; i >= 0; --i) {
if (string.indexOf(substrings[i]) != -1) {
// str contains substrings[i]
array.push(substrings[i]);
}
}
var arrayLength = commonWords.length;
var words = []; //new array to say the words
for (var i = 0; i < arrayLength; i++) {
if ($text.indexOf(commonWords[i]) > -1){
words.push(commonWords[i]);
}
}
相关文章:
- 使用“;这个“;JavaScript原型方法中的关键字
- JavaScript 中的嵌套函数和 “this” 关键字
- 重写需要javascript中带有import关键字的语法
- Javascript 对象和 this 关键字
- 如何在不知道关键字的情况下访问javascript对象值
- 可以在Javascript中添加新的关键字吗
- javascript中的这个关键字
- Javascript关键字搜索子类Div值
- 在对象中用Javascript编写此关键字
- 使用Javascript根据URL中的关键字隐藏下拉选项
- Javascript 中的 const 关键字 scope
- javascript关键字查找器循环
- Javascript 关键字 THIS 和 ELEMENT
- 使用 RhinoScriptEngine,我如何更改 javascript 关键字“this”所指的内容
- Javascript关键字“;这个“;变得未定义
- 别名javascript关键字
- Regex来匹配函数和函数调用,但不包括JavaScript关键字
- 一个真正的JavaScript关键字或者只是notepad++中的一个bug
- 如何使用Javascript关键字作为变量名
- 为..的 - JavaScript 关键字 Internet Explorer 变通方法