- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一系列搜索词,例如-
var searchTerms = ["blow", "search", "fly", "type"]
和一个字符串数组,比如
var arrayToBeSearched = ["blowing", "searched", "flew", "typed", "blah", "blah","blah"]
当我检查数组时,有没有一种简单的方法可以包含过去式或其他单词变体?或者我应该只在 searchTerms 中包含变体吗?
最佳答案
存在词形还原算法,例如波特词干分析器。这会将你的话映射到它们的词干,然后可以直接比较它们是否相等。算法描述here .转载Javascript implementation完整的:
// Porter stemmer in Javascript. Few comments, but it's easy to follow against the rules in the original
// paper, in
//
// Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14,
// no. 3, pp 130-137,
//
// see also http://www.tartarus.org/~martin/PorterStemmer
// Release 1 be 'andargor', Jul 2004
// Release 2 (substantially revised) by Christopher McKenzie, Aug 2009
var stemmer = (function(){
var step2list = {
"ational" : "ate",
"tional" : "tion",
"enci" : "ence",
"anci" : "ance",
"izer" : "ize",
"bli" : "ble",
"alli" : "al",
"entli" : "ent",
"eli" : "e",
"ousli" : "ous",
"ization" : "ize",
"ation" : "ate",
"ator" : "ate",
"alism" : "al",
"iveness" : "ive",
"fulness" : "ful",
"ousness" : "ous",
"aliti" : "al",
"iviti" : "ive",
"biliti" : "ble",
"logi" : "log"
},
step3list = {
"icate" : "ic",
"ative" : "",
"alize" : "al",
"iciti" : "ic",
"ical" : "ic",
"ful" : "",
"ness" : ""
},
c = "[^aeiou]", // consonant
v = "[aeiouy]", // vowel
C = c + "[^aeiouy]*", // consonant sequence
V = v + "[aeiou]*", // vowel sequence
mgr0 = "^(" + C + ")?" + V + C, // [C]VC... is m>0
meq1 = "^(" + C + ")?" + V + C + "(" + V + ")?$", // [C]VC[V] is m=1
mgr1 = "^(" + C + ")?" + V + C + V + C, // [C]VCVC... is m>1
s_v = "^(" + C + ")?" + v; // vowel in stem
return function (w) {
var stem,
suffix,
firstch,
re,
re2,
re3,
re4,
origword = w;
if (w.length < 3) { return w; }
firstch = w.substr(0,1);
if (firstch == "y") {
w = firstch.toUpperCase() + w.substr(1);
}
// Step 1a
re = /^(.+?)(ss|i)es$/;
re2 = /^(.+?)([^s])s$/;
if (re.test(w)) { w = w.replace(re,"$1$2"); }
else if (re2.test(w)) { w = w.replace(re2,"$1$2"); }
// Step 1b
re = /^(.+?)eed$/;
re2 = /^(.+?)(ed|ing)$/;
if (re.test(w)) {
var fp = re.exec(w);
re = new RegExp(mgr0);
if (re.test(fp[1])) {
re = /.$/;
w = w.replace(re,"");
}
} else if (re2.test(w)) {
var fp = re2.exec(w);
stem = fp[1];
re2 = new RegExp(s_v);
if (re2.test(stem)) {
w = stem;
re2 = /(at|bl|iz)$/;
re3 = new RegExp("([^aeiouylsz])\\1$");
re4 = new RegExp("^" + C + v + "[^aeiouwxy]$");
if (re2.test(w)) { w = w + "e"; }
else if (re3.test(w)) { re = /.$/; w = w.replace(re,""); }
else if (re4.test(w)) { w = w + "e"; }
}
}
// Step 1c
re = /^(.+?)y$/;
if (re.test(w)) {
var fp = re.exec(w);
stem = fp[1];
re = new RegExp(s_v);
if (re.test(stem)) { w = stem + "i"; }
}
// Step 2
re = /^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/;
if (re.test(w)) {
var fp = re.exec(w);
stem = fp[1];
suffix = fp[2];
re = new RegExp(mgr0);
if (re.test(stem)) {
w = stem + step2list[suffix];
}
}
// Step 3
re = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/;
if (re.test(w)) {
var fp = re.exec(w);
stem = fp[1];
suffix = fp[2];
re = new RegExp(mgr0);
if (re.test(stem)) {
w = stem + step3list[suffix];
}
}
// Step 4
re = /^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/;
re2 = /^(.+?)(s|t)(ion)$/;
if (re.test(w)) {
var fp = re.exec(w);
stem = fp[1];
re = new RegExp(mgr1);
if (re.test(stem)) {
w = stem;
}
} else if (re2.test(w)) {
var fp = re2.exec(w);
stem = fp[1] + fp[2];
re2 = new RegExp(mgr1);
if (re2.test(stem)) {
w = stem;
}
}
// Step 5
re = /^(.+?)e$/;
if (re.test(w)) {
var fp = re.exec(w);
stem = fp[1];
re = new RegExp(mgr1);
re2 = new RegExp(meq1);
re3 = new RegExp("^" + C + v + "[^aeiouwxy]$");
if (re.test(stem) || (re2.test(stem) && !(re3.test(stem)))) {
w = stem;
}
}
re = /ll$/;
re2 = new RegExp(mgr1);
if (re.test(w) && re2.test(w)) {
re = /.$/;
w = w.replace(re,"");
}
// and turn initial Y back to y
if (firstch == "y") {
w = firstch.toLowerCase() + w.substr(1);
}
return w;
}
})();
关于javascript - 在检查字符串时有没有办法包含单词变体(过去式、动名词)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15794411/
我有一些文档,它的创建时间以毫秒为单位。我需要将它们分为今天、昨天、过去 7 天、过去 30 天、超过 30 天。 我使用了以下代码:convertSimpleDayFormat(1347022979
我正在尝试创建一个 Crystal 报告,用于在图表中显示十二个月的月费率。 对于此任务,我有一个 dc_date(mm/dd/yyyy) 作为变量,我将其更改为 mmyyyy 格式。但是当我看到月份
我想使用 Linux 命令将年转换为天(过去)。例如: 用户给出的号码:10年前 程序应计算 10 年前的天数总和(例如:10*12*31(年/月/日)--> 3720 天)。 程序将其与今年开始的当
我的要求有点复杂,为了便于理解,让我用简单的例子说明一下。假设,我们必须计算过去 12 个月消费者按月购买的产品数量。所以我们要做的是: SELECT DATENAME(MONTH, m.Invoic
如果我今天(2012 年 8 月 21 日)运行查询,我想得到这个结果集: [...] 为了得到这个,我正在使用以下内容,但它似乎过于复杂。这可以简化吗? ;WITH Numbers_cte([num
这是我目前用来显示下周五日期的方法。 $date_friday = date('F j', strtotime('Friday')); print $date_friday; 我应该使用什么来显示过去
我有一个包含一个日期时间列的表。我只需要返回过去 6 个月的行。这可以通过 来完成 where datetime_column > DATEADD(m, -6, current_timestamp)
我正在为我的应用使用 Firestore,并且希望能够获得过去 7 天内发布的最受欢迎的项目。在理想的情况下,调用看起来像这样: let calendar = Calendar.cur
我有以下方法来计算时间: public static String sumTime(String date1, String date2) throws ParseException { Ca
我想检索过去 24 小时内发布的观看次数最多的前 10 个主题。如果网站在过去 24 小时内获得的主题少于 10 个,则必须检索前一天剩余的浏览次数最多的主题。 我以unix格式将时间存储在数据库中,
我想从我的 MySQL 服务器获取过去 x 个月的所有记录。例如使用 2 个月(不是过去 2 个月,比如过去 60 天,而是过去整个月等等。如果实际月份是 4 月,我想要 2 月和 3 月的所有记录)
我正在尝试使用 php 创建一个博客系统,我需要一种方法来计算过去 21 天内浏览次数最多的帖子。有人知道这样做的好方法吗? 我对 php 没有太多经验,所以我需要有人给我指出正确的方向。我试图研究
我正在尝试查找过去 26 周每周结束时的用户总数。 例如, 从第 26 周开始,该周有 100 名用户注册,并且有 20 名新用户注册,因此到第 26 周结束时,注册用户数量为 120 名 从第 25
假设我们有下表,列如下所示: Rides ride_id start_time end_time passenger_id driver_id ride
好的,我正在尝试获取过去 24 小时内包含“价格”列的所有行的平均值。 $tickerBuy = $this->model->ticker("date AS datetime, avg(price)
我有一个账户表,其中名为“dateCreation”的列是账户的创建日期。 我想获取过去 12 个月内创建的所有帐户,但有时 2 个月或更长时间都没有创建帐户。在这几个月里,我希望 mysql 返回
是否有办法检索过去 7 天内添加的所有用户?添加用户时,包含时间戳。日期被添加到他们的“信息”中。 例如这样: 因此日期公式为:D/M/YYYY。谁能帮我理解如何做到这一点? PS:奇怪的OL3gee
我有一个 Angualr 服务,可以列出从当前月份开始的 12 个月和年份: JS: app.factory('12months', function() { return {
我有一个弹出窗口,其中一个名为 fade 的元素应该扩展到屏幕的整个宽度(当没有可滚动内容时它会这样做)。但是,当内容超过浏览器窗口的 100% 时,它不会扩展到页面高度的 100%。 如果我设置 h
我正在尝试选择过去 5 天的整数字段的总和,我需要将它分组为每一天。 我在确定分组时遇到了一些问题。到目前为止,这是我的 sql 查询: select sum(`amount_sale`) a
我是一名优秀的程序员,十分优秀!