Skip to content

Commit

Permalink
total parsing fix for reorganised data setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
rohankulkz committed May 2, 2024
1 parent 48a73ed commit d1c8f9e
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.text.Html;
import android.text.Layout;
import android.text.TextUtils;
import android.util.JsonReader;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
Expand All @@ -40,10 +41,14 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -487,14 +492,39 @@ public void setNSPWhichPage() {
// Creates an NSP Does page, called by SET_NSP_DOES_PAGE
// Assigns the sentence to be shown on the screen
@Override
public void setNSPDoesPage() {
public void setNSPDoesPage() throws IOException, JSONException {

InputStream loaded = JSON_Helper.assetManager().open("/robotutor_assets/assets/story/sw/nsp/story/story_40/nsp.json");



StringBuilder textBuilder = new StringBuilder();
try (Reader reader = new BufferedReader(new InputStreamReader
(loaded, StandardCharsets.UTF_8))) {
int c = 0;
while ((c = reader.read()) != -1) {
textBuilder.append((char) c);
}
}

JSONObject newOBJ = new JSONObject();

try {
newOBJ = new JSONObject(textBuilder.toString());
}catch (JSONException err){
Log.d("Error", err.toString());
}

NSPQuestion nspQuestions = new NSPQuestion(newOBJ);



if(nsp_does_mode && isNSPDoesPage) {
for(int i = 0; i < NSPQuestion.choices.size(); i++) {
if (NSPQuestion.choices.get(i).type.equals("correct")) {
NSPDoesCorrect = NSPQuestion.choices.get(i).text;
} if (NSPQuestion.choices.get(i).type.equals(nsp_choice_type)) {
NSPDoesDistractor = NSPQuestion.choices.get(i).text;
for(int i = 0; i < nspQuestions.choices.size(); i++) {
if (nspQuestions.choices.get(i).type.equals("correct")) {
NSPDoesCorrect = nspQuestions.choices.get(i).text;
} if (nspQuestions.choices.get(i).type.equals(nsp_choice_type)) {
NSPDoesDistractor = nspQuestions.choices.get(i).text;
// NSPSentenceIndex = NSPQuestion.choices.get(i).index;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

package cmu.xprize.comp_questions;

import org.json.JSONException;

import java.io.IOException;

import cmu.xprize.util.ILoadableObject;
import edu.cmu.xprize.listener.ListenerBase;

Expand Down Expand Up @@ -168,7 +172,7 @@ public interface ICQn_ViewManager extends ILoadableObject {

void setNSPDoesQuestion();

void setNSPDoesPage();
void setNSPDoesPage() throws IOException, JSONException;

void hasNSPDistractor();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ public class NSPChoice implements ILoadableObject{
public String text;
public Double coherence;

public NSPChoice(String type, int index, String text, Double coherence){
this.type = type;
this.index = index;
this.text = text;
this.coherence = coherence;
};

@Override
public void loadJSON(JSONObject jsonObj, IScope scope) {
try{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cmu.xprize.comp_questions;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cmu.xprize.util.ILoadableObject;
Expand All @@ -15,14 +18,45 @@
public class NSPQuestion implements ILoadableObject {
// json loadable
public NSPContextSentence context_sentence;
public NSPChoice choices;

public ArrayList<NSPChoice> choices;


public NSPQuestion(JSONObject jsonObject) throws JSONException {

JSONArray arr = jsonObject.getJSONArray("choices");

for (int i = 0; i < arr.length(); i++) {

JSONObject jsonObject1 = arr.getJSONObject(i);

NSPChoice choice = new NSPChoice(jsonObject1.getString("type"),
i,
jsonObject1.getString("text"),
1.4
);


choices.add(choice);
}


}



// //todo choices is not a list and cannot be accessed as one. You must create an accumulated list of all nsp choices to iterate through NSP options.
// @Override
// public void loadJSON(JSONObject jsonObj, IScope scope) {
// JSON_Helper.parseSelf(jsonObj, this, CClassMap.classMap, scope);
//
// }
//todo choices is not a list and cannot be accessed as one. You must create an accumulated list of all nsp choices to iterate through NSP options.
@Override
public void loadJSON(JSONObject jsonObj, IScope scope) {
public void loadJSON(JSONObject jsonObj, IScope scope) throws JSONException {
JSON_Helper.parseSelf(jsonObj, this, CClassMap.classMap, scope);

}


}
}
3 changes: 2 additions & 1 deletion util/src/main/java/cmu/xprize/util/ILoadableObject.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cmu.xprize.util;

import org.json.JSONException;
import org.json.JSONObject;


public interface ILoadableObject {
public void loadJSON(JSONObject jsonObj, IScope scope);
public void loadJSON(JSONObject jsonObj, IScope scope) throws JSONException;
}

0 comments on commit d1c8f9e

Please sign in to comment.