package test;
import java.io.IOException;
import java.io.StringReader;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.json.*;
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
public class Test2 {
public static void main(String[] args) throws IOException {
Test2 t2= new Test2();
String jsonstring = t2.ReadFile("dataset4.json");
t2.parse_json(jsonstring);
/*
String text="白雪公主和七个小矮人";
Analyzer anal=new IKAnalyzer(true);
StringReader reader=new StringReader(text);
TokenStream ts=anal.tokenStream("", reader);
CharTermAttribute term=ts.getAttribute(CharTermAttribute.class);
while(ts.incrementToken()){
System.out.print(term.toString()+"|");
}
anal.close();
reader.close();
System.out.println();
*/
}
public void parse_json(String jsonString) throws IOException{
try {
//String jsonString = "{\"result\":[{\"Image\":{\"Width\":800,\"Height\":600,\"Title\":\"View from 15th Floor\",\"Thumbnail\":{\"Url\":\"http://www.example.com/image/481989943\",\"Height\":125,\"Width\":\"100\"},\"IDs\":[116, 943, 234, 38793]}},{\"Image\":{\"Width\":800,\"Height\":1000,\"Title\":\"View from 15th Floor\",\"Thumbnail\":{\"Url\":\"http://www.example.com/image/481989943\",\"Height\":125,\"Width\":\"100\"},\"IDs\":[116, 943, 234, 38793]}}]}";
//String jsonString2= "{\"result\":[{\"_count\": 54,\"id\": \"100\", \"_name\": \"搞笑的公路电影\"}, {\"_count\": 54, \"id\": \"101\", \"_name\": \"根据真实故事改编的爱情片\"}]}";
JSONObject jsonObject = new JSONObject(jsonString.trim());
JSONArray jsonar = jsonObject.getJSONArray("result");
/*
for(int i = 0;i<jsonar.length();i++)
{
JSONObject jsob = jsonar.getJSONObject(i).getJSONObject("Image");
String _name = jsob.getString("Height");
System.out.println("length:" + jsonar.length());
System.out.println("Height:" + _name);
}
*/
Map<String,Integer> jsomap = new HashMap<String,Integer>();
for(int i = 0;i<jsonar.length();i++)
{
JSONObject jsob = jsonar.getJSONObject(i);
String _name = jsob.getString("_name");
//System.out.println("length:" + jsonar.length());
//System.out.println("_name:" + _name);
Analyzer anal=new IKAnalyzer(true);
StringReader reader=new StringReader(_name);
TokenStream ts=anal.tokenStream("", reader);
CharTermAttribute term=ts.getAttribute(CharTermAttribute.class);
while(ts.incrementToken()){
String temp =term.toString();
System.out.print(term.toString()+"|");
if((jsomap.containsKey(temp)))
{
//System.out.println("存在此key:"+temp+"value: "+jsomap.get(temp));
int m = jsomap.get(temp);
//System.out.println("m的值: "+m);
jsomap.put(temp, ++m);
}
else
{
//System.out.println("不存在此key:"+temp+"value: "+jsomap.get(temp));
jsomap.put(temp, 1);
}
}
anal.close();
reader.close();
System.out.println();
}
for (Iterator i = jsomap.keySet().iterator(); i.hasNext();) {
Object obj = i.next();
// System.out.println(obj);// 循环输出key
System.out.println("key=" + obj + " value=" + jsomap.get(obj));
//System.out.println("num"+jsomap.size());
}
/*
String _name = jsonObject.getString("_name");
System.out.print("_name: "+_name);
JSONObject jsonObjectImage = jsonObject.getJSONObject("Image");
String width = jsonObjectImage.getString("Width");
String height = jsonObjectImage.getString("Height");
String title = jsonObjectImage.getString("Title");
System.out.println("WIDTH:" + width);
System.out.println("HEIGHT:" + height);
System.out.println("TITLE:" + title);
JSONObject jsonObjectTHU = jsonObjectImage.getJSONObject("Thumbnail");
String url = jsonObjectTHU.getString("Url");
String width2 = jsonObjectTHU.getString("Width");
String height2 = jsonObjectTHU.getString("Height");
System.out.println("URL:" + url);
System.out.println("WIDTH2:" + width2);
System.out.println("HEIGHT2:" + height2);
JSONArray jsonArrayIDS = jsonObjectImage.getJSONArray("IDs");
for(int i=0;i<jsonArrayIDS.length();i++){
System.out.println("元素" + i + ":" + jsonArrayIDS.getInt(i));
}*/
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 读文件,返回字符串
* @param path
* @return
*/
public String ReadFile(String path){
File file = new File(path);
BufferedReader reader = null;
String laststr = "";
try {
//System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
//一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// System.out.println("line " +line+ ": " + tempString+"\n");
laststr = laststr + tempString;
//line++ ;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return laststr;
}
}