-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextAnalytics.java
More file actions
128 lines (121 loc) · 4.18 KB
/
Copy pathTextAnalytics.java
File metadata and controls
128 lines (121 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* TextAnalytics.java
* program that takes in a book and counts the occurences for each word then print the top 5 and user inputed word.
* Homework 7
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TextAnalytics {
static Scanner scan = new Scanner(System.in);
static final ObjectHashMap hash = new ObjectHashMap();
/**
* funciton reads the file
* @param textFile name of the book
* The readFile function has the time complexity of O(n^2)
*/
public static void readFile(File textFile){
Scanner input = null;
String text = "";
String title = "";
try {
input = new Scanner(textFile);
} catch (FileNotFoundException e) {
System.out.println("File doesn't exist");
System.exit(0);
}
while (input.hasNextLine()){
text = input.nextLine();
text = text.toLowerCase();
if (text.contains("title: ")){
title = text.split(":")[1];
}
if (text.equals("*** start of the project gutenberg ebook" + title + " ***")){
while (!text.equals("*** end of the project gutenberg ebook" + title + " ***")){
text = input.nextLine();
String[] wordList = new String[text.length()];
text = text.toLowerCase();
wordList = text.split("\\s+");
setHashMap(wordList);
}
}
}
input.close();
}
/**
* function that sets up hash table
* @param text the line that will be evaluated
* setHashMap function has time complexity of O(n^2)
*/
public static void setHashMap(String[] wordList){
for (String word : wordList){
word = word.replaceAll("[^a-z]", "");
if (!word.equals("")){
if (hash.containsKey(word)){
Entry existingEntry = hash.getEntry(word);
existingEntry.value = (Integer)existingEntry.value + 1;
} else {
hash.put(word,1);
}
}
}
}
/**
* function that runs the program
* runProgram function has time complexity of O(n)
*/
public static void runProgram(){
Object choice = "";
while (!choice.equals("q")){
System.out.println("Type a word or type q to quit");
choice = scan.next();
if (choice.equals("q")){
System.out.println("Thanks for playing");
choice = "q";
} else {
if (hash.containsKey(choice)){
System.out.println("The word " + "'" + choice + "'" + " occurs " + hash.find(choice) + " times");
} else {
System.out.println("The word " + "'" + choice + "'" + " is not present");
}
}
}
}
/**
* function prints out the top 5 most used words in a book
* printTopFive function has time complexity of O(n^2)
*/
public static void printTopFive(){
Entry[] entryArr = hash.getEntries();
insertionSort(entryArr);
int cnt = 1;
System.out.println("--Top 5 Most Frequent Words--");
for (int i = entryArr.length-1; i > entryArr.length - 6; i--){
System.out.println(cnt + ".) " + "'" + entryArr[i].key + "'" + " " + hash.find(entryArr[i].key) + " uses.");
cnt++;
}
}
/**
* function sorts the array from least to greatest
* @param entryArr array of Entry
* sortArr function has time complexity of O(n^2)
*/
private static void insertionSort(Entry[] entryArr){
for(int i = 1; i < entryArr.length; i++){
Entry currEntry = entryArr[i];
int j = i;
while (j > 0 && (Integer)currEntry.value < (Integer)entryArr[j-1].value){
entryArr[j] = entryArr[j-1];
j--;
}
entryArr[j] = currEntry;
}
}
public static void main(String[] args){
File textFile = new File(args[0]);
readFile(textFile);
printTopFive();
runProgram();
scan.close();
}
}