Java-词频

Java-词频,java,eclipse,word-frequency,Java,Eclipse,Word Frequency,我在Eclipse中创建了一个Java程序。程序计算每个单词的频率。例如,如果用户输入“我去了商店”,程序将产生输出“12”,即1个单词的长度为1(“I”)1个单词的长度为2(“to”)1个单词的长度为3(“the”)和2个单词的长度为4(“去了”,“商店”) 这些是我得到的结果。我不希望显示带有0的输出。如何隐藏这些内容,并仅显示1,2,3,4,5的结果 The cat sat on the mat words[1]=0 words[2]=1 words[3]=5 words[4]=0 wor

我在Eclipse中创建了一个Java程序。程序计算每个单词的频率。例如,如果用户输入“我去了商店”,程序将产生输出“12”,即1个单词的长度为1(“I”)1个单词的长度为2(“to”)1个单词的长度为3(“the”)和2个单词的长度为4(“去了”,“商店”)

这些是我得到的结果。我不希望显示带有0的输出。如何隐藏这些内容,并仅显示1,2,3,4,5的结果

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++) {




    }}}}}
猫坐在垫子上 字[1]=0 字[2]=1 字[3]=5 字[4]=0 字[5]=0 导入java.util.Scanner; 导入java.io.*; 公共级马林森利亚姆8 { 公共静态void main(字符串[]args)引发异常 { 扫描仪扫描=新扫描仪(新文件(“body.txt”); while(scan.hasNext()) { 字符串s; s=scan.nextLine(); 字符串输入=s; String strippedInput=input.replaceAll(“\\W”和“); System.out.println(“+strippedInput”); String[]strings=strippedInput.split(“”); int[]计数=新的int[6]; int-total=0; 字符串文本=空; for(字符串str:strings) if(str.length()对于(int j=1;j只需在打印前添加一个复选框

for (int i = 1; i < counts.length; i++) {
    if (counts[i] > 0) { //filter out 0-count lengths
        System.out.println("words["+ i + "]="+counts[i]);
    }
for(int i=1;i0){//过滤掉0计数长度
System.out.println(“单词[“+i+”]=“+counts[i]);
}

只需在打印前添加支票即可

for (int i = 1; i < counts.length; i++) {
    if (counts[i] > 0) { //filter out 0-count lengths
        System.out.println("words["+ i + "]="+counts[i]);
    }
for(int i=1;i0){//过滤掉0计数长度
System.out.println(“单词[“+i+”]=“+counts[i]);
}

添加一条if语句,检查长度为“i”的字数是否等于0

如果这是真的,就不要表现出来,如果不是,就表现出来

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


B我先回答。我们的答案都有效。

添加一个if语句,检查长度为“i”的字数是否等于0

如果这是真的,就不要表现出来,如果不是,就表现出来

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


B比我快。我们的答案都有效。

我会使用Java8流式API

参见我的示例:

// 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);
    }
}
它输出:

1 1 1 2

我会使用Java8流式API

参见我的示例:

// 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);
    }
}
它输出:

1 1 1 2

我知道您要求使用Java,但只是为了比较,下面是我在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

我知道您要求使用Java,但只是为了比较,下面是我在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

如果你不加0或其他东西,你怎么知道频率对应于什么长度的单词?如果你不加0或其他东西,你怎么知道频率对应于什么长度的单词?我误读了你的代码,那完全是我的错误!我以为你写了
单词[I]>0;
我很抱歉。我误读了你的代码,这完全是我的错误!我以为你写了
字[I]>0;
我的道歉。对我的java8方法简单。但是更短。在我的java眼中有点神秘。但是绝对值得一提!+1对我的java8方法简单。但是更短。在我的java眼中有点神秘。但是绝对值得一提!+1如果你投反对票,请留下评论。我也投了反对票,so我怀疑有人刚刚通过并否决了这篇文章的所有内容。@b事实上,所有答案都显示了+1/-1的平衡。该问题目前为+2/-2.)哦,对了。谢谢你提醒我,我最近得到了这项特权:)如果你投了反对票,请留下评论。我也投了反对票,所以我怀疑有人刚刚通过了这篇文章,并且对所有内容都投了反对票。@b事实上,所有答案都显示了+1/-1的平衡。这个问题目前是+2/-2。:)哦,对了。谢谢你提醒我,我最近这项特权:)