Count Frequency of Words of Different Lengths
【Question】
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').
I've created this program to read a string entered by the user but I want to adjust the code to read each line of a text file. Any help would be great.
import java.util.Scanner;
public class WordLengthFrequency
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
while (true)
{
System.out.println("Enter text:");
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;
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++){
StringBuilder sb = new StringBuilder(i).append(i + "letter words:");
for (int j = 1; j <= counts[i]; j++) {
sb.append('*');
System.out.println(i + "letter words:" + counts[i]);
System.out.println(sb);
System.out.println(("mean lenght:") + ((double) total / strings.length));
}
}
}
}
}
【Answer】
It’s a hassle to perform the count in Java. You can write a simple script in Java and integrate it into the Java application. About the integration of SPL script and a Java application, see How to Call an SPL Script in Java.
A |
|
1 |
=file("d:\\data.txt").read() |
2 |
=A1.words() |
3 |
=A2.groups(len(~):length;count(~):count) |
A1: Read in the text file as strings.
A2: Split each string into a sequence of English words.
A3: Group A2 by the length of words and count the words in each group.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProcSPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL