简体   繁体   中英

Java - Word Frequency

I've created a Java program in Eclipse. The program counts the frequency of each word. For example if the user entered 'I went to the shop' the program would produce the output '1 1 1 2' that is 1 word of length 1 ('I') 1 word of length 2 ('to') 1 word of length 3 ('the') and 2 words of length 4 ('went' , 'shop').

These are the results I'm getting. I don't want the output with a 0 to be shown. How can I hide these and only have the results with 1,2,3,4,5 shown.

The cat sat on the mat
words[1]=0
words[2]=1
words[3]=5
words[4]=0
words[5]=0


  import java.util.Scanner;
 import java.io.*;

 public class mallinson_Liam_8
{

 public static void main(String[] args) throws Exception
 {

    Scanner scan = new Scanner(new File("body.txt"));

    while(scan.hasNext())
    {

        String s;
        s = scan.nextLine();
        String input = s;
        String strippedInput = input.replaceAll("\\W", " ");

        System.out.println("" + strippedInput);

        String[] strings = strippedInput.split(" ");
        int[] counts = new int[6];
        int total = 0;
        String text = null;

            for (String str : strings)
                if (str.length() < counts.length)
                    counts[str.length()] += 1;
            for (String s1 : strings)
                total += s1.length();   

            for (int i = 1; i < counts.length; i++){  
                System.out.println("words["+ i + "]="+counts[i]);
        StringBuilder sb = new StringBuilder(i).append(i + " letter words: ");
            for (int j = 1; j <= counts[i]; j++) {




    }}}}}

I know you asked for Java, but just for comparison, here is how I'd do it in Scala:

val s = "I went to the shop"
val sizes = s.split("\\W+").groupBy(_.length).mapValues(_.size)
// sizes = Map(2 -> 1, 4 -> 2, 1 -> 1, 3 -> 1)

val sortedSizes = sizes.toSeq.sorted.map(_._2)
// sortedSizes = ArrayBuffer(1, 1, 1, 2)

println(sortedSizes.mkString(" "))
// outputs: 1 1 1 2

Simply add a check before you print...

for (int i = 1; i < counts.length; i++) {
    if (counts[i] > 0) { //filter out 0-count lengths
        System.out.println("words["+ i + "]="+counts[i]);
    }

Add an if-statement that checks if the number of words of length 'i' is equal to 0.

If that is true, don't show it, if it is not, show it.

for (int i =0; i < counts.length; i++) {
 if (counts[i] != 0) {
  System.out.println("words[" + i + "]="+counts[i]); 
 }
}

Edit:

bbill beat me to it. Our answers both work.

I'd use the Java8 streaming API.

See my example:

// import java.nio.file.*;
import java.util.*;
import java.util.stream.Collectors;

public class CharacterCount {
    public static void main(String[] args) {

        // define input
        String input = "I went to the shop";
        // String input = new String(Files.readAllBytes(Paths.get("body.txt")));

        // calculate output
        String output =

                // split input by whitespaces and other non-word-characters
                Arrays.stream(input.split("\\W+"))

                // group words by length of word
                .collect(Collectors.groupingBy(String::length))

                // iterate over each group of words
                .values().stream()

                // count the words for this group
                .map(List::size)

                // join all values into one, space separated string
                .map(Object::toString).collect(Collectors.joining(" "));

        // print output to console
        System.out.println(output);
    }
}

It outputs:

1 1 1 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM