From 102089cb23cf12e1722c95bcd7962ef408ad6a42 Mon Sep 17 00:00:00 2001 From: dymik739 Date: Thu, 8 Jun 2023 15:31:50 +0300 Subject: [PATCH] lab5: add development files, not ready for production --- labs/5/Main.java | 50 +++++++++++++++++ labs/5/Sentence.java | 99 ++++++++++++++++++++++++++++++++++ labs/5/Symbol.java | 26 +++++++++ labs/5/Text.java | 28 ++++++++++ labs/5/Word.java | 92 +++++++++++++++++++++++++++++++ labs/5/lab5lib/Fetcher.java | 56 +++++++++++++++++++ labs/5/lab5lib/fetchContent.py | 10 ++++ 7 files changed, 361 insertions(+) create mode 100644 labs/5/Main.java create mode 100644 labs/5/Sentence.java create mode 100644 labs/5/Symbol.java create mode 100644 labs/5/Text.java create mode 100644 labs/5/Word.java create mode 100644 labs/5/lab5lib/Fetcher.java create mode 100644 labs/5/lab5lib/fetchContent.py diff --git a/labs/5/Main.java b/labs/5/Main.java new file mode 100644 index 0000000..5b613cd --- /dev/null +++ b/labs/5/Main.java @@ -0,0 +1,50 @@ +import lab5lib.Fetcher; + +public class Main { + public static void main(String[] args) { + boolean demoContent; + + demoContent = parseArgs(args); + String inputString = getInput(demoContent); + System.out.println(inputString); + + Text text = new Text(inputString); + text.cleanFirstSentence(); + + System.out.println(text); + } + + private static String getInput(boolean demoContent) { + if (demoContent) { + return "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 { + return receiveText(); + } + } + + private static boolean parseArgs(String[] args) { + boolean demoContent = false; + + for (String arg : args) { + if ("-d".equals(arg)) { + demoContent = true; + } + } + + return demoContent; + } + + private static String receiveText() { + try { + return Fetcher.fetchTextFromPython(); + } catch (Exception e) { + return "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."; + } + } +} diff --git a/labs/5/Sentence.java b/labs/5/Sentence.java new file mode 100644 index 0000000..5fe2112 --- /dev/null +++ b/labs/5/Sentence.java @@ -0,0 +1,99 @@ +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Sentence { + private Word[] words; + private Symbol ending; + + public Sentence(Word[] words, Symbol ending) { + this.words = words; + } + + public Sentence(Word[] words) { + this.words = words; + } + + public Sentence(String sentence) { + int totalSentenceLength = sentence.length(); + this.ending = new Symbol(sentence.charAt(totalSentenceLength-1)); + + String[] rawWords = sentence.substring(0, totalSentenceLength-1) + .split("(?<=[,;:]?) "); + + Word[] preparedWords = new Word[rawWords.length]; + + for (int i = 0; i < rawWords.length; i++) { + preparedWords[i] = new Word(rawWords[i]); + } + + this.words = preparedWords; + } + + public void addWord(Word newWord) { + Word[] newWordArray = new Word[1]; + newWordArray[0] = newWord; + addWords(newWordArray); + } + + public void addWords(Word[] newWords) { + int currentWordsAmount = this.words.length; + int newWordsAmount = newWords.length; + int totalLength = currentWordsAmount + newWordsAmount; + + Word[] updatedWords = new Word[currentWordsAmount + newWordsAmount]; + + for (int i = 0; i < currentWordsAmount; i++) { + updatedWords[i] = this.words[i]; + } + + for (int i = 0; i < newWordsAmount; i++) { + updatedWords[currentWordsAmount+i] = newWords[i]; + } + + this.words = updatedWords; + } + + public int index(Word w) { + int wordIndex = -1; + + for (int i = 0; i < this.words.length; i++) { + if (this.words[i].equals(w)) { + wordIndex = i; + break; + } + } + + return wordIndex; + } + + public void removeWord(Word w) { + int deleteIndex = index(w); + removeWord(deleteIndex); + } + + public void removeWord(int deleteIndex) { + // first pass, lookup + Word[] updatedArray = new Word[this.words.length-1]; + + for (int i = 0; i < deleteIndex; i++) { + updatedArray[i] = this.words[i]; + } + + for (int i = 0; i < this.words.length - deleteIndex - 2; i++) { + updatedArray[deleteIndex+i] = this.words[deleteIndex+i+1]; + } + + this.words = updatedArray; + } + + public String toString() { + String result = ""; + + for (Word i : this.words) { + result += i.toString() + " "; + } + + return result.substring(0, result.length()-1) + this.ending.toString(); + //return result; + } +} diff --git a/labs/5/Symbol.java b/labs/5/Symbol.java new file mode 100644 index 0000000..f0da4e6 --- /dev/null +++ b/labs/5/Symbol.java @@ -0,0 +1,26 @@ +public class Symbol { + private char symbol; + + public Symbol(char symbol) { + this.symbol = symbol; + } + + public void setSymbol(char symbol) { + this.symbol = symbol; + } + + public char getSymbol() { + // this is painful. + return symbol; + } + + public String toString() { + char[] arr = new char[1]; + arr[0] = this.symbol; + return new String(arr); + } + + public boolean equals(Symbol o) { + return this.symbol == o.symbol; + } +} diff --git a/labs/5/Text.java b/labs/5/Text.java new file mode 100644 index 0000000..7d8061b --- /dev/null +++ b/labs/5/Text.java @@ -0,0 +1,28 @@ +//import java.util.regex.Pattern; + +public class Text { + private Sentence[] sentences; + + public Text(String input) { + String[] rawSentences = input.split("\\[.?!]( |$)"); + Sentence[] newSentences = new Sentence[rawSentences.length]; + + for (int i = 0; i < rawSentences.length; i++) { + newSentences[i] = new Sentence(rawSentences[i]); + } + + this.sentences = newSentences; + } + + public void cleanFirstSentence() {} + + public String toString() { + String result = ""; + + for (Sentence s : this.sentences) { + result += s.toString() + " "; + } + + return result; + } +} diff --git a/labs/5/Word.java b/labs/5/Word.java new file mode 100644 index 0000000..df8db75 --- /dev/null +++ b/labs/5/Word.java @@ -0,0 +1,92 @@ +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Word { + private Symbol[] symbols; + private Symbol ending; + + public Word(String rawWord) { + Symbol p; + Symbol[] w; + + Matcher punctuationMatcher = Pattern.compile("[,;:]?").matcher(rawWord); + String foundPunctuation; + + if (punctuationMatcher.find()) { + foundPunctuation = punctuationMatcher.group(); + } else { + foundPunctuation = ""; + } + + if ("".equals(foundPunctuation)) { + p = new Symbol(' '); + } else { + p = new Symbol(foundPunctuation.charAt(0)); + } + + Matcher wordMatcher = Pattern.compile("[A-Za-z]*").matcher(rawWord); + String foundWord; + + if (wordMatcher.find()) { + foundWord = wordMatcher.group(); + } else { + foundWord = ""; + } + + Symbol[] newWordArray = new Symbol[foundWord.length()]; + + for (int i = 0; i < foundWord.length(); i++) { + newWordArray[i] = new Symbol(foundWord.charAt(i)); + } + + this.symbols = newWordArray; + this.ending = p; + } + + /* + public Word(Symbol[] word, Symbol ending) { + this.word = word; + this.ending = ending; + } + + public Word(Symbol[] word) { + this.word = word; + this.ending = new Symbol(); + } + + public Word() { + this.word = new Word; + this.ending = new Symbol(); + } + */ + + //@Override + public boolean equals(Word o) { + if (o.symbols.length != this.symbols.length) { + return false; + } + + for (int i = 0; i < this.symbols.length; i++) { + if (this.symbols[i] != o.symbols[i]) { + return false; + } + } + + return true; + } + + @Override + public String toString() { + String result = ""; + + for (Symbol i : this.symbols) { + result += i.toString(); + } + + if (!this.ending.equals(new Symbol(' '))) { + result += this.ending.toString(); + } + + return result; + } +} diff --git a/labs/5/lab5lib/Fetcher.java b/labs/5/lab5lib/Fetcher.java new file mode 100644 index 0000000..a40d442 --- /dev/null +++ b/labs/5/lab5lib/Fetcher.java @@ -0,0 +1,56 @@ +package lab5lib; + +//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); + } + } + */ + + /** + * Allows to fetch unique strings directly from the search engine + * + * @since 0.2 + * + * @throws Exception in case if the server fails to provide the content + */ + public static String fetchTextFromPython() throws Exception { + try { + Process contentFetcher = Runtime.getRuntime().exec("python3 lab3lib/fetchContent.py"); + Scanner reader = new Scanner(contentFetcher.getInputStream()); + return new String(reader.nextLine()); + } catch (Exception e) { + throw new Exception("Failed to fetch content from the server"); + } + } + + /* + 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/5/lab5lib/fetchContent.py b/labs/5/lab5lib/fetchContent.py new file mode 100644 index 0000000..df28032 --- /dev/null +++ b/labs/5/lab5lib/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)