add the support of sideloading texts into the lab3 code

This commit is contained in:
dymik739 2023-05-20 10:57:37 +03:00
parent 400aaab49d
commit d606f6994f
3 changed files with 91 additions and 25 deletions

View File

@ -1,31 +1,41 @@
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import lab3lib.Fetcher;
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, "[.?!] ?");
boolean demoContent = false;
StringBuilder inputStringBuilder = new StringBuilder();
if (demoContent) {
inputStringBuilder = new StringBuilder("Testing, text to make up words. 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.");
} else {
inputStringBuilder = receiveText();
}
System.out.println("Processing string: " + inputStringBuilder);
final StringBuilder[] sentences = splitStringBuilder(inputStringBuilder, "[.?!] ?");
Set<StringBuilder> firstSentenceWords = new HashSet<StringBuilder>();
for (StringBuilder word : splitStringBuilder(sentences[0], ",? ")) {
firstSentenceWords.add(word);
}
long startTime = System.nanoTime();
final 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() + "...");
System.out.print("Searching for '" + toLowerCaseStringBuilder(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!");
if (compareStringBuilders(toLowerCaseStringBuilder(word1), toLowerCaseStringBuilder(word2))) {
wordFound = true;
tempWords.remove(word2);
break;
@ -33,6 +43,7 @@ public class Finder {
}
if (wordFound) {
System.out.println(" found!");
firstSentenceWords = tempWords;
} else {
System.out.println(" absent.");
@ -40,33 +51,29 @@ public class Finder {
}
}
long endTime = System.nanoTime();
final 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 receiveText() {
try {
return Fetcher.fetchTextFromPython();
} catch (Exception e) {
return new StringBuilder("Testing, text to make up words. 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.");
}
}
private static StringBuilder toLowerCaseStringBuilder(StringBuilder inputStringBuilder) {
return new StringBuilder(new String(inputStringBuilder).toLowerCase());
}
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 if (s.size() > 1) {
return new StringBuilder("Found more than one word (" + s.toString() + "), can't pick one.");
} else {
return new StringBuilder("No such word has been found!");
}

View File

@ -0,0 +1,49 @@
package lab3lib;
//import java.net.URL;
//import java.io.BufferedInputStream;
import java.util.Scanner;
import java.lang.Exception;
//import javax.json.JsonObject;
public class Fetcher {
public void main() {}
/*
private static String fetchString(String remote_url) {
try {
Scanner reader = new Scanner(new URL(remote_url).openStream(), "UTF-8");
String jsonString = "";
if (reader.hasNextLine()) {
jsonString = reader.nextLine();
}
return jsonString;
} catch (Exception e) {
System.out.println("[ERROR] Failed to fetch resource from " + remote_url + " due to the following exception: " + e);
System.exit(1);
}
}
*/
public static StringBuilder fetchTextFromPython() {
try {
Process contentFetcher = Runtime.getRuntime().exec("python3 lab3lib/fetchContent.py");
Scanner reader = new Scanner(contentFetcher.getInputStream());
return new StringBuilder(reader.nextLine());
} catch (Exception e) {
return new StringBuilder("");
}
}
/*
public StringBuilder fetchText(String request) {
String responce = fetchString("http://10.1.1.2:8080/search?language=en-US&format=json&q=" + request);
JSONObject results = new JSONObject(responce);
return results.get("results").get(0).get("content");
}
*/
}
//JSONObject results = 'http://10.1.1.2:8080/search?q=test&language=en-US&format=json'

View File

@ -0,0 +1,10 @@
import requests
from random import randint
import json
r = requests.get("http://10.1.1.2:8080/search?q=test&format=json&language=en-US")
results = json.loads(r.text)["results"]
final_content = results[randint(0, len(results))]['content']
print(final_content)