matrix.TextUrlMatrix.java Source code

Java tutorial

Introduction

Here is the source code for matrix.TextUrlMatrix.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 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 TextUrlMatrix {

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

        double a = 0.7;
        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 jArray = (JSONArray) jParser.parse(in);
        BufferedReader in2 = new BufferedReader(new InputStreamReader(
                new FileInputStream("/Users/nSabri/Desktop/tweetMatris/userTweetsUrls.json"), "ISO-8859-9"));
        JSONArray jArray2 = (JSONArray) jParser.parse(in2);
        File fout = new File("/Users/nSabri/Desktop/textUrlMatris.csv");
        FileOutputStream fos = new FileOutputStream(fout);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

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

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

                JSONObject tweet1text = (JSONObject) jArray.get(i);
                JSONObject tweet2text = (JSONObject) jArray.get(j);
                JSONObject tweet1url = (JSONObject) jArray2.get(i);
                JSONObject tweet2url = (JSONObject) jArray2.get(j);
                String tweetText1 = tweet1text.get("tweets").toString();
                String tweetText2 = tweet2text.get("tweets").toString();
                String tweetUrl1 = tweet1url.get("title").toString() + tweet1url.get("meta").toString();
                String tweetUrl2 = tweet2url.get("title").toString() + tweet1url.get("meta").toString();

                double CosSimValueText = cossim.Cosine_Similarity_Score(tweetText1, tweetText2);

                double CosSimValueUrl = cossim.Cosine_Similarity_Score(tweetUrl1, tweetUrl2);

                double TextUrlSimValue = (a * CosSimValueText) + ((1 - a) * CosSimValueUrl);

                TextUrlSimValue = Double.parseDouble(new DecimalFormat("##.###").format(TextUrlSimValue));

                bw.write(Double.toString(TextUrlSimValue) + ", ");

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

    }

}