From 3d5f01488aa0df78347fe94566a7d7cf9042d145 Mon Sep 17 00:00:00 2001 From: Noah Heuser <nheuser@uni-koblenz.de> Date: Fri, 28 Jun 2024 12:54:56 +0200 Subject: [PATCH] changed result and intput data format --- ChatGPTParsing/src/json/JSONArray.java | 13 ++- ChatGPTParsing/src/json/JSONBoolean.java | 15 +++ ChatGPTParsing/src/json/JSONString.java | 4 + ChatGPTParsing/src/writer/Writer.java | 125 +++++++++++++---------- 4 files changed, 104 insertions(+), 53 deletions(-) create mode 100644 ChatGPTParsing/src/json/JSONBoolean.java diff --git a/ChatGPTParsing/src/json/JSONArray.java b/ChatGPTParsing/src/json/JSONArray.java index e932cd1..e0dc047 100644 --- a/ChatGPTParsing/src/json/JSONArray.java +++ b/ChatGPTParsing/src/json/JSONArray.java @@ -1,9 +1,11 @@ package json; +import java.util.Collections; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; -public class JSONArray implements JSONDatatype { +public class JSONArray implements JSONDatatype, Iterable<JSONDatatype> { private List<JSONDatatype> a; @@ -15,6 +17,10 @@ public class JSONArray implements JSONDatatype { a.add(e); } + public static void shuffle(JSONArray arr) { + Collections.shuffle(arr.a); + } + @Override public String toString() { StringBuilder sb = new StringBuilder(); @@ -30,4 +36,9 @@ public class JSONArray implements JSONDatatype { return sb.toString(); } + @Override + public Iterator<JSONDatatype> iterator() { + return a.iterator(); + } + } diff --git a/ChatGPTParsing/src/json/JSONBoolean.java b/ChatGPTParsing/src/json/JSONBoolean.java new file mode 100644 index 0000000..c28df2a --- /dev/null +++ b/ChatGPTParsing/src/json/JSONBoolean.java @@ -0,0 +1,15 @@ +package json; + +public class JSONBoolean implements JSONDatatype { + + private boolean b; + + public JSONBoolean(boolean b) { + this.b = b; + } + + @Override + public String toString() { + return b ? "true" : "false"; + } +} diff --git a/ChatGPTParsing/src/json/JSONString.java b/ChatGPTParsing/src/json/JSONString.java index 4d2b7c1..d6ee136 100644 --- a/ChatGPTParsing/src/json/JSONString.java +++ b/ChatGPTParsing/src/json/JSONString.java @@ -8,6 +8,10 @@ public class JSONString implements JSONDatatype { this.s = s; } + public String getString() { + return s; + } + @Override public String toString() { return "\"" + this.s + "\""; diff --git a/ChatGPTParsing/src/writer/Writer.java b/ChatGPTParsing/src/writer/Writer.java index 0335d51..4a488d4 100644 --- a/ChatGPTParsing/src/writer/Writer.java +++ b/ChatGPTParsing/src/writer/Writer.java @@ -3,7 +3,6 @@ package writer; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; -import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.LinkedList; @@ -15,6 +14,8 @@ import auxiliary.ListOfRulesContains; import grammar.Type2Grammar; import grammar.Type3Grammar; import json.JSONArray; +import json.JSONBoolean; +import json.JSONDatatype; import json.JSONObject; import json.JSONString; import type2.Type2Parser; @@ -26,8 +27,10 @@ public class Writer { private String defaultPath = "//wsl.localhost/Ubuntu/home/noah/gptgrammarparsing/PythonAPI"; private String path; + private int t3Counter = 0; + private int t2Counter = 0; - private static final int DISTINCT_WORDS = 20; + private static final int DISTINCT_WORDS = 3; private static final int MAX_LENGTH = 20; private WordGenerator wg = new WordGenerator(); @@ -42,19 +45,15 @@ public class Writer { this.path = path; } - private String createGrammarWordsPair(String grammar, List<String> words) { - JSONObject grammarWordsPair = new JSONObject(); - JSONArray wordArray = new JSONArray(); - for (String word : words) { - JSONString wordJSON = new JSONString(word); - wordArray.add(wordJSON); + private void writePairsToFile(JSONObject pairs, int type) { + String d = path + "/inputData/t" + type + "/input"; + if (type == 2) { + d += t2Counter++; + } else { + d += t3Counter++; } - grammarWordsPair.put(grammar, wordArray); - return grammarWordsPair.toString(); - } - - private void writePairsToFile(List<String> pairs) { - try (FileWriter fw = new FileWriter(path + "/grammarWordsPairs.txt")) { + d += ".txt"; + try (FileWriter fw = new FileWriter(d)) { fw.write(pairs.toString()); } catch (IOException e) { e.printStackTrace(); @@ -62,30 +61,42 @@ public class Writer { } private void writeResultToFileType2(List<Map<String, List<List<String>>>> normalizedRuleSet, List<String> words) { - try (BufferedWriter bw = new BufferedWriter(new FileWriter(path + "/resultsOfMembershipTest.txt"))) { - int i = 0; - for (Map<String, List<List<String>>> rs : normalizedRuleSet) { - for (int j = 0; j < DISTINCT_WORDS; j++) { - bw.write(t2p.isPartOfLanguage(rs, words.get(i++)) ? "true" : "false"); - bw.newLine(); - } - bw.newLine(); + int i = 0; + int k = 0; + JSONObject results = new JSONObject(); + for (Map<String, List<List<String>>> rs : normalizedRuleSet) { + JSONArray resultsPerGrammar = new JSONArray(); + for (int j = 0; j < DISTINCT_WORDS; j++) { + boolean result = t2p.isPartOfLanguage(rs, words.get(i++)); + resultsPerGrammar.add(new JSONBoolean(result)); } + results.put(k + "", resultsPerGrammar); + k++; + } + try (BufferedWriter bw = new BufferedWriter( + new FileWriter(path + "/resultsAlgo/resultsT2/resultsAlgorithmT2_" + t2Counter + ".txt"))) { + bw.write(results.toString()); } catch (IOException e) { e.printStackTrace(); } } private void writeResultToFileType3(List<FiniteStateMachine> fsms, List<String> words) { - try (BufferedWriter bw = new BufferedWriter(new FileWriter(path + "/resultsOfMembershipTest.txt"))) { - int i = 0; - for (FiniteStateMachine fsm : fsms) { - for (int j = 0; j < DISTINCT_WORDS; j++) { - bw.write(t3p.isPartOfLanguage(fsm, words.get(i++)) ? "true" : "false"); - bw.newLine(); - } - bw.newLine(); + int i = 0; + int k = 0; + JSONObject results = new JSONObject(); + for (FiniteStateMachine fsm : fsms) { + JSONArray resultsPerGrammar = new JSONArray(); + for (int j = 0; j < DISTINCT_WORDS; j++) { + boolean result = t3p.isPartOfLanguage(fsm, words.get(i++)); + resultsPerGrammar.add(new JSONBoolean(result)); } + results.put(k + "", resultsPerGrammar); + k++; + } + try (BufferedWriter bw = new BufferedWriter( + new FileWriter(path + "/resultsAlgo/resultsT3/resultsAlgorithmT3_" + t3Counter + ".txt"))) { + bw.write(results.toString()); } catch (IOException e) { e.printStackTrace(); } @@ -93,7 +104,7 @@ public class Writer { public void runType2Grammar(List<String> vars, List<String> alphabet, int maxConclusios, int maxConclusioSize, String start, int mode, int n) { - List<String> grammarWordsPairs = new LinkedList<>(); + JSONObject grammarWordsPairs = new JSONObject(); List<Map<String, List<List<String>>>> ruleSets = new LinkedList<>(); List<Map<String, List<List<String>>>> normalizedRuleSets = new LinkedList<>(); List<String> words = new LinkedList<>(); @@ -106,17 +117,22 @@ public class Writer { ruleSets.add(g.getRules()); Map<String, List<List<String>>> normalizedRuleSet = t2p.createCNF(g.getRules(), g.getStart(), mode); normalizedRuleSets.add(normalizedRuleSet); - List<String> wordsPerGrammar = wordGeneratorType2(normalizedRuleSet, alphabet, containsWords); - grammarWordsPairs.add(createGrammarWordsPair(g.toStringPython(), wordsPerGrammar)); - words.addAll(wordsPerGrammar); + JSONArray wordsPerGrammar = wordGeneratorType2(normalizedRuleSet, alphabet, containsWords); + grammarWordsPairs.put(g.toStringPython(), wordsPerGrammar); + for (JSONDatatype js : wordsPerGrammar) { + if (js instanceof JSONString) { + JSONString word = (JSONString) js; + words.add(word.getString()); + } + } } writeResultToFileType2(normalizedRuleSets, words); - writePairsToFile(grammarWordsPairs); + writePairsToFile(grammarWordsPairs, 2); } - private List<String> wordGeneratorType2(Map<String, List<List<String>>> normalizedRuleSet, List<String> alphabet, + private JSONArray wordGeneratorType2(Map<String, List<List<String>>> normalizedRuleSet, List<String> alphabet, Set<String> containsWords) { - List<String> words = new LinkedList<>(); + JSONArray words = new JSONArray(); int i = 0; long startTime = System.currentTimeMillis(); long elapsedTime = 0; @@ -130,33 +146,33 @@ public class Writer { if (percent <= 0.2) { word = wg.wordGenerator(MAX_LENGTH); if (!containsWords.contains(word)) { - words.add(word); + words.add(new JSONString(word)); containsWords.add(word); i++; } } else if (percent <= 0.5) { word = wg.wordGeneratorOverAlphabet(MAX_LENGTH, alphabet); if (!containsWords.contains(word)) { - words.add(word); + words.add(new JSONString(word)); containsWords.add(word); i++; } } else { word = wg.wordGeneratorOverType2Grammar(MAX_LENGTH, normalizedRuleSet); if (!containsWords.contains(word)) { - words.add(word); + words.add(new JSONString(word)); containsWords.add(word); i++; } } elapsedTime = (new Date()).getTime() - startTime; } - Collections.shuffle(words); + JSONArray.shuffle(words); return words; } - private List<String> wordGeneratorType3(FiniteStateMachine fsm, List<String> alphabet, Set<String> containsWords) { - List<String> words = new LinkedList<>(); + private JSONArray wordGeneratorType3(FiniteStateMachine fsm, List<String> alphabet, Set<String> containsWords) { + JSONArray words = new JSONArray(); int i = 0; long startTime = System.currentTimeMillis(); long elapsedTime = 0; @@ -170,34 +186,34 @@ public class Writer { if (percent <= 0.2) { word = wg.wordGenerator(MAX_LENGTH); if (!containsWords.contains(word)) { - words.add(word); + words.add(new JSONString(word)); containsWords.add(word); i++; } } else if (percent <= 0.5) { word = wg.wordGeneratorOverAlphabet(MAX_LENGTH, alphabet); if (!containsWords.contains(word)) { - words.add(word); + words.add(new JSONString(word)); containsWords.add(word); i++; } } else { word = wg.wordGeneratorOverType3Grammar(MAX_LENGTH, fsm, alphabet); if (!containsWords.contains(word)) { - words.add(word); + words.add(new JSONString(word)); containsWords.add(word); i++; } } elapsedTime = (new Date()).getTime() - startTime; } - Collections.shuffle(words); + JSONArray.shuffle(words); return words; } public void runType3Grammar(List<String> vars, List<String> alphabet, int maxConclusios, String start, int mode, int n) { - List<String> grammarWordsPairs = new LinkedList<>(); + JSONObject grammarWordsPairs = new JSONObject(); List<Map<String, List<List<String>>>> ruleSets = new LinkedList<>(); List<FiniteStateMachine> fsms = new LinkedList<>(); List<String> words = new LinkedList<>(); @@ -210,12 +226,17 @@ public class Writer { ruleSets.add(g.getRules()); FiniteStateMachine fsm = t3p.grammarToFSM(g.getRules(), g.getStart()); fsms.add(fsm); - List<String> wordsPerGrammar = wordGeneratorType3(fsm, alphabet, containsWords); - grammarWordsPairs.add(createGrammarWordsPair(g.toStringPython(), wordsPerGrammar)); - words.addAll(wordsPerGrammar); + JSONArray wordsPerGrammar = wordGeneratorType3(fsm, alphabet, containsWords); + grammarWordsPairs.put(g.toStringPython(), wordsPerGrammar); + for (JSONDatatype js : wordsPerGrammar) { + if (js instanceof JSONString) { + JSONString word = (JSONString) js; + words.add(word.getString()); + } + } } writeResultToFileType3(fsms, words); - writePairsToFile(grammarWordsPairs); + writePairsToFile(grammarWordsPairs, 3); } } -- GitLab