140 lines
4.9 KiB
Java
140 lines
4.9 KiB
Java
|
import java.util.HashSet;
|
||
|
import java.util.Set;
|
||
|
import java.util.regex.Pattern;
|
||
|
|
||
|
public class Finder {
|
||
|
public static void main(String[] args) {
|
||
|
StringBuilder inputStringBuilder = new StringBuilder("Testing, text to make up words iiiiiii. Testing, text without specific, up words. Therefore, we don't care about arteriscs and other useless symbols, as the only reason to use it is when the code runs horrible.");
|
||
|
StringBuilder[] sentences = splitStringBuilder(inputStringBuilder, "[.?!] ?");
|
||
|
Set<StringBuilder> firstSentenceWords = new HashSet<StringBuilder>();
|
||
|
|
||
|
for (StringBuilder word : splitStringBuilder(sentences[0], ",? ")) {
|
||
|
firstSentenceWords.add(word);
|
||
|
}
|
||
|
|
||
|
long startTime = System.nanoTime();
|
||
|
|
||
|
for (StringBuilder sentence : subarrayStringBuilder(sentences, 1, sentences.length)) {
|
||
|
StringBuilder[] words = splitStringBuilder(sentence, ",? ");
|
||
|
|
||
|
for (StringBuilder word1 : words) {
|
||
|
System.out.print("Searching for " + word1 + " in " + firstSentenceWords.toString() + "...");
|
||
|
|
||
|
Set<StringBuilder> tempWords = new HashSet<>(firstSentenceWords);
|
||
|
|
||
|
boolean wordFound = false;
|
||
|
for (StringBuilder word2 : firstSentenceWords) {
|
||
|
if (compareStringBuilders(word1, word2)) {
|
||
|
System.out.println(" found!");
|
||
|
wordFound = true;
|
||
|
tempWords.remove(word2);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (wordFound) {
|
||
|
firstSentenceWords = tempWords;
|
||
|
} else {
|
||
|
System.out.println(" absent.");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
long endTime = System.nanoTime();
|
||
|
|
||
|
/*
|
||
|
if (firstSentenceWords.size() == 1) {
|
||
|
System.out.println("Found these words: " + firstSentenceWords.toString());
|
||
|
|
||
|
for (StringBuilder word : firstSentenceWords) {
|
||
|
System.out.printf("%s, ", new String(word));
|
||
|
}
|
||
|
|
||
|
|
||
|
System.out.println();
|
||
|
} else if (firstSentenceWords.size() > 1) {
|
||
|
System.out.println("Found these words: "
|
||
|
} else {
|
||
|
System.out.println("No words found!");
|
||
|
}
|
||
|
*/
|
||
|
System.out.println(getFinalMessage(firstSentenceWords));
|
||
|
System.out.println("Stats: search execution took " + (endTime - startTime) + "ns");
|
||
|
}
|
||
|
|
||
|
private static StringBuilder getFinalMessage(Set<StringBuilder> s) {
|
||
|
if (s.size() == 1) {
|
||
|
return new StringBuilder("Found the word '" + s.iterator().next() + "'");
|
||
|
} else if (s.size() >= 1) {
|
||
|
return new StringBuilder("Found more than one word (" + s.toString() + "), can't decide.");
|
||
|
} else {
|
||
|
return new StringBuilder("No such word has been found!");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static boolean compareStringBuilders(StringBuilder a, StringBuilder b)
|
||
|
{
|
||
|
if (a.length() == b.length())
|
||
|
{
|
||
|
for (int i = 0; i < a.length(); i++)
|
||
|
{
|
||
|
if (a.charAt(i) != b.charAt(i))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
return true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// apparently, this is incredibly complicated in Java
|
||
|
private static String[] subarray(String[] array, int start, int end) {
|
||
|
String[] result = new String[end - start];
|
||
|
|
||
|
for (int i = start, j = 0; i < end; i++, j++) {
|
||
|
result[j] = array[i];
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
// little wrapper to simplify StringBuilder usage task
|
||
|
private static StringBuilder[] subarrayStringBuilder(StringBuilder[] array, int start, int end) {
|
||
|
String[] tempArray = subarray(stringBuilderArrayToStringArray(array), start, end);
|
||
|
return stringArrayToStringBuilderArray(tempArray);
|
||
|
}
|
||
|
|
||
|
// name describes it well enough
|
||
|
private static StringBuilder[] stringArrayToStringBuilderArray(String[] inputArray) {
|
||
|
StringBuilder[] outputArray = new StringBuilder[inputArray.length];
|
||
|
|
||
|
for (int i = 0; i < inputArray.length; i++) {
|
||
|
outputArray[i] = new StringBuilder(inputArray[i]);
|
||
|
}
|
||
|
|
||
|
return outputArray;
|
||
|
}
|
||
|
|
||
|
// reverse of the above
|
||
|
private static String[] stringBuilderArrayToStringArray(StringBuilder[] inputArray) {
|
||
|
String[] outputArray = new String[inputArray.length];
|
||
|
|
||
|
for (int i = 0; i < inputArray.length; i++) {
|
||
|
outputArray[i] = new String(inputArray[i]);
|
||
|
}
|
||
|
|
||
|
return outputArray;
|
||
|
}
|
||
|
|
||
|
// same as String split method but using StringBuilder
|
||
|
private static StringBuilder[] splitStringBuilder(StringBuilder input, String regexp) {
|
||
|
String[] tempStrings = Pattern.compile(regexp).split(input);
|
||
|
return stringArrayToStringBuilderArray(tempStrings);
|
||
|
}
|
||
|
|
||
|
}
|