Created
November 3, 2015 04:52
-
-
Save nihanthd/60c0eda3d01d3b7fdb11 to your computer and use it in GitHub Desktop.
Code to parse and pre-process the JSON file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Attributes { | |
public static void main(String[] args) throws IOException { | |
BufferedReader br = new BufferedReader(new FileReader(args[0])); | |
String line = ""; | |
JsonParser jsonParser = new JsonParser(); | |
BufferedWriter output = | |
new BufferedWriter(new FileWriter(new File(args[1]))); | |
HashSet<String> ratings = new HashSet<String>(); | |
while((line = br.readLine()) != null){ | |
JsonElement json = jsonParser.parse(line); | |
JsonObject review = json.getAsJsonObject(); | |
JsonObject newReview = new JsonObject(); | |
Set<Map.Entry<String, JsonElement>> entries = review.entrySet(); | |
for (Map.Entry<String, JsonElement> entry: entries) { | |
if(entry.getKey().toString().equalsIgnoreCase("stars")) | |
{ | |
ratings.add(entry.getValue().toString()); | |
newReview.add(entry.getKey().toString(), entry.getValue()); | |
} | |
else if( entry.getKey().toString().equalsIgnoreCase("text")) | |
{ | |
newReview.add(entry.getKey().toString(), entry.getValue()); | |
} | |
output.write(newReview.toString()+"\n"); | |
} | |
} | |
output.write("Possible ratings are : "); | |
for(String rating: ratings){ | |
output.write(rating + " " ); | |
} | |
br.close(); | |
output.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment