-
Notifications
You must be signed in to change notification settings - Fork 0
/
Messages.java
152 lines (148 loc) · 5.52 KB
/
Messages.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Messages implements Iterable<Message> {
Message[] messages;
public Messages(){
try {
File file = new File(System.getProperty("user.dir") + "/messages.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
int messagesSize = 1;
while ((st = br.readLine()) != null) {
if (st.startsWith("#"))
messagesSize++;
}
messages = new Message[messagesSize];
}
catch (IOException e){
System.out.println(e);
}
}
public void generateMessages(String path){
try {
File file = new File(path);
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
String from = "", to = "", message = "";
int i = 0;
int countingMessages = messages.length - 1;
while ((st = br.readLine()) != null) {
if (st.startsWith("From:"))
from = st.substring(5);
else if (st.startsWith("To:"))
to = st.substring(3);
else if (!st.startsWith("#"))
message += st;
else if ((st.startsWith("#"))) {
Message massageToInput = new Message(from, to, message);
messages[i] = massageToInput;
i++;
if (countingMessages != 0) {
from = "";
to = "";
message = "";
}
countingMessages--;
}
}
Message massageToInput = new Message(from, to, message);
messages[i] = massageToInput;
}
catch(IOException e)
{System.out.println(e);}
}
public Iterator<Message> iterator(){
return new MessagesIterator();
}
private int countWords(Message count){
String[] toCount=count.getThemessage().split(" ");
return toCount.length;
}
private String checkDot(String toCheck){
String fixedWord;
char dot=toCheck.charAt(toCheck.length()-1);
if(dot=='.')
fixedWord=toCheck.substring(0,toCheck.length()-1);
else
fixedWord=toCheck;
return fixedWord;
}
public HashTable createHashTable(int i, int m){
//int n=countWords(messages[i]);
HashTable tableForSentence=new HashTable(m);
String fixedWord;
String[] splitting=messages[i].getThemessage().split(" ");
for(int j=0;j<splitting.length;j++){
fixedWord=checkDot(splitting[j]);
Word input=new Word(fixedWord);
int hashCode=tableForSentence.Hashfunction(input.getKey());
if(tableForSentence.getTable()[hashCode]!=null) {
if (!tableForSentence.getTable()[hashCode].containsandRaise(input.getWord())) // Assuming the same words have the same generative key
tableForSentence.gainandChain(hashCode,input.getWord());
else
tableForSentence.updateFields();
}
else
tableForSentence.gainandChain(hashCode,input.getWord());
}
return tableForSentence;
}
public void createHashTables(String m){
for(int i=0;i<messages.length;i++){
messages[i].setHashTable(createHashTable(i,Integer.parseInt(m)));
}
}
private boolean checkifFriends(int i,BTree btree){
boolean found=true;
String strToCheck1=messages[i].getNameOfRecipient()+" & "+ messages[i].getNameOfSender();
String strToCheck2=messages[i].getNameOfSender()+" & "+messages[i].getNameOfRecipient();
Pair toSearch=btree.search(strToCheck1);
Pair toSearch2=btree.search(strToCheck2);
if(toSearch ==null && toSearch2==null)
found=false;
return found;
}
public String findSpams(String path,BTree btree){
String output = "";
boolean foundSpam=false;
try {
Spams spamArr = new Spams(path);
for(int i=0;i<messages.length;i++){
if (!checkifFriends(i,btree)){
Iterator<Spam> it=spamArr.iterator();
while(it.hasNext()&!foundSpam){
Spam obj=it.next();
HashListElement toSearch=messages[i].getTable().searchWord(obj.getWord());
if(toSearch!=null){
if((toSearch.getCounter()/(double)messages[i].getTable().getnumberOfEntries())*100 >= obj.getPercent()){
output+=(i+",");foundSpam=true;}
}}}foundSpam=false;}
return BTree.shave(output, ',');
}
catch(IOException e) {
System.out.println(e);
}
return output;
}
private class MessagesIterator<T> implements Iterator<T>{
private int index;
public MessagesIterator(){
index=0;
}
public boolean hasNext(){
return index<messages.length;
}
public T next(){
if(hasNext()){
int currindex=index;
this.index++;
return (T)messages[currindex];
}
throw new NoSuchElementException();
}
}
}