/
wordCount.java
125 lines (112 loc) · 2.36 KB
/
wordCount.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
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
import java.util.Scanner;
import java.util.ArrayList;
import java.util.ListIterator;
import java.io.*;
public class wordCount
{
File src;
Scanner in;
ArrayList<Node> list;
ListIterator<Node> iter;
public wordCount ( String s )
{
long startTime = System.currentTimeMillis();
list = new ArrayList<Node>();
try
{
in = new Scanner(src = new File(s));
while ( in.hasNext() )
{
String str = in.next();
str = str.replaceAll("\\p{P}","");
str = str.replaceAll("\\s","");
if ( !str.equals("") )
{
Node n = new Node(str,1);
if ( !exists(n,list) )
{
list.add(n);
}
}
}
iter = list.listIterator();
Node[] orderedNodes = toOrderedArray( list,iter );
printList( orderedNodes );
}
catch ( FileNotFoundException e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println( "\nRuntime: " + ( ( (float)( endTime - startTime ) / 1000 ) + "s" ) );
}
public Node[] toOrderedArray ( ArrayList<Node> list, ListIterator<Node> iter )
{
Node[] nodes = new Node[list.size()];
int i=0;
while ( list.size() > 0 )
{
Node n = findMax( list,iter );
nodes[i] = n;
i++;
list.remove( n );
}
return nodes;
}
public Node findMax ( ArrayList<Node> list, ListIterator<Node> iter )
{
//Default value for each Node is 1.
Node n = null;
int max = 0;
for ( int i=0;i<list.size();i++ )
{
if ( list.get(i).count > max )
{
n = list.get( i );
max = n.count;
}
}
return n;
}
public boolean exists ( Node n, ArrayList<Node> list )
{
for ( int i=0;i<list.size();i++ )
{
Node m = list.get(i);
if ( n.data.equals(m.data) )
{
m.count++;
return true;
}
}
return false;
}
public void printList ( Node[] nodes )
{
PrintWriter pw;
try
{
pw = new PrintWriter( "wordCount_Output_HP.txt","UTF-8" );
for ( int i=0;i<nodes.length;i++ )
{
pw.println( "("+nodes[i].data+", "+nodes[i].count+")" );
}
pw.close();
}
catch ( FileNotFoundException e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
catch ( UnsupportedEncodingException e )
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
}
public static void main (String args[])
{
new wordCount( "HarryPotter1-7.txt" );
}
}