Search in sources :

Example 6 with JSONObject

use of json.JSONObject in project IR_Base by Linda-sunshine.

the class DocAnalyzer method LoadJSON.

protected JSONObject LoadJSON(String filename) {
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF-8"));
        StringBuffer buffer = new StringBuffer(1024);
        String line;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        reader.close();
        return new JSONObject(buffer.toString());
    } catch (Exception e) {
        System.out.print('X');
        return null;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) JSONObject(json.JSONObject) BufferedReader(java.io.BufferedReader) FileInputStream(java.io.FileInputStream) JSONException(json.JSONException) InvalidFormatException(opennlp.tools.util.InvalidFormatException) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 7 with JSONObject

use of json.JSONObject in project IR_Base by Linda-sunshine.

the class HTSMAnalyzer method LoadNewEggDoc.

// Load a document and analyze it.
public void LoadNewEggDoc(String filename) {
    JSONObject prod = null;
    String item;
    JSONArray itemIds, reviews;
    try {
        JSONObject json = LoadJSON(filename);
        prod = json.getJSONObject(m_category);
        itemIds = prod.names();
        System.out.printf("Under %s category, Number of Items: %d\n", m_category, itemIds.length());
    } catch (Exception e) {
        System.out.print('X');
        return;
    }
    for (int i = 0; i < itemIds.length(); i++) {
        try {
            item = itemIds.getString(i);
            reviews = prod.getJSONArray(item);
            for (int j = 0; j < reviews.length(); j++) {
                if (this.m_stnDetector != null)
                    AnalyzeNewEggPostWithSentence(new _NewEggPost(reviews.getJSONObject(j), item));
                else
                    AnalyzeNewEggPost(new _NewEggPost(reviews.getJSONObject(j), item));
            }
        } catch (JSONException e) {
            System.out.print('P');
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
Also used : JSONObject(json.JSONObject) JSONArray(json.JSONArray) structures._NewEggPost(structures._NewEggPost) JSONException(json.JSONException) ParseException(java.text.ParseException) JSONException(json.JSONException) IOException(java.io.IOException) InvalidFormatException(opennlp.tools.util.InvalidFormatException) FileNotFoundException(java.io.FileNotFoundException) ParseException(java.text.ParseException)

Example 8 with JSONObject

use of json.JSONObject in project IR_Base by Linda-sunshine.

the class MultiThreadedLMAnalyzer method LoadJsonDoc.

// Load a document and analyze it.
@Override
public void LoadJsonDoc(String filename) {
    _Product prod = null;
    JSONArray jarray = null;
    try {
        JSONObject json = LoadJSON(filename);
        prod = new _Product(json.getJSONObject("asin"));
        jarray = json.getJSONArray("related");
    } catch (Exception e) {
        // fail to parse a json document
        System.err.print('X');
        return;
    }
    for (int i = 0; i < jarray.length(); i++) {
        try {
            _Post post = new _Post(jarray.getJSONObject(i));
            if (post.isValid(m_dateFormatter)) {
                long timeStamp = m_dateFormatter.parse(post.getDate()).getTime();
                String content;
                // append document title into document content
                if (Utils.endWithPunct(post.getTitle()))
                    content = post.getTitle() + " " + post.getContent();
                else
                    content = post.getTitle() + ". " + post.getContent();
                // int ID, String name, String prodID, String title, String source, int ylabel, long timeStamp
                _Doc review = new _Doc(m_corpus.getSize(), post.getID(), prod.getID(), post.getTitle(), content, post.getLabel() - 1, timeStamp);
                if (this.m_stnDetector != null)
                    AnalyzeDocWithStnSplit(review);
                else
                    AnalyzeDoc(review);
            }
        } catch (ParseException e) {
            System.out.print('T');
        } catch (JSONException e) {
            System.out.print('P');
        }
    }
}
Also used : JSONObject(json.JSONObject) structures._Doc(structures._Doc) structures._Product(structures._Product) JSONArray(json.JSONArray) JSONException(json.JSONException) ParseException(java.text.ParseException) JSONException(json.JSONException) InvalidFormatException(opennlp.tools.util.InvalidFormatException) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) structures._Post(structures._Post)

Example 9 with JSONObject

use of json.JSONObject in project IR_Base by Linda-sunshine.

the class ParentChildAnalyzer method loadParentDoc.

public void loadParentDoc(String fileName) {
    if (fileName == null || fileName.isEmpty())
        return;
    JSONObject json = LoadJSON(fileName);
    String title = Utils.getJSONValue(json, "title");
    String content = Utils.getJSONValue(json, "content");
    String name = Utils.getJSONValue(json, "name");
    String[] sentences = null;
    // _ParentDoc d = new _ParentDoc(m_corpus.getSize(), name, title,
    // content, 0);
    _ParentDoc d = new _ParentDoc4DCM(m_corpus.getSize(), name, title, content, 0);
    try {
        JSONArray sentenceArray = json.getJSONArray("sentences");
        sentences = new String[sentenceArray.length()];
        // shall we add title into this sentence array
        for (int i = 0; i < sentenceArray.length(); i++) sentences[i] = Utils.getJSONValue(sentenceArray.getJSONObject(i), "sentence");
        if (AnalyzeDocByStn(d, sentences))
            parentHashMap.put(name, d);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
Also used : JSONObject(json.JSONObject) structures._ParentDoc(structures._ParentDoc) structures._ParentDoc4DCM(structures._ParentDoc4DCM) JSONArray(json.JSONArray) JSONException(json.JSONException)

Example 10 with JSONObject

use of json.JSONObject in project IR_Base by Linda-sunshine.

the class _Post method getJSON.

public JSONObject getJSON() throws JSONException {
    JSONObject json = new JSONObject();
    // must contain
    json.put("postID", m_ID);
    // must contain
    json.put("author", m_author);
    // must contain
    json.put("authorID", m_authorID);
    // might be missing
    json.put("replyTo", m_replyToID);
    // must contain
    json.put("date", m_date);
    // might be missing
    json.put("title", m_title);
    // must contain
    json.put("content", m_content);
    return json;
}
Also used : JSONObject(json.JSONObject)

Aggregations

JSONObject (json.JSONObject)8 JSONException (json.JSONException)5 FileNotFoundException (java.io.FileNotFoundException)4 IOException (java.io.IOException)4 ParseException (java.text.ParseException)4 JSONArray (json.JSONArray)4 InvalidFormatException (opennlp.tools.util.InvalidFormatException)4 JSONObject (org.apache.tapestry5.json.JSONObject)3 structures._Doc (structures._Doc)3 Test (org.junit.Test)2 structures._ParentDoc (structures._ParentDoc)2 structures._Post (structures._Post)2 structures._Product (structures._Product)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 BeginRender (org.apache.tapestry5.annotations.BeginRender)1 structures._ChildDoc (structures._ChildDoc)1 structures._NewEggPost (structures._NewEggPost)1 structures._ParentDoc4DCM (structures._ParentDoc4DCM)1