-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.java
64 lines (57 loc) · 2.39 KB
/
App.java
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
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Scanner;
/**
* This class serves as the applications entry point.
* It fetches a text from a given URL, processes the text into words,
* and adds them into a hash set. It then prints various satistice about the
* hash set
*/
public class App {
private static final String SPECIAL_CHARACTERS = "\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\+\\`\\-\\=\\{\\}\\[\\]\\|\\\\\\:\\\"\\;\\'\\<\\>\\,\\.\\/\\“\\”\\™";
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
try {
HashStringSet hashSet = new HashStringSet(9973); // Initialize with a prime number as number of buckets
// Fetch the book
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://www.gutenberg.org/files/4300/4300-h/4300-h.htm")) // Replace link
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// Process the text
Scanner scanner = new Scanner(response.body());
while (scanner.hasNext()) {
String word = scanner.next();
word = normalize(word);
hashSet.add(word);
}
// Print the results
System.out.println("Number of words: " + hashSet.size());
System.out.println("Number of buckets: " + hashSet.numBuckets());
System.out.println("Number of used buckets: " + hashSet.numUsedBuckets());
System.out.println("Load factor: " + hashSet.loadFactor());
System.out.println("Memory efficiency factor: " + hashSet.memoryEfficiencyFactor());
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
e.printStackTrace();
}
}
/**
* Normalizes a word by converting it to lower case and removing special
* characters
*
* @param word The word to normalize
* @return The normalized word
*/
private static String normalize(String word) {
word = word.toLowerCase();
word = word.replaceAll("[" + SPECIAL_CHARACTERS + "]", "");
return word;
}
}