-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigramElement.java
More file actions
111 lines (102 loc) · 2.67 KB
/
Copy pathBigramElement.java
File metadata and controls
111 lines (102 loc) · 2.67 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
/*
* File: BigramElement
* Author: Jackson Davenport
*
* This is a helper class of a single bigram element. An element is the base
* word and a hashmap of the previous words and their counts. This class
* handles the table operations while the structure to hold all these is held
* elsewhere.
*/
import java.util.Hashtable;
import java.util.Enumeration;
import java.io.Serializable;
public class BigramElement implements Serializable{
private String baseWord;
private Hashtable<String, Integer> previousWord;
private int totalCount;
public BigramElement(String word){
baseWord = word;
previousWord = new Hashtable<String, Integer>();
totalCount = 0;
}
/* addWord(String word)
* Takes as input the previous word, so either increment the
* count or add the new instance to the list
*/
public void addWord(String word){
// If the word is already tracked increment the count
if(previousWord.containsKey(word)){
int value = previousWord.get(word) + 1;
previousWord.put(word, value);
}
else{// Otherwise add it into the hash table with count = 1
previousWord.put(word, 1);
}
totalCount++;
}
/* addWord(String word, int count)
* Takes as input the previous word and set it to the
* desired count
*/
public void addWord(String word, int count){
previousWord.put(word, count);
totalCount += count;
}
/*
* Object Methods
*/
/* int hashCode()
* Overwrite the hashcode method to return the hash of the baseword
*/
public int hashCode(){
return baseWord.hashCode();
}
/* String toString()
* Overwrite the toString method to print out the keys and counts
*/
public String toString(){
String output = getWord() + " - [";
Enumeration e = getKeys();
while(e.hasMoreElements()){
String key = (String) e.nextElement();
output = output + key + ": " + getCount(key);
if(e.hasMoreElements()){
output = output + ", ";
}
}
return output + "]";
}
/* boolean equals(Object o)
* Check the baseword, total count, and individual counts
*/
public boolean equals(Object o){
if(o == null || !(o instanceof BigramElement)){
return false;
}
if(!((BigramElement) o).getWord().equals(this.getWord())){
return false;
}
if(((BigramElement) o).getTotalCount() != this.getTotalCount()){
return false;
}
return true;
}
/*
* Getter Methods
*/
public int getCount(String word){
return previousWord.get(word);
}
public int getTotalCount(){
return totalCount;
}
public Enumeration getKeys(){
return previousWord.keys();
}
public String getWord(){
return baseWord;
}
public boolean containsKey(String word){
return previousWord.containsKey(word);
}
}