lab5: add development files, not ready for production

This commit is contained in:
dymik739 2023-06-08 15:31:50 +03:00
parent 7e2f549ae6
commit 102089cb23
7 changed files with 361 additions and 0 deletions

50
labs/5/Main.java Normal file
View File

@ -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.";
}
}
}

99
labs/5/Sentence.java Normal file
View File

@ -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;
}
}

26
labs/5/Symbol.java Normal file
View File

@ -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;
}
}

28
labs/5/Text.java Normal file
View File

@ -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;
}
}

92
labs/5/Word.java Normal file
View File

@ -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;
}
}

View File

@ -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'

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)