4 Commits

8 changed files with 265 additions and 7 deletions

13
labs/3/Benchmarker.java Normal file
View File

@@ -0,0 +1,13 @@
import lab3lib.Finder;
public class Benchmarker {
public static void main(String[] args) {
System.out.print("Timing object creation...");
long startTime = System.nanoTime();
Finder obj = new Finder();
long endTime = System.nanoTime();
System.out.println(" Finished!");
System.out.println("Operation took " + (endTime - startTime) + "ns");
}
}

146
labs/3/Finder.java Normal file
View File

@@ -0,0 +1,146 @@
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) {
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);
}
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 '" + toLowerCaseStringBuilder(word1) + "' in " + firstSentenceWords.toString() + "...");
Set<StringBuilder> tempWords = new HashSet<>(firstSentenceWords);
boolean wordFound = false;
for (StringBuilder word2 : firstSentenceWords) {
if (compareStringBuilders(toLowerCaseStringBuilder(word1), toLowerCaseStringBuilder(word2))) {
wordFound = true;
tempWords.remove(word2);
break;
}
}
if (wordFound) {
System.out.println(" found!");
firstSentenceWords = tempWords;
} else {
System.out.println(" absent.");
}
}
}
final long endTime = System.nanoTime();
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 pick one.");
} 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);
}
}

View File

@@ -1,5 +0,0 @@
public class Main {
public static void main(String[] args) {
}
}

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)

View File

@@ -0,0 +1,7 @@
import
public class Letter {
this.
public void main() {
}
}

View File

@@ -4,8 +4,7 @@ import renderSuite.Vec3;
import renderSuite.Util; import renderSuite.Util;
import renderSuite.Ray; import renderSuite.Ray;
public class Sphere { public class Sphere extends Surface {
Vec3 p, c;
double r; double r;
public Sphere(Vec3 position, Vec3 color, double radius) { public Sphere(Vec3 position, Vec3 color, double radius) {

View File

@@ -0,0 +1,39 @@
package renderSuite;
import renderSuite.Vec3;
//import renderSuite.Util;
//import renderSuite.Ray;
public class Surface {
Vec3 p, c;
public void main() {
}
/*
public boolean intersect(Ray r) {
// OLD
//double dist = Util.d(ray_position, position);
//return (Util.d(ray_position, position) <= radius);
// using RTX
Vec3 l = new Vec3(this.p.x - r.p.x,
this.p.y - r.p.y,
this.p.z - r.p.z);
Vec3 nl = l.get_norm();
double cosine = Util.dot(r.f, nl);
// >90 degrees = no intersection
if (cosine < 0) {
return false;
}
double tc = l.len() * cosine;
double d = Math.sqrt( l.len()*l.len() - tc*tc );
return (d < this.r);
}
*/
}