From d606f6994f3acd02030be9532ba6b39b0f59e945 Mon Sep 17 00:00:00 2001 From: dymik739 Date: Sat, 20 May 2023 10:57:37 +0300 Subject: [PATCH] add the support of sideloading texts into the lab3 code --- labs/3/Finder.java | 57 +++++++++++++++++++--------------- labs/3/lab3lib/Fetcher.java | 49 +++++++++++++++++++++++++++++ labs/3/lab3lib/fetchContent.py | 10 ++++++ 3 files changed, 91 insertions(+), 25 deletions(-) create mode 100644 labs/3/lab3lib/Fetcher.java create mode 100644 labs/3/lab3lib/fetchContent.py diff --git a/labs/3/Finder.java b/labs/3/Finder.java index 10cdc9d..432dff1 100644 --- a/labs/3/Finder.java +++ b/labs/3/Finder.java @@ -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 firstSentenceWords = new HashSet(); 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 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(); - - /* - if (firstSentenceWords.size() == 1) { - System.out.println("Found these words: " + firstSentenceWords.toString()); + final long endTime = System.nanoTime(); - 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 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!"); } diff --git a/labs/3/lab3lib/Fetcher.java b/labs/3/lab3lib/Fetcher.java new file mode 100644 index 0000000..f554170 --- /dev/null +++ b/labs/3/lab3lib/Fetcher.java @@ -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' diff --git a/labs/3/lab3lib/fetchContent.py b/labs/3/lab3lib/fetchContent.py new file mode 100644 index 0000000..df28032 --- /dev/null +++ b/labs/3/lab3lib/fetchContent.py @@ -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)