@@ -836,15 +836,16 @@ public class HistoArray {
836836
837837 <program xml : id =" python-input-file2" interactive =" activecode" language =" python" >
838838 <code >
839- def main():
839+ def main():
840+ """ Program to read a file and count the frequency of words in the file. """
840841 data = open('alice30.txt')
841- wordList = data.read().split()
842+ wordList = data.read().split() # split the file into a list of words
842843 count = {}
843- for w in wordList:
844+ for w in wordList: # iterate over the list of words
844845 w = w.lower()
845846 count[w] = count.get(w,0) + 1
846- keyList = sorted(count.keys())
847- for k in keyList:
847+ keyList = sorted(count.keys()) # sort the keys in alphabetical order
848+ for k in keyList: # iterate over the sorted list of keys
848849 print("%-20s occurred %4d times" % (k, count[k]))
849850main()
850851 </code > <tests > </tests >
@@ -883,36 +884,39 @@ main()
883884
884885 <program xml : id =" java-use-input-file" interactive =" activecode" language =" java" datafile =" alice30.txt" >
885886 <code >
886- import java.util.Scanner;
887- import java.util.ArrayList;
888- import java.io.File;
889- import java.io.IOException;
890- import java.util.TreeMap;
887+ import java.util.Scanner;
888+ import java.util.ArrayList; // import the ArrayList class to use dynamic arrays
889+ import java.io.File;
890+ import java.io.IOException;
891+ import java.util.TreeMap; // import the TreeMap class to use a map for counting word frequencies
892+ /**
893+ * Program to read a file and count the frequency of words in the file.
894+ */
891895public class HistoMap {
892896 public static void main(String[] args) {
893897 Scanner data = null;
894- TreeMap< String,Integer> count;
898+ TreeMap< String,Integer> count; // create a TreeMap to hold word counts, mapping each word (String) to its frequency (Integer)
895899 Integer idx;
896900 String word;
897901 Integer wordCount;
898- try {
902+ try { // Try to open the data file and handle any potential IOExceptions
899903 data = new Scanner(new File("alice30.txt"));
900904 }
901905 catch ( IOException e) {
902906 System.out.println("Unable to open data file");
903907 e.printStackTrace();
904908 System.exit(0);
905909 }
906- count = new TreeMap< String,Integer> ();
910+ count = new TreeMap< String,Integer> (); // create a TreeMap to hold word counts
907911 while(data.hasNext()) {
908- word = data.next().toLowerCase();
909- wordCount = count.get(word);
912+ word = data.next().toLowerCase(); // read each word from the file, convert it to lowercase
913+ wordCount = count.get(word); // get the current count for the word from the TreeMap
910914 if (wordCount == null) {
911915 wordCount = 0;
912916 }
913- count.put(word,++wordCount);
917+ count.put(word,++wordCount); // increment the count for the word and put it back into the TreeMap
914918 }
915- for(String i : count.keySet()) {
919+ for(String i : count.keySet()) { // iterate over the keys (words) in the TreeMap
916920 System.out.printf("%-20s occurred %5d times\n", i, count.get(i) );
917921 }
918922 }
0 commit comments