matrix.CreateTextMatrix.java Source code

Java tutorial

Introduction

Here is the source code for matrix.CreateTextMatrix.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package matrix;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author nSabri
 */
public class CreateTextMatrix {

    private String jsonInputPath;
    private String cvsOutputPath;

    public void textMatrix()
            throws UnsupportedEncodingException, FileNotFoundException, IOException, ParseException {

        CosSim cossim = new CosSim();
        JSONParser jParser = new JSONParser();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                new FileInputStream("/Users/nSabri/Desktop/tweetMatris/userTweets.json"), "ISO-8859-9"));
        JSONArray a = (JSONArray) jParser.parse(in);
        File fout = new File("/Users/nSabri/Desktop/tweetMatris.csv");
        FileOutputStream fos = new FileOutputStream(fout);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

        for (int i = 0; i < 1000; i++) {

            for (int j = 0; j < 1000; j++) {

                JSONObject tweet1 = (JSONObject) a.get(i);
                JSONObject tweet2 = (JSONObject) a.get(j);
                String tweetText1 = tweet1.get("tweets").toString();
                String tweetText2 = tweet2.get("tweets").toString();

                double CosSimValue = cossim.Cosine_Similarity_Score(tweetText1, tweetText2);
                CosSimValue = Double.parseDouble(new DecimalFormat("##.###").format(CosSimValue));
                bw.write(Double.toString(CosSimValue) + ", ");

            }
            bw.newLine();
        }
        bw.close();

    }

    public String getJsonInputPath() {
        return jsonInputPath;
    }

    public void setJsonInputPath(String jsonInputPath) {
        this.jsonInputPath = jsonInputPath;
    }

    public String getCvsOutputPath() {
        return cvsOutputPath;
    }

    public void setCvsOutputPath(String cvsOutputPath) {
        this.cvsOutputPath = cvsOutputPath;
    }
}