计算一对单词在 java 中出现了多少次
Count how many times pair of words appeared in java
如何获取给定字符串上的单词对示例
快,快,棕色,棕色狐狸,狐狸跳
跳过去等...
那你数一数出现了多少次?
下面的代码只能统计单个单词。
import java.util.*;
import java.util.Map;
import java.util.HashMap;
public class Tokenizer
{
public static void main(String[] args)
{
int index = 0; int tokenCount; int i =0;
Map<String,Integer> wordCount = new HashMap<String,Integer>();
Map<Integer,Integer> letterCount = new HashMap<Integer,Integer>();
String message="The Quick brown fox jumps over the lazy brown dog the quick";
StringTokenizer string = new StringTokenizer(message);
tokenCount = string.countTokens();
System.out.println("Number of tokens = " + tokenCount);
while (string.hasMoreTokens()) {
String word = string.nextToken().toLowerCase();
Integer count = wordCount.get(word);
Integer lettercount = letterCount.get(word);
if(count == null) {
wordCount.put(word, 1);
}
else {
wordCount.put(word, count + 1);
}
}
for (String words : wordCount.keySet())
{System.out.println("Word : " + words + " has count :" +wordCount.get(words));
}
int first ,second;
first = second = Integer.MIN_VALUE;
String firstword ="";
String secondword="";
for(Map.Entry<String, Integer> entry : wordCount.entrySet())
{
int count = entry.getValue();
String word = entry.getKey();
if(count>first){
second = first;
secondword = firstword;
first = count;
firstword = word;
}
else if(count>second && count ==first){
second = count;
secondword = word;
}
}
System.out.println(firstword + "" + first);
System.out.println(secondword + " " + second);
for(i = 0; i < message.length(); i++){
char c = message.charAt(i);
if (c != ' ') {
int value = letterCount.getOrDefault((int) c, 0);
letterCount.put((int) c, value + 1);
}
}
for(int key : letterCount.keySet()) {
System.out.println((char) key + ": " + letterCount.get(key));
}
}
}
好的,所以从这个问题我了解到你需要检查一个字符串中的一对单词是否必须在整个字符串中计算。我看到你的代码,觉得它比要求的要复杂得多。请参阅以下代码段。
- 以space作为分隔符分割源字符串
- 连接相邻的字符串,用 space 分隔它们
- 在源字符串中搜索连接的字符串
- 如果没有找到,则添加到Map中,key为词对,value为1。
如果找到,从映射中获取单词对的值并递增并将其设置回去。
String message = "The Quick brown fox jumps over the lazy brown dog the quick";
String[] split = message.split(" ");
Map<String, Integer> map = new HashMap<>();
int count = 0;
for (int i = 0; i < split.length - 1; i++) {
String temp = split[i] + " " + split[i + 1];
temp = temp.toLowerCase();
if (message.toLowerCase().contains(temp)) {
if (map.containsKey(temp))
map.put(temp, map.get(temp) + 1);
else
map.put(temp, 1);
}
}
System.out.println(map);
这是完整的主方法代码,
如果有任何疑问,请告诉我。
public static void main(String[] args)
{
int index = 0; int tokenCount; int i =0;
Map<String,Integer> wordCount = new HashMap<String,Integer>();
Map<Integer,Integer> letterCount = new HashMap<Integer,Integer>();
String message="The Quick brown fox jumps over the lazy brown dog the quick";
StringTokenizer string = new StringTokenizer(message);
tokenCount = string.countTokens();
System.out.println("Number of tokens = " + tokenCount);
while (string.hasMoreTokens()) {
String word = string.nextToken().toLowerCase();
Integer count = wordCount.get(word);
Integer lettercount = letterCount.get(word);
System.out.println("Count : " + count);
if(count == null) {
wordCount.put(word, 1);
}
else {
wordCount.put(word, count + 1);
}
}
for (String words : wordCount.keySet())
{
System.out.println("Word : " + words + " has count :" +wordCount.get(words));
}
}
while (string.hasMoreTokens()) {
String word = string.nextToken().toLowerCase();
if (string.hasMoreTokens())
word += " "+string.nextToken().toLowerCase();
Integer count = wordCount.get(word);
Integer lettercount = letterCount.get(word);
if(count == null) {
wordCount.put(word, 1);
}
else {
wordCount.put(word, count + 1);
}
}
如何获取给定字符串上的单词对示例
快,快,棕色,棕色狐狸,狐狸跳 跳过去等...
那你数一数出现了多少次?
下面的代码只能统计单个单词。
import java.util.*;
import java.util.Map;
import java.util.HashMap;
public class Tokenizer
{
public static void main(String[] args)
{
int index = 0; int tokenCount; int i =0;
Map<String,Integer> wordCount = new HashMap<String,Integer>();
Map<Integer,Integer> letterCount = new HashMap<Integer,Integer>();
String message="The Quick brown fox jumps over the lazy brown dog the quick";
StringTokenizer string = new StringTokenizer(message);
tokenCount = string.countTokens();
System.out.println("Number of tokens = " + tokenCount);
while (string.hasMoreTokens()) {
String word = string.nextToken().toLowerCase();
Integer count = wordCount.get(word);
Integer lettercount = letterCount.get(word);
if(count == null) {
wordCount.put(word, 1);
}
else {
wordCount.put(word, count + 1);
}
}
for (String words : wordCount.keySet())
{System.out.println("Word : " + words + " has count :" +wordCount.get(words));
}
int first ,second;
first = second = Integer.MIN_VALUE;
String firstword ="";
String secondword="";
for(Map.Entry<String, Integer> entry : wordCount.entrySet())
{
int count = entry.getValue();
String word = entry.getKey();
if(count>first){
second = first;
secondword = firstword;
first = count;
firstword = word;
}
else if(count>second && count ==first){
second = count;
secondword = word;
}
}
System.out.println(firstword + "" + first);
System.out.println(secondword + " " + second);
for(i = 0; i < message.length(); i++){
char c = message.charAt(i);
if (c != ' ') {
int value = letterCount.getOrDefault((int) c, 0);
letterCount.put((int) c, value + 1);
}
}
for(int key : letterCount.keySet()) {
System.out.println((char) key + ": " + letterCount.get(key));
}
}
}
好的,所以从这个问题我了解到你需要检查一个字符串中的一对单词是否必须在整个字符串中计算。我看到你的代码,觉得它比要求的要复杂得多。请参阅以下代码段。
- 以space作为分隔符分割源字符串
- 连接相邻的字符串,用 space 分隔它们
- 在源字符串中搜索连接的字符串
- 如果没有找到,则添加到Map中,key为词对,value为1。
如果找到,从映射中获取单词对的值并递增并将其设置回去。
String message = "The Quick brown fox jumps over the lazy brown dog the quick"; String[] split = message.split(" "); Map<String, Integer> map = new HashMap<>(); int count = 0; for (int i = 0; i < split.length - 1; i++) { String temp = split[i] + " " + split[i + 1]; temp = temp.toLowerCase(); if (message.toLowerCase().contains(temp)) { if (map.containsKey(temp)) map.put(temp, map.get(temp) + 1); else map.put(temp, 1); } } System.out.println(map);
这是完整的主方法代码, 如果有任何疑问,请告诉我。
public static void main(String[] args)
{
int index = 0; int tokenCount; int i =0;
Map<String,Integer> wordCount = new HashMap<String,Integer>();
Map<Integer,Integer> letterCount = new HashMap<Integer,Integer>();
String message="The Quick brown fox jumps over the lazy brown dog the quick";
StringTokenizer string = new StringTokenizer(message);
tokenCount = string.countTokens();
System.out.println("Number of tokens = " + tokenCount);
while (string.hasMoreTokens()) {
String word = string.nextToken().toLowerCase();
Integer count = wordCount.get(word);
Integer lettercount = letterCount.get(word);
System.out.println("Count : " + count);
if(count == null) {
wordCount.put(word, 1);
}
else {
wordCount.put(word, count + 1);
}
}
for (String words : wordCount.keySet())
{
System.out.println("Word : " + words + " has count :" +wordCount.get(words));
}
}
while (string.hasMoreTokens()) {
String word = string.nextToken().toLowerCase();
if (string.hasMoreTokens())
word += " "+string.nextToken().toLowerCase();
Integer count = wordCount.get(word);
Integer lettercount = letterCount.get(word);
if(count == null) {
wordCount.put(word, 1);
}
else {
wordCount.put(word, count + 1);
}
}