Skip to content

Commit

Permalink
most of analyser working, formatting issues due to depreciated webser…
Browse files Browse the repository at this point in the history
…vice
  • Loading branch information
Deren Vural committed May 1, 2020
1 parent d624af5 commit fd76cf8
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 221 deletions.
9 changes: 3 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ android {
dependencies {
// JAR files
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation fileTree(include: ['*.jar'], dir: 'libs')
// Google
implementation 'com.google.android.material:material:1.1.0'
implementation 'com.google.android.gms:play-services-auth:18.0.0'

// AndroidX
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
Expand All @@ -37,18 +35,17 @@ dependencies {
implementation 'androidx.navigation:navigation-ui:2.2.2'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.annotation:annotation:1.1.0'

// Testing
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

// Firebase
implementation 'com.google.firebase:firebase-invites:17.0.0'
implementation 'com.google.firebase:firebase-auth:19.3.1'
implementation 'com.google.firebase:firebase-firestore:21.4.3'
implementation 'com.google.firebase:firebase-storage:19.1.1'

// Volley
implementation 'com.android.volley:volley:1.1.1'
// ui4j webkit
implementation files('libs/ui4j-webkit-4.0.0.jar')
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;

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

import java.lang.ref.WeakReference;
Expand All @@ -26,6 +28,11 @@ public class analyser {
private scraperAsyncTask task;
private WeakReference<SourceReadActivity> context;
private fdatabase db;
private JSONObject jsonAIF;
private MutableLiveData<Article> article;
// Constants
private static final String converterURL = "http://ws.arg.tech/m/turninator";
private static final String analyserURL = "http://ws.arg.tech/m/inference_identifier";

public analyser(SourceReadActivity owner, fdatabase db) {
// Set database
Expand All @@ -36,14 +43,18 @@ public analyser(SourceReadActivity owner, fdatabase db) {

// Set activity
context = new WeakReference<SourceReadActivity>(owner);

// Set article
article = new MutableLiveData<Article>(null);
}

public void fetch_article(final Article article, Observer<Boolean> observer) {
public void fetch_article(final Article newArticle, Observer<Boolean> observer) {
// Inform user of progress
Toast.makeText(context.get(), "Scraping article content..", Toast.LENGTH_SHORT).show();

// execute async task
task.execute(article);
article.setValue(newArticle);
task.execute(article.getValue());

// Check for task finish
task.getDone().observe(context.get(), observer);
Expand All @@ -52,14 +63,13 @@ public void fetch_article(final Article article, Observer<Boolean> observer) {
@Override
public void onChanged(Boolean done) {
if (done) {
// Convert to JSON-AIF
convertToJSONAIF(article);

// Analyse the JSON-AIF
analyse(article);
// Set live data
article.setValue(task.getData().getValue());

// Update database
update_database(article);
if(article != null && article.getValue() != null) {
// Convert to JSON-AIF
convertToJSONAIF();
}
}
}
});
Expand All @@ -79,68 +89,38 @@ public void onChanged(Boolean done) {
*
*
**/
private void convertToJSONAIF(Article article) {
// Get URL
final String url = "http://ws.arg.tech/m/turninator";

// Get JSON-AIF
String data = article.getText();
String[] sentences = data.split("\\.");
HashMap[] nodes = new HashMap[sentences.length];
HashMap[] locutions = new HashMap[sentences.length];
String[] edges = new String[(sentences.length * 2) - 2];
for (int i = 0; i < sentences.length; i++) {
HashMap<String, Object> locution = new HashMap<String, Object>();
HashMap<String, Object> info = new HashMap<String, Object>();

locution.put("text", sentences[i]);
locution.put("type", "L");
locution.put("nodeID", i);
locution.put("timestamp", i);

nodes[i] = locution;

info.put("personID", 0);
info.put("nodeID", i);
info.put("timestamp", i);

locutions[i] = info;
/*
if(i == 0){
// 1 edge, latter
edges[i] = i - 1;
}else if (i == (sentences.length - 1)){
// 1 edge, former
}else{
// 2 edges
}*/
}

// Add JSON parameters
HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put("nodes", nodes);
parameters.put("edges", edges);
parameters.put("text", data);
parameters.put("locutions", locutions);
private void convertToJSONAIF() {
// Get body text & clean
String data = article.getValue().getText();

// Inform user of progress
Toast.makeText(context.get(), "Analysing..", Toast.LENGTH_SHORT).show();

// Make https POST request
context.get().getHttpHandler().argtech_request_post(url, parameters,
new Response.Listener<JSONObject>() {
context.get().getHttpHandler().argtech_request_post(converterURL, data,
new Response.Listener<String>() {
@Override
public void onResponse(JSONObject response) {
public void onResponse(String response) {
// Cut token out of html response
Log.d("ARG-TECH", "Webservice POST Request Response received");

//try {
//
Iterator<String> q = response.keys();
Toast.makeText(context.get(), "Analysis complete..", Toast.LENGTH_SHORT).show();
//} catch (JSONException error) {
// Log.e("JSON error", "error reading JSON: " + error.getMessage());
//}
try {
// Attempt to create the JSON object response
jsonAIF = new JSONObject(response);

// Put response into article
Article newArticle = article.getValue();
newArticle.setAif(jsonAIF.toString());
article.setValue(newArticle);

// Notify user
Toast.makeText(context.get(), "Conversion to AIF complete..", Toast.LENGTH_SHORT).show();

// Attempt analysis on json-aif
analyse();
} catch (JSONException error) {
Log.e("JSON error", "error reading JSON: " + error.getMessage());
}
}
},
new Response.ErrorListener() {
Expand All @@ -156,68 +136,38 @@ public void onErrorResponse(VolleyError error) {
/*
* Make POST requests to ARG-tech webservices for analysis of sent JSON-AIF
**/
private void analyse(Article article) {
// Get URL
final String url = "http://ws.arg.tech/m/inference_identifier";

private void analyse() {
// Get JSON-AIF
String data = article.getText();
String[] sentences = data.split("\\.");
HashMap[] nodes = new HashMap[sentences.length];
HashMap[] locutions = new HashMap[sentences.length];
String[] edges = new String[(sentences.length * 2) - 2];
for (int i = 0; i < sentences.length; i++) {
HashMap<String, Object> locution = new HashMap<String, Object>();
HashMap<String, Object> info = new HashMap<String, Object>();

locution.put("text", sentences[i]);
locution.put("type", "L");
locution.put("nodeID", i);
locution.put("timestamp", i);

nodes[i] = locution;

info.put("personID", 0);
info.put("nodeID", i);
info.put("timestamp", i);

locutions[i] = info;
/*
if(i == 0){
// 1 edge, latter
edges[i] = i - 1;
}else if (i == (sentences.length - 1)){
// 1 edge, former
}else{
// 2 edges
}*/
}

// Add JSON parameters
HashMap<String, Object> parameters = new HashMap<String, Object>();
parameters.put("nodes", nodes);
parameters.put("edges", edges);
parameters.put("text", data);
parameters.put("locutions", locutions);
String data = article.getValue().getAif();

// Inform user of progress
Toast.makeText(context.get(), "Analysing..", Toast.LENGTH_SHORT).show();

// Make https POST request
context.get().getHttpHandler().make_volley_request_post(url, parameters,
new Response.Listener<JSONObject>() {
context.get().getHttpHandler().argtech_request_post(analyserURL, data,
new Response.Listener<String>() {
@Override
public void onResponse(JSONObject response) {
public void onResponse(String response) {
// Cut token out of html response
Log.d("ARG-TECH", "Webservice POST Request Response received");

//try {
//
Iterator<String> q = response.keys();
Toast.makeText(context.get(), "Analysis complete..", Toast.LENGTH_SHORT).show();
//} catch (JSONException error) {
// Log.e("JSON error", "error reading JSON: " + error.getMessage());
//}
try {
// Attempt to create the JSON object response
jsonAIF = new JSONObject(response);

// Put response into article
Article newArticle = article.getValue();
newArticle.setAif(jsonAIF.toString());
article.setValue(newArticle);

// Notify user
Toast.makeText(context.get(), "Argument Structure analysis complete..", Toast.LENGTH_SHORT).show();

// Update database
update_database();
} catch (JSONException error) {
Log.e("JSON error", "error reading JSON: " + error.getMessage());
}
}
},
new Response.ErrorListener() {
Expand All @@ -231,18 +181,20 @@ public void onErrorResponse(VolleyError error) {
}


private void update_database(final Article article) {
// Save analysis to database
db.update_article_field(article, "veracity",new OnCompleteListener<Void>(){
@Override
public void onComplete (@NonNull Task<Void> veracityTask) {
if (veracityTask.isSuccessful()) {
Log.d("DB", "updated article - '" + article.getDatabase_id() + "' field - 'veracity'");
} else {
// Log error
Log.e("DB", "update failed: ", veracityTask.getException());
private void update_database() {
if(article != null && article.getValue() != null) {
// Save analysis to database
db.update_article_field(article.getValue(), "aif", new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> aifTask) {
if (aifTask.isSuccessful()) {
Toast.makeText(context.get(), "Article '" + article.getValue().getTitle() + "' analysis saved to database!", Toast.LENGTH_SHORT).show();
} else {
// Log error
Log.e("DB", "update failed: ", aifTask.getException());
}
}
}
});
});
}
}
}
Loading

0 comments on commit fd76cf8

Please sign in to comment.