gpt4 book ai didi

javascript - 如何使用 JavaScript 计算长字符串中多个子字符串的出现次数

转载 作者:行者123 更新时间:2023-11-28 04:39:16 25 4
gpt4 key购买 nike

我是 JavaScript 新手。我只是尝试了很多,但没有得到答案和信息来显示如何一次计算长字符串中多个子字符串的出现次数。

进一步的信息:我需要获取这些子字符串的出现次数,如果它们出现的次数太多,我需要一次替换它们,所以我需要一次性获取出现次数。

这是一个例子:长字符串文本如下,

Super Bowl 50 was an American football game to determine the champion of the National Football League (NFL) for the 2015 season. The American Football Conference (AFC) champion Denver Broncos defeated the National Football Conference (NFC) champion Carolina Panthers 24–10 to earn their third Super Bowl title. The game was played on February 7, 2016, at Levi's Stadium in the San Francisco Bay Area at Santa Clara, California. As this was the 50th Super Bowl, the league emphasized the "golden anniversary" with various gold-themed initiatives, as well as temporarily suspending the tradition of naming each Super Bowl game with Roman numerals (under which the game would have been known as "Super Bowl L"), so that the logo could prominently feature the Arabic numerals 50.

子串是一个问题,但我需要的是一次性统计该子串中每个单词的出现次数。例如,此字符串中的单词“name”、“NFL”、“championship”、“game”和“is”、“the”。

What is the name of NFL championship game?

问题之一是有些子字符串不在文本中,有些子字符串已经显示了很多次。(我可能会替换它)

我尝试过的代码如下,这是错误的,我尝试了很多不同的方法但没有好的结果。

$(".showMoreFeatures").click(function(){
var text= $(".article p").text(); // This is to get the text.

var textCount = new Array();
// Because I use match, so for the first word "what", will return null, so
this is to avoid this null. and I was plan to get the count number, if it is
more than 7 or even more, I will replace them.

var qus = item2.question; //This is to get the sub-string
var checkQus = qus.split(" "); // I split the question to words

var newCheckQus = new Array();
// This is the array I was plan put the sub-string which count number less than 7, which I really needed words.

var count = new Array();
// Because it is a question as sub-string and have many words, so I wan plan to get their number and put them in a array.


for(var k =0; k < checkQus.length; k++){
textCount = text.match(checkQus[k],"g")
if(textCount == null){
continue;
}
for(var j =0; j<checkQus.length;j++){
count[j] = textCount.length;
}
//count++;
}

我尝试了很多不同的方法,也进行了很多搜索,但没有好的结果。上面的代码只是想展示我的尝试和我的想法(可能完全错误)。但实际上它不起作用,如果你知道如何实现它,解决我的问题,请告诉我,不需要纠正我的代码。

非常感谢。

最佳答案

如果我正确理解了问题,那么您似乎需要计算问题中的单词 (que) 在文本 (txt) 中出现的次数...

var txt = "Super Bowl 50 was an American ...etc... Arabic numerals 50.";
var que = "What is the name of NFL championship game?";

我将在普通 JavaScript 中完成此操作,您可以根据需要将其转置为 JQuery。

首先,为了专注于文本,我们可以通过将字符串更改为小写并删除一些标点符号来使事情变得更简单。

// both strings to lowercase
txt = txt.toLowerCase();
que = que.toLowerCase();

// remove punctuation
// using double \\ for proper regular expression syntax
var puncArray = ["\\,", "\\.", "\\(", "\\)", "\\!", "\\?"];

puncArray.forEach(function(P) {
// create a regular expresion from each punctuation 'P'
var rEx = new RegExp( P, "g");

// replace every 'P' with empty string (nothing)
txt = txt.replace(rEx, '');
que = que.replace(rEx, '');
});

现在我们可以从 strque 创建一个更干净的数组,以及从 que 创建一个哈希表,如下所示...

// Arrays: split at every space
var txtArray = txt.split(" ");
var queArray = que.split(" ");

// Object, for storing 'que' counts
var queObject = {};

queArray.forEach(function(S) {
// create 'queObject' keys from 'queArray'
// and set value to zero (0)
queObject[S] = 0;
});

queObject 将用于保存计数的单词。如果你此时要console.debug(queObject),它看起来会像这样......

console.debug(queObject);
/* =>
queObject = {
what: 0,
is: 0,
the: 0,
name: 0,
of: 0,
nfl: 0,
championship: 0,
game: 0
}
*/

现在我们要测试 txtArray 中的每个元素,看看它是否包含 queArray 中的任何元素。如果测试为 true,我们将向等效的 queObject 属性添加 +1,如下所示...

// go through each element in 'queArray'
queArray.forEach(function(A) {
// create regular expression for testing
var rEx = new RegExp( A );

// test 'rEx' against elements in 'txtArray'
txtArray.forEach(function(B) {
// is 'A' in 'B'?
if (rEx.test(B)) {
// increase 'queObject' property 'A' by 1.
queObject[A]++;
}
});
});

我们在这里使用 RegExp test 方法而不是 String match 方法,因为我们只想知道“is A in B == true”是否为真。如果为真,那么我们将相应的 queObject 属性增加 1。此方法还会查找单词中的单词,例如 'San Francisco' 中的 'is' 等.

一切顺利,将 queObject 记录到控制台将显示问题中的每个单词在文本中出现的次数。

console.debug(queObject);
/* =>
queObject = {
what: 0
is: 2
the: 17
name: 0
of: 2
nfl: 1
championship: 0
game: 4
}
*/

希望有帮助。 :)

有关更多信息,请参阅 MDN:

关于javascript - 如何使用 JavaScript 计算长字符串中多个子字符串的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43928087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com