Skip to content

Instantly share code, notes, and snippets.

@nihanthd
Created November 3, 2015 04:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nihanthd/60c0eda3d01d3b7fdb11 to your computer and use it in GitHub Desktop.
Save nihanthd/60c0eda3d01d3b7fdb11 to your computer and use it in GitHub Desktop.
Code to parse and pre-process the JSON file
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