-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkChecker.java
More file actions
143 lines (137 loc) · 3.21 KB
/
LinkChecker.java
File metadata and controls
143 lines (137 loc) · 3.21 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
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
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Arrays;
public class LinkChecker {
public final int DICTIONARY_SIZE=658964;
public final int INDEX_START_VALUE =0;
public String[] readDictionary() throws Exception
{
String[] dictionaryList = new String[DICTIONARY_SIZE];
String FilePath = new File("words.txt").getAbsolutePath();;
FileReader fileRead = new FileReader(FilePath);
BufferedReader buffRead = new BufferedReader(fileRead);
for(int index = INDEX_START_VALUE; index<dictionaryList.length -1 ; index++ )
{
dictionaryList[index] = buffRead.readLine();
}
buffRead.close();
fileRead.close();
return dictionaryList;
}
public ArrayList<String> readWordList(String wordChain)
{
boolean creatingArray = true;
ArrayList<String> wordList= new ArrayList<String>();
Scanner wordScanner = new Scanner(wordChain);
wordScanner.useDelimiter(",");
while( creatingArray)
{
if( wordScanner.hasNext())
{
wordList.add(wordScanner.next());
}
else
{
creatingArray = false;
}
}
wordScanner.close();
return wordList;
}
public boolean isUniqueList(ArrayList<String> wordList)
{
boolean noSimilarWords = true;
int index = INDEX_START_VALUE;
int offset =1;
if( wordList.size() > 2) {
while(index < wordList.size() && noSimilarWords)
{
String indexString = wordList.get(index);
for( offset = 1; offset < (wordList.size() - index);offset++)
{
String offsetString = wordList.get(index + offset);
if(indexString == offsetString )
{
noSimilarWords = false;
}
}
index++;
offset =1;
}
}
else if( wordList.size() == 2)
{
if(wordList.get(offset) == wordList.get(index) )
{
noSimilarWords = false;
}
}
return noSimilarWords;
}
public boolean isEnglishWord(String testWord, String[] dictionaryList )
{
if( Arrays.binarySearch(dictionaryList,testWord) >= 0)
{
return true;
}
else {
return false;
}
}
public boolean isDifferentByOne(String oldWord, String newWord)
{
int charDifferences = 0;
char[] oldWordChar = oldWord.toCharArray();
char[] newWordChar = newWord.toCharArray();
if(oldWordChar.length == newWordChar.length)
{
for( int index =INDEX_START_VALUE ; index < oldWordChar.length; index++)
{
if(oldWordChar[index] != newWordChar[index] )
{
charDifferences++;
}
}
if( charDifferences == 1)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public boolean isWordChain(ArrayList<String> wordList) throws Exception
{
boolean validWordChain = true;
int index = INDEX_START_VALUE;
String[] dictionaryList = readDictionary();
while(index < wordList.size() -1 && validWordChain)
{
String previousWord = wordList.get(index);
if(isEnglishWord(previousWord, dictionaryList) == false)
{
validWordChain = false;
}
index++;
String nextWord = wordList.get(index);
if(isDifferentByOne(previousWord,nextWord)== false)
{
validWordChain = false;
}
}
if(!isUniqueList(wordList))
{
validWordChain = false;
}
return validWordChain;
}
}