如何找到有特殊单词的句子?(问题:缩写和点)



我用Javascript写的代码是用搜索点找到句子。但是缩略语是个问题。如何编写忽略缩写的代码?

我代码:

console.clear();
var text = "They tried to kill Alan Rackham about an hour after he had seen the accident. They bungled the job. They shot at him from ambush—with an ordinary automatic pistol—as he was walking up to his house; and Brave, who had a sixth sense for danger which never failed him, knocked Alan over at the very instant of the shot and sprawled across him, a great solid shield holding him down and protecting him despite his angry wrigglings. Brave's grenade pistol was in his hand before the two of them hit ground, and he sent four quick shots at the bushes, spaced so that the tiny hot fragments tore hell out of thirty yards of shrubbery. Nobody yelled about or groaned. Brave waited a full minute, and then he rose cautiously, so that Alan could sit up and brush himself off and swear as he spat out dirt. They about went into the house and Alan reported the assassination attempt to his immediate superior, Dr. Getty. After that they didn't try again to kill Alan for a long time.";
function searchWord(word) {
var i = 0;
var t = 0;
var c = 0;
var start = 0;
while (c == 0) {
i = 0;
t = 0;
var numb = text.indexOf(word, start);
var numb2 = text.indexOf(word, start) - 1;
if (text.indexOf(word, start) == -1) {
document.write("<br>there is no any sentences about your word");
c = 1;
} else {
while (i == 0) {
if (text.charAt(numb) == ".") {
i = 1;
console.log("Ladies and gentlemen, we got him.");
console.log(numb);
if (text.charAt(numb + 1) == " " && text.charAt(numb + 2) == text.charAt(numb +
2).toUpperCase()) {
console.log("omg, perfect :o");
} else {
numb = numb + 1;
}
} else {
numb = numb + 1;
}
}
while (t == 0) {
if (text.charAt(numb2) == ".") {
t = 1;
console.log("Ladies and gentlemen we got him twice");
console.log(numb2);
} else {
numb2 = numb2 - 1;
if (numb2 == 0) {
t = 1;
}
}
}
start = numb;
if (numb2 == 0) {
document.write(text.slice(numb2, numb + 1));
} else {
document.write(text.slice(numb2 + 1, numb + 1));
}
}
}
}
searchWord("about");

运行:他们试图杀死艾伦·拉克姆大约一个小时后,他看到了事故。没有人大声喊叫或呻吟。他们走进房子,艾伦向他的直接上级报告了暗杀企图。

没有关于你的词的句子

代码认为"这是句末"。

我想创建一个包含常用缩写的数组,但是有这样一个问题:例如,如果代码忽略缩写,那么程序就不能定义这样的句子&;这是我的tel.&;

这是一个大问题,如果你想做得很好,你需要学习语言处理。这不仅仅是字符串操作的问题,这个问题与语言的许多部分有关。我建议你使用nlp.js,它是在JS中实验LP的最佳库。

如果没有NLP,或者非常复杂的算法,或者使用一个包含所有可能缩写的非常大的字典,这是不可能的。

你的问题是:

...this is some sentence with an abbr. to show an example.  The next sentence also using an abbr. will continue...

使用上面的例子,没有办法搜索"。"并确定它是一个句子,句子中有3个点,在句子结束之前和开始之后,所以你甚至不能测试字符串的长度或类似的东西。

最新更新