/** * A TextAnalyser object takes text either as a String or from a file, * and it builds up a count of the occurrences of each letter in that text. * It provides various methods for interrogating the data. * Letters are regarded as case-insensitive (so e.g. b = B). * * @author Lyndon While et al. * @version 1.2 */ import java.util.*; public class TextAnalyser { private int letters; // the total number of letters analysed private int[] frequencies; // the number of occurrences of each letter of the alphabet (case-insensitive) // initialise the instance variables public TextAnalyser() { frequencies = new int[26]; } // initialise the instance variables and analyse the String initial public TextAnalyser(String initial) { this(); analyse(initial); } // analyse the String s // add to each count in frequencies the number of occurrences of each letter in s, and update letters public void analyse (String s) { for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (Character.isLetter(c)) { frequencies[Character.toLowerCase(c) - 'a']++; letters++; } } } // analyse the contents of the file f // read in the file as a FileIO object, then analyse the contents public void analyseFile (String f) { for (String l : (new FileIO(f)).lines) analyse(l); } // return the number of letters that have been seen since the last reset public int lettersAnalysed() { return letters; } // return the number of occurrences of letter since the last reset public int frequency (char letter) { return frequencies[Character.toLowerCase(letter) - 'a']; } // clear the instance variables public void reset() { letters = 0; for (int i = 0; i < 26; i++) frequencies[i] = 0; } // return the most common letter seen since the last reset // if no letters have been analysed, return '?' public char mostFrequent () { if (letters == 0) return '?'; else { char best = 'a'; for (char c = 'b'; c <= 'z'; c++) if (frequency(c) > frequency(best)) best = c; return best; } } // use Oblongs to draw a histogram of the frequencies // create an Oblong for each bar of the histogram, and arrange them appropriately on the screen public void histogram() { int s = 10; for (int k = 0; k < 26; k++) new Oblong(0, 5 + k * (s + 1), frequency((char) ('a' + k)), s, "red"); } // prize-winner 2018: Ramon Cuevas public void histogramRC() { Random rand = new Random(); String[] listOfColors = new String[6]; listOfColors[0] = "red"; listOfColors[1] = "blue"; listOfColors[2] = "black"; listOfColors[3] = "green"; listOfColors[4] = "yellow"; listOfColors[5] = "magenta"; for(int i = 0; i < 26; i++){ if(frequencies[i] > 0){ Oblong letterOblong = new Oblong(i*10 , 0 , 10 , frequency((char)(i + 97)), listOfColors[rand.nextInt(6)]); letterOblong.makeVisible(); letterOblong.slowMoveVertical(20); letterOblong.slowMoveHorizontal(60); letterOblong.slowMoveVertical(20); } else{ Oblong emptySpace = new Oblong(i*10 , 0 , 10 , 0 , "white"); emptySpace.makeVisible(); } } } // prize-winner 2018: James Porter public void histogramJP() { ArrayList Bars = new ArrayList(); String colour = "black"; new Oblong(15, 185, 301, 2, "black"); for(int i = 0; i < 26; i++){ if(i % 5 == 0){ colour ="red"; } if(i % 5 == 1){ colour = "magenta"; } if(i % 5 == 2){ colour = "blue"; } if(i % 5 == 3){ colour = "green"; } if(i % 5 == 4){ colour = "yellow"; } if(frequencies[i] >= 1){ Bars.add(new Oblong(15 + i * 11, 85 - frequencies[i], 10, frequencies[i], "black")); Bars.get(i).slowMoveVertical(100); Bars.get(i).changeColor(colour); } else{ Bars.add(new Oblong(15 + i * 11, 185 - frequencies[i], 10, frequencies[i], colour)); } } } }