-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
115 lines (85 loc) · 3.37 KB
/
Main.java
File metadata and controls
115 lines (85 loc) · 3.37 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
package com.company;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class Main {
private static String testFileLocation = "C:\\Users\\gbagh\\OneDrive\\Desktop\\ML\\TestFile.txt";
private static String outputFileLocation = "c:\\Users\\triewords";
// will split the url and get the words
private static List<String> parseUrl(String url){
if (url.equals(""))
return null;
String [] words = url.split("/");
if (words.length == 0)
return null;
List <String> urlWords = new ArrayList<String>();
urlWords.add("http");
for (int i = 1; i < words.length - 1; ++ i){
if (words[i].equals(""))
continue;
urlWords.add(words[i]);
}
String lastWord = words[words.length - 1];
for (int i = 0; i < lastWord.length(); ++ i){
urlWords.add(lastWord.substring(i, i + 1));
}
return urlWords;
}
public static void main(String[] args) {
//create the Trie class
Trie trie = new Trie();
// write your code here
BufferedReader reader;
int cnt = 0;
try {
reader = new BufferedReader(new FileReader(testFileLocation));
String line = reader.readLine();
while (line != null) {
// get the list of word by url
List<String> wordsList = parseUrl(line);
// add trie our non nul line
if (wordsList != null) {
if (!trie.containsUrl(wordsList)){
trie.addWord(wordsList);
++cnt;
}
}
// read next line
line = reader.readLine();
}
reader.close();
List<String> urls = trie.getWords(new ArrayList<String>());
System.out.println("the count number of URL inserted in trie " + urls.size());
System.out.println("Number of leaf Node = " + trie.getLeafs());
System.out.println("Number of Character Node = " + trie.getCharacterNodes());
System.out.println("Number of Word Node = " + trie.getWordNodes());
double inBytes = getFileSize(testFileLocation);
System.out.println("size of file in bytes = " + inBytes);
List<String> trieNodeWords = trie.getNodeWords();
writeTrieNodeInFile(trieNodeWords);
double outFileSize = getFileSize(outputFileLocation);
System.out.println("size of trie in bytes = " + outFileSize);
} catch (IOException e) {
e.printStackTrace();
}
}
public static double getFileSize(String name){
File file =new File(name);
double bytes = 0;
if (file.exists()){
bytes = file.length();
}
return bytes;
}
public static void writeTrieNodeInFile(List<String> trieNodeWords){
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileLocation));
for (String word : trieNodeWords){
writer.write(word + '\n');
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}