Warning: Creating default object from empty value in /home/patricknevindwyer/digilutionary.com/wp-includes/functions.php on line 292
digilutionary.com
Patrick Dwyer

Java

Java based programming and research.

Frequency :: Java

in News, Java, Comparative Programming by patrick


Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/patricknevindwyer/digilutionary.com/wp-includes/functions-formatting.php on line 76

This program is part of the Comparative Programming :: Frequency Analysis set of examples.

Our Java example, while longer than most of the other frequency analysis programs, is fairly straight forward in it’s approach. To keep track of the number of character occurances in our text, we want to create a map of characters to numbers, which we’ll increment every time we see a valid character. In lines 11 through 19 we create our Hashtable and initialize it with the letters ‘a’ through ‘z’, associating with each the value ‘0.0′.

We’ll need to reference the letters ‘a’ through ‘z’ twice in our program; once to create our Hashtable, and again to print the results. For this reason we create a string of letters in alphabetical order on line 12. We can iterate through the string with a simple for loop (lines 17 and 44) to access our character counts.

With our Hashtable ready, we can open and read our file. On line 22 we create a BufferedReader to access the contents of our text file line by line. Our while loop (line 26) extracts each line from the file, placing it’s value in the line variable. This while loop will continue until our input file’s readLine method returns null, which signifies the end of the file.

Once we have a line of text from our file, we need to break it down into characters, the fundamental unit of text for our analysis. To do this we use a for loop, starting on line 29, that counts from 0 up to the length of our line. Using the index of our for loop, we can sequentially extract the characters from our line of text (line 30) using the String class’ substring method.

We don’t want to count any random characters we come across; we’re only interested in letters. Line 33 of our program contains a simple Regular Expression to test our extracted character. The pattern [a-zA-Z] matches any lower or upper case character, but nothing else. If our character matches our regular expression, we want to include it in our frequency analysis, so translate the character to lower case (in case it was an upper case character), and increment our character frequency in the Hashtable (line 37). We increment this value by first retrieving the old value using our current character

freq.get(c)

adding 1

freq.get(c) + 1.0

and then inserting our incremented value back into the Hashtable

freq.put(c, freq.get(c) + 1.0)

The last step in our for loop keeps track of the total number of valid characters so far encountered.

Once we’re done with our file, having read each line and extracted each valid character, we need to calculate our character frequencies and print them out (lines 44 to 48).

Using the string of letters from ‘a’ to ‘z’ that we defined earlier, we can get the key and value for each of the characters in our Hashtable. The key is the character itself, a letter from ‘a’ to ‘z’, while the value is how often the character occured in the text file we analyzed, the number we computed in the course of analyzing the line and characters of the file.

With the key and value in hand, it is a simple matter to determine the relative frequency of our character in the file (line 46). Printing the frequency of our character takes a little bit more work. We’d like the output of our program to look like:

a: 8.00
b: 1.51
c: 2.42
d: 3.90
e: 12.84
f: 2.20
.
.
.
t: 9.42
u: 3.02
v: 0.94
w: 2.28
x: 0.15
y: 2.16
z: 0.04

A first attempt at printing our results could be:

System.out.println(key + ": " + perc);

Which correctly prints a letter, followed by a colon, a space, and our frequency, but our frequency is a significantly long number:

a: 7.9990618682967325
b: 1.5108759990916503
c: 2.4194118807679277
d: 3.9029257051809445
.
.
.

We want to limit our frequency to two decimal places. Thankfully there is a simple method in the String class for printing numbers and strings in a controlled manner. We can pass C-Style string formats to the format method to create a better output string (line 47). The %2.2f instructs the format method that our floating point value should be printed with at most two leading digits, and two trailing digits.

While our frequency analysis is done, it’s important to note that our entire program is wrapped in a try-catch block; a construct of the java language intended for exception processing. When working with any type of Input/Output, be it files, networks, or peripherals, there is the possibility that communication will break, or somehow be disrupted. Many Java classes leave it up to the programmer to handle these situations. In our case opening and reading from a file can cause IOExceptions that we need to account for. In our program we don’t attempt to recover from the error, opting instead to print out a program trace of where the error occured (line 53).

Java
01import java.util.*;
02import java.io.*;
03
04public class freq {
05
06 public static void main(String[] args) {
07
08 try {
09
10 // create our map of lowercase letters to integers
11 Hashtable<String, Double> freq = new Hashtable<String, Double>();
12 String set = "abcdefghijklmnopqrstuvwxyz";
13
14 int count = 0;
15
16 // initialize our character counts to 0
17 for ( int i = 0; i < set.length(); i++) {
18 freq.put(set.substring(i, i+1), 0.0);
19 }
20
21 // open our file
22 BufferedReader in = new BufferedReader(new FileReader(args[0]));
23 String line;
24
25 // read in each line from the file
26 while ( (line = in.readLine()) != null) {
27
28 // extract each character from the line
29 for (int i = 0; i < line.length(); i++) {
30 String c = line.substring(i, i+1);
31
32 // try and match our character to a lower or upper case letter
33 if (c.matches("[a-zA-Z]")) {
34
35 // increment the count of our character
36 c = c.toLowerCase();
37 freq.put(c, freq.get(c) + 1.0);
38 count++;
39 }
40 }
41 }
42
43 // calculate the frequency of each of our characters, printing the result
44 for (int i = 0; i < set.length(); i++) {
45 String key = set.substring(i, i + 1);
46 double perc = freq.get(key) / count * 100.0;
47 System.out.println(key + ": " + String.format("%2.2f", perc));
48 }
49
50 } catch (IOException ioe) {
51
52 // catch any problems we have reading the file from disk
53 ioe.printStackTrace();
54 }
55 }
56}
[+-] Toggle Line Numbers

Program Source: freq.java
Text Source: republic.txt
This text was acquired from Project Gutenberg, and
is distributed as per the license at the beginning of the text.

Compiling the example

From the command line:

javac freq.java

Running the example

java freq republic.txt

Echo :: Java

in News, Java, Comparative Programming by patrick


Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/patricknevindwyer/digilutionary.com/wp-includes/functions-formatting.php on line 76

This program is part of the Comparative Programming :: Echo set of examples.

This single Java class is about as bare-bones as a Java program can get. We utilize a Java 5 foreach loop to retrieve each program argument and print it back to System.out, finishing with a new line.

Java

public class echo {

public static void main(String[] args) {

for (String arg : args) {
System.out.print(arg + " ");
}
System.out.println("");
}

}

Program Source: echo.java

Compiling the example

From the command line:

javac echo.java

Running the example

java echo Hello World

Wobble

in Java, Real Time Video by patrick


Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/patricknevindwyer/digilutionary.com/wp-includes/functions-formatting.php on line 76


Wobble
Click to enlarge
Sample output from the Wobble program
Wobble is a distortion of visible reality. A live video stream is warped and compressed, creating a sea-sick inducing ripple with the video. While appearing visually complicated, the actual effect is quite easy to create. The incoming video image is broken into three rows and three columns, like a tic-tac-toe board. The horizontal and vertical scale of each piece is controlled by a sin or cosine function, so that the 9 pieces shrink and grow together. The result feels like a much more complex ripple across the whole video image.

The Wobble program is pure Java, utilizing the QTJava quicktime capture extensions for video acquisition. In the program archive you will find:

  • Wobble source code
  • QTJava.zip - Quicktime for Java
  • VBP.jar - Interface for QTJava
  • wobble.sh - Used to run the program


On Mac OSX (10.2 or higher), and some supported Linux system, you can use the wobble.sh shell script to compile and run the program. For windows systems, or if for some reason the shell script fails to work, the following commands will compile and run the program.

javac -classpath .:VBP.JAR wobble.java
java -classpath .:VBP.JAR wobble

SlideGame

in Java, Real Time Video by patrick


Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/patricknevindwyer/digilutionary.com/wp-includes/functions-formatting.php on line 76


SlideGame
Click to enlarge
Sample output from the SlideGame program
SlideGame is a computer controlled version of the classic numbered tile game. In the numbered tile game a 4×4 square containing 15 tiles (one missing tile) is the game board. The goal of the game is to use the empty space on the board to shift around the tiles, trying to rearrange them into their original order. SlideGame uses a much larger game board (20 by 40), with a single tile missing. The computer controls the movement of the tiles, and the only goal is to continuously move tiles around. In SlideGame the tiles aren’t numbered, rather they represent chunks of live video. When the game starts the video is in the correct order. As a tile is moved, the chunk of video moves along with it. The result is a jumbled set of tiles of live video.

The SlideGame program is pure Java, utilizing the QTJava quicktime capture extensions for video acquisition. In the program archive you will find:

  • SlideGame source code
  • QTJava.zip - Quicktime for Java
  • VBP.jar - Interface for QTJava
  • slideGame.sh - Used to run the program


SlideGame

slidegame.zip
On Mac OSX (10.2 or higher), and some supported Linux system, you can use the slideGame.sh shell script to compile and run the program. For windows systems, or if for some reason the shell script fails to work, the following commands will compile and run the program.

javac -classpath .:VBP.JAR slideGame.java
java -classpath .:VBP.JAR slideGame

Pointillism

in Java, Real Time Video by patrick


Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/patricknevindwyer/digilutionary.com/wp-includes/functions-formatting.php on line 76


Pointillism
Click to enlarge
Sample output from the Pointillism program
Pointillism tries to recreate the painting technique of the same name, as applied to displaying a live video stream. For each frame of video a series of points are choosen; for each of these points a color is extracted, and a small point drawn to the screen with this color. The result has interesting effects when the image is perfectly still, and when the contents of the video are moving. As objects within view of the camera come to rest, the pointilistic representation becomes clear; when objects are moving the screen becomes jumbled, with faster objects becoming lest distinct, slower more distinct.

The Pointillism program is pure Java, utilizing the QTJava quicktime capture extensions for video acquisition. In the program archive you will find:

  • Pointillism source code
  • QTJava.zip - Quicktime for Java
  • VBP.jar - Interface for QTJava
  • pointilism.sh - Used to run the program


Pointillism

pointillism.zip
On Mac OSX (10.2 or higher), and some supported Linux system, you can use the pointilism.sh shell script to compile and run the program. For windows systems, or if for some reason the shell script fails to work, the following commands will compile and run the program.

javac -classpath .:VBP.JAR pointilism.java
java -classpath .:VBP.JAR pointilism