计算java中句子中每个单词的频率

我正在编写一个非常基本的java程序来计算句子中每个单词的频率,到目前为止我设法做了这么多

import java.io.*; class Linked { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Enter the sentence"); String st = br.readLine(); st = st + " "; int a = lengthx(st); String arr[] = new String[a]; int p = 0; int c = 0; for (int j = 0; j < st.length(); j++) { if (st.charAt(j) == ' ') { arr[p++] = st.substring(c,j); c = j + 1; } } } static int lengthx(String a) { int p = 0; for (int j = 0; j < a.length(); j++) { if (a.charAt(j) == ' ') { p++; } } return p; } } 

我已经提取了每个字符串并将其存储在一个数组中,现在的问题实际上是如何计算每个“单词”重复的实例的数量以及如何显示以便重复的单词不会多次显示,你能帮助我吗?一个?

使用带有单词作为键的映射并计为值,就像这样

  Map map = new HashMap<>(); for (String w : words) { Integer n = map.get(w); n = (n == null) ? 1 : ++n; map.put(w, n); } 

如果你不允许使用java.util,那么你可以使用一些排序算法对arr进行排序并执行此操作

  String[] words = new String[arr.length]; int[] counts = new int[arr.length]; words[0] = words[0]; counts[0] = 1; for (int i = 1, j = 0; i < arr.length; i++) { if (words[j].equals(arr[i])) { counts[j]++; } else { j++; words[j] = arr[i]; counts[j] = 1; } } 

自Java 8以来,ConcurrentHashMap的一个有趣的解决方案

  ConcurrentMap m = new ConcurrentHashMap<>(); m.compute("x", (k, v) -> v == null ? 1 : v + 1); 

在Java 8中,您可以用两个简单的行来编写它! 此外,您还可以利用并行计算。

这是最美妙的方式:

 Stream stream = Stream.of(text.toLowerCase().split("\\W+")).parallel(); Map wordFreq = stream .collect(Collectors.groupingBy(String::toString,Collectors.counting())); 

尝试这个

 public class Main { public static void main(String[] args) { String text = "the quick brown fox jumps fox fox over the lazy dog brown"; String[] keys = text.split(" "); String[] uniqueKeys; int count = 0; System.out.println(text); uniqueKeys = getUniqueKeys(keys); for(String key: uniqueKeys) { if(null == key) { break; } for(String s : keys) { if(key.equals(s)) { count++; } } System.out.println("Count of ["+key+"] is : "+count); count=0; } } private static String[] getUniqueKeys(String[] keys) { String[] uniqueKeys = new String[keys.length]; uniqueKeys[0] = keys[0]; int uniqueKeyIndex = 1; boolean keyAlreadyExists = false; for(int i=1; i 

输出:

 the quick brown fox jumps fox fox over the lazy dog brown Count of [the] is : 2 Count of [quick] is : 1 Count of [brown] is : 2 Count of [fox] is : 3 Count of [jumps] is : 1 Count of [over] is : 1 Count of [lazy] is : 1 Count of [dog] is : 1 
 import java.util.*; public class WordCounter { public static void main(String[] args) { String s = "this is a this is this a this yes this is a this what it may be i do not care about this"; String a[] = s.split(" "); Map words = new HashMap<>(); for (String str : a) { if (words.containsKey(str)) { words.put(str, 1 + words.get(str)); } else { words.put(str, 1); } } System.out.println(words); } } 

输出:{a = 3,be = 1,may = 1,yes = 1,this = 7,about = 1,i = 1,is = 3,it = 1,do = 1,not = 1,what = 1 ,care = 1}

从Java 10,您可以使用以下内容:

 import java.util.Arrays; import java.util.stream.Collectors; public class StringFrequencyMap { public static void main(String... args){ String[] wordArray = {"One", "One", "Two","Three", "Two", "two"}; var freq = Arrays.stream(wordArray) .collect(Collectors.groupingBy(x -> x, Collectors.counting())); System.out.println(freq); } } 

输出:

 {One=2, two=1, Two=2, Three=1} 

你可以试试这个

 public static void frequency(String s) { String trimmed = s.trim().replaceAll(" +", " "); String[] a = trimmed.split(" "); ArrayList p = new ArrayList<>(); for (int i = 0; i < a.length; i++) { if (p.contains(i)) { continue; } int d = 1; for (int j = i+1; j < a.length; j++) { if (a[i].equals(a[j])) { d += 1; p.add(j); } } System.out.println("Count of "+a[i]+" is:"+d); } } 
 package naresh.java; import java.util.HashMap; import java.util.HashSet; import java.util.Set; public class StringWordDuplicates { static void duplicate(String inputString){ HashMap wordCount = new HashMap(); String[] words = inputString.split(" "); for(String word : words){ if(wordCount.containsKey(word)){ wordCount.put(word, wordCount.get(word)+1); } else{ wordCount.put(word, 1); } } //Extracting of all keys of word count Set wordsInString = wordCount.keySet(); for(String word : wordsInString){ if(wordCount.get(word)>1){ System.out.println(word+":"+wordCount.get(word)); } } } public static void main(String args[]){ duplicate("I am Java Programmer and IT Server Programmer with Java as Best Java lover"); } } 
 class find { public static void main(String nm,String w) { int l,i; int c=0; l=nm.length();String b=""; for(i=0;i 
 public class wordFrequency { private static Scanner scn; public static void countwords(String sent) { sent = sent.toLowerCase().replaceAll("[^az ]", ""); ArrayList arr = new ArrayList(); String[] sentarr = sent.split(" "); Map a = new HashMap(); for (String word : sentarr) { arr.add(word); } for (String word : arr) { int count = Collections.frequency(arr, word); a.put(word, count); } for (String key : a.keySet()) { System.out.println(key + " = " + a.get(key)); } } public static void main(String[] args) { scn = new Scanner(System.in); System.out.println("Enter sentence:"); String inp = scn.nextLine(); countwords(inp); } } 

确定文件中单词的频率。

 File f = new File(fileName); Scanner s = new Scanner(f); Map counts = new Map(); while( s.hasNext() ){ String word = s.next(); if( !counts.containsKey( word ) ) counts.put( word, 1 ); else counts.put( word, counts.get(word) + 1 ); 

}

以下程序查找频率,对其进行相应的排序并打印出来。

以下是按频率分组的输出:

 0-10: The 2 Is 4 11-20: Have 13 Done 15 

这是我的计划:

 package com.company; import java.io.*; import java.util.*; import java.lang.*; /** * Created by ayush on 12/3/17. */ public class Linked { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Enter the sentence"); String st = br.readLine(); st=st.trim(); st = st + " "; int count = lengthx(st); System.out.println(count); String arr[] = new String[count]; int p = 0; int c = 0; for (int i = 0; i < st.length(); i++) { if (st.charAt(i) == ' ') { arr[p] = st.substring(c,i); System.out.println(arr[p]); c = i + 1; p++; } } Map map = new HashMap<>(); for (String w : arr) { Integer n = map.get(w); n = (n == null) ? 1 : ++n; map.put(w, n); } for (String key : map.keySet()) { System.out.println(key + " = " + map.get(key)); } Set> entries = map.entrySet(); Comparator> valueComparator = new Comparator>() { @Override public int compare(Map.Entry e1, Map.Entry e2) { Integer v1 = e1.getValue(); Integer v2 = e2.getValue(); return v1.compareTo(v2); } }; List> listOfEntries = new ArrayList>(entries); Collections.sort(listOfEntries, valueComparator); LinkedHashMap sortedByValue = new LinkedHashMap(listOfEntries.size()); for(Map.Entry entry : listOfEntries){ sortedByValue.put(entry.getKey(), entry.getValue()); } for(Map.Entry entry : listOfEntries){ sortedByValue.put(entry.getKey(), entry.getValue()); } System.out.println("HashMap after sorting entries by values "); Set> entrySetSortedByValue = sortedByValue.entrySet(); for(Map.Entry mapping : entrySetSortedByValue){ System.out.println(mapping.getKey() + " ==> " + mapping.getValue()); } } static int lengthx(String a) { int count = 0; for (int j = 0; j < a.length(); j++) { if (a.charAt(j) == ' ') { count++; } } return count; } } 
 import java.io.*; class Linked { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Enter the sentence"); String st = br.readLine(); st = st + " "; int a = lengthx(st); String arr[] = new String[a]; int p = 0; int c = 0; for (int j = 0; j < st.length(); j++) { if (st.charAt(j) == ' ') { arr[p++] = st.substring(c,j); c = j + 1; } } } static int lengthx(String a) { int p = 0; for (int j = 0; j < a.length(); j++) { if (a.charAt(j) == ' ') { p++; } } return p; } } 

只需使用Java 8 Stream collectors groupby函数:

  import java.util.function.Function; import java.util.stream.Collectors; static String[] COUNTRY_NAMES = { "China", "Australia", "India", "USA", "USSR", "UK", "China", "France", "Poland", "Austria", "India", "USA", "Egypt", "China" }; Map result = Stream.of(COUNTRY_NAMES).collect( Collectors.groupingBy(Function.identity(), Collectors.counting())); 
 public class WordFrequencyProblem { public static void main(String args[]){ String s="the quick brown fox jumps fox fox over the lazy dog brown"; String alreadyProcessedWords=""; boolean isCount=false; String[] splitWord = s.split("\\s|\\."); for(int i=0;i 
 public class TestSplit { public static void main(String[] args) { String input="Find the repeated word which is repeated in this string"; List output= (List) Arrays.asList(input.split(" ")); for(String str: output) { int occurrences = Collections.frequency(output, str); System.out.println("Occurence of " + str+ " is "+occurrences); } System.out.println(output); } }