如何计算一个词在对象中 string/store 中出现的次数?
How do I count the number of occurrences of a word in a string/store it in an object?
我正在尝试编写一个函数,当给定一个字符串时,returns一个对象,其中每个键都是给定字符串中的一个单词,其值是该单词在给定字符串中出现的次数字符串。
这是我拥有的:
function countWords(str) {
var strArray = str.split(' ');
var output = {};
if(str.length === 0) {
return output;
} else {
strArray.map(function(n) {
output[n] = str.split(n).length - 1;
});
}
return output;
}
这是我添加时的控制台输出...
console.log(strArray);
console.log(output);
...到代码:
Object {
a: 4,
ask: 1,
bunch: 3,
get: 1,
try: 1
}
["ask", "a", "bunch", "try", "a", "bunch", "get", "a", "bunch"]
由于某些原因,'a'出现的次数太多了 1 次,但其他都是正确的。
有人看到我做错了什么吗?
问题是当你这样做时
str.split(n)
如果 n
很短,特别是如果它是单个字符,例如 a
,那么它可能出现在字符串中其他单词的 内部 也会分裂。例如,'abc a abc'.split('a')
将导致长度为 4 的数组,即使字符串中只有一个 a
。
解决方案的一个可能步骤是使用正则表达式,并在单词周围放置单词边界:
output[n] = str.split(new RegExp(String.raw`\b${n}\b`)).length - 1
但是不使用中间数组会更优雅,而是使用reduce
:
const countWords = str => str.split(' ').reduce((a, word) => {
a[word] = (a[word] || 0) + 1;
return a;
}, {});
console.log(countWords('ask a bunch, try a bunch, get a bunch'));
不是内部拆分,而是将计数增加一:
strArray.forEach(function(n) {
output[n] = (output[n] || 0) + 1;
});
要计算单词出现的次数,给定的字符串可以用 space 拆分,或者您可以使用 space 作为分隔符逐字阅读。
这里是java中的示例例子,在这个例子中java Scanner class用于从给定的输入流中逐字读取,如果单词匹配,计数器值将增加。
import java.util.Scanner;
public class WordOccurrencesInGivenString {
public static void main(String[] args) {
String word = "try";
try (Scanner s = new Scanner(System.in)) {
int counter = 0;
String str = null;
do {
str = s.next();
if (str.equals(word)) {
++counter;
}
} while (!str.equals("exit"));
System.out.println("No of occurrences of a word [" + word + "] : " + counter);
} catch (Exception e) {
e.printStackTrace();
}
}
}
在这个例子中"exit"单词用于停止从输入流中读取数据并打印单词出现的次数。
我正在尝试编写一个函数,当给定一个字符串时,returns一个对象,其中每个键都是给定字符串中的一个单词,其值是该单词在给定字符串中出现的次数字符串。
这是我拥有的:
function countWords(str) {
var strArray = str.split(' ');
var output = {};
if(str.length === 0) {
return output;
} else {
strArray.map(function(n) {
output[n] = str.split(n).length - 1;
});
}
return output;
}
这是我添加时的控制台输出...
console.log(strArray);
console.log(output);
...到代码:
Object { a: 4, ask: 1, bunch: 3, get: 1, try: 1 } ["ask", "a", "bunch", "try", "a", "bunch", "get", "a", "bunch"]
由于某些原因,'a'出现的次数太多了 1 次,但其他都是正确的。
有人看到我做错了什么吗?
问题是当你这样做时
str.split(n)
如果 n
很短,特别是如果它是单个字符,例如 a
,那么它可能出现在字符串中其他单词的 内部 也会分裂。例如,'abc a abc'.split('a')
将导致长度为 4 的数组,即使字符串中只有一个 a
。
解决方案的一个可能步骤是使用正则表达式,并在单词周围放置单词边界:
output[n] = str.split(new RegExp(String.raw`\b${n}\b`)).length - 1
但是不使用中间数组会更优雅,而是使用reduce
:
const countWords = str => str.split(' ').reduce((a, word) => {
a[word] = (a[word] || 0) + 1;
return a;
}, {});
console.log(countWords('ask a bunch, try a bunch, get a bunch'));
不是内部拆分,而是将计数增加一:
strArray.forEach(function(n) {
output[n] = (output[n] || 0) + 1;
});
要计算单词出现的次数,给定的字符串可以用 space 拆分,或者您可以使用 space 作为分隔符逐字阅读。
这里是java中的示例例子,在这个例子中java Scanner class用于从给定的输入流中逐字读取,如果单词匹配,计数器值将增加。
import java.util.Scanner;
public class WordOccurrencesInGivenString {
public static void main(String[] args) {
String word = "try";
try (Scanner s = new Scanner(System.in)) {
int counter = 0;
String str = null;
do {
str = s.next();
if (str.equals(word)) {
++counter;
}
} while (!str.equals("exit"));
System.out.println("No of occurrences of a word [" + word + "] : " + counter);
} catch (Exception e) {
e.printStackTrace();
}
}
}
在这个例子中"exit"单词用于停止从输入流中读取数据并打印单词出现的次数。