public void buildIndex(JSONObject indexData) {

    try {
      Directory dir = FSDirectory.open(new File(indexDir));
      IKAnalyzer analyzer = new IKAnalyzer();
      analyzer.setUseSmart(true);
      IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_35, analyzer);
      indexWriter = new IndexWriter(dir, iwc);
      indexWriter.deleteAll();

      JSONArray statusData = indexData.getJSONArray("statusData");
      for (int i = 0; i < statusData.length(); i++) {
        String text = statusData.getString(i);
        Document doc = new Document();
        doc.add(
            new Field(
                "text",
                text,
                Field.Store.YES,
                Field.Index.ANALYZED,
                Field.TermVector.WITH_POSITIONS_OFFSETS));
        indexWriter.addDocument(doc);
      }

      JSONArray userData = indexData.getJSONArray("userData");
      for (int i = 0; i < userData.length(); i++) {
        String text = userData.getString(i);
        Document doc = new Document();
        doc.add(
            new Field(
                "text",
                text,
                Field.Store.YES,
                Field.Index.ANALYZED,
                Field.TermVector.WITH_POSITIONS_OFFSETS));
        indexWriter.addDocument(doc);
      }
      // indexWriter.commit();
      System.out.println("Index is done");
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (JSONException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      try {
        indexWriter.close();
      } catch (CorruptIndexException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
  @SuppressWarnings("finally")
  public JSONObject getKeyWords(JSONObject semiData) {
    JSONObject keyWords = new JSONObject();

    try {
      this.buildIndex(semiData);
      this.getIndexInfo(this.indexDir, 4);
      this.generateWekaFile(this.termList, this.maxDocNum, this.wekaFile);
      JSONArray array = this.Cluster(this.wekaFile, 7);
      int totalNum = 0;
      for (int i = 0; i < array.length(); i++) {
        totalNum += array.getJSONArray(i).length();
      }
      keyWords.put("maxFreq", this.maxFreq);
      keyWords.put("totalNum", totalNum);
      keyWords.put("WordList", array);
    } catch (WeiboException e) {
      System.out.print("getKeyWords++++++weibo\n");
      System.out.print("error:" + e.getError() + "toString:" + e.toString());
      keyWords.put("error", e.getError());
      e.printStackTrace();
    } catch (Exception e) {
      System.out.print("getKeyWords++++++Exception");
      keyWords.put("error", e.toString());
      e.printStackTrace();
    } finally {
      try {
        this.myDelete(this.indexDir);
        this.myDelete(this.wekaFile);
      } catch (Exception e) {
        e.printStackTrace();
      }
      return keyWords;
    }
  }
 /**
  * @param res
  * @return
  * @throws WeiboException
  */
 static List<User> constructResult(Response res) throws WeiboException {
   JSONArray list = res.asJSONArray();
   try {
     int size = list.length();
     List<User> users = new ArrayList<User>(size);
     for (int i = 0; i < size; i++) {
       users.add(new User(list.getJSONObject(i)));
     }
     return users;
   } catch (JSONException e) {
   }
   return null;
 }
Exemple #4
0
 static List<Count> constructCounts(Response res) throws WeiboException {
   try {
     JSONArray list = res.asJSONArray();
     int size = list.length();
     List<Count> counts = new ArrayList<Count>(size);
     for (int i = 0; i < size; i++) {
       counts.add(new Count(list.getJSONObject(i)));
     }
     return counts;
   } catch (JSONException jsone) {
     throw new WeiboException(jsone);
   } catch (WeiboException te) {
     throw te;
   }
 }
Exemple #5
0
 public static List<FavoritesTag> constructTag(Response res) throws WeiboException {
   try {
     JSONArray list = res.asJSONObject().getJSONArray("tags");
     int size = list.length();
     List<FavoritesTag> tags = new ArrayList<FavoritesTag>(size);
     for (int i = 0; i < size; i++) {
       tags.add(new FavoritesTag(list.getJSONObject(i)));
     }
     return tags;
   } catch (JSONException jsone) {
     throw new WeiboException(jsone);
   } catch (WeiboException te) {
     throw te;
   }
 }
 public static List<User> constructUsers(Response res) throws WeiboException {
   try {
     JSONArray list = res.asJSONArray();
     int size = list.length();
     List<User> users = new ArrayList<User>(size);
     for (int i = 0; i < size; i++) {
       users.add(new User(list.getJSONObject(i)));
     }
     return users;
   } catch (JSONException jsone) {
     throw new WeiboException(jsone);
   } catch (WeiboException te) {
     throw te;
   }
 }
 public static List<SchoolSearch> constructSchoolSearch(Response res) throws WeiboException {
   try {
     JSONArray list = res.asJSONArray();
     int size = list.length();
     List<SchoolSearch> schools = new ArrayList<SchoolSearch>(size);
     for (int i = 0; i < size; i++) {
       schools.add(new SchoolSearch(list.getJSONObject(i)));
     }
     return schools;
   } catch (JSONException jsone) {
     throw new WeiboException(jsone);
   } catch (WeiboException te) {
     throw te;
   }
 }
Exemple #8
0
 public MentionsIds(JSONObject json) throws WeiboException {
   try {
     nextCursor = json.getLong("next_cursor");
     previousCursor = json.getLong("previous_cursor");
     totalNumber = json.getLong("total_number");
     hasvisible = json.getBoolean("hasvisible");
     interval = json.getLong("interval");
     JSONArray list = json.getJSONArray("statuses");
     int size = list.length();
     statusesIds = new ArrayList<String>(size);
     for (int i = 0; i < size; i++) {
       statusesIds.add(list.getString(i));
     }
   } catch (JSONException jsone) {
     throw new WeiboException(jsone);
   }
 }
 /**
  * @param res
  * @return
  * @throws WeiboException
  */
 public static UserWapper constructWapperUsers(Response res) throws WeiboException {
   JSONObject jsonUsers = res.asJSONObject(); // asJSONArray();
   try {
     JSONArray user = jsonUsers.getJSONArray("users");
     int size = user.length();
     List<User> users = new ArrayList<User>(size);
     for (int i = 0; i < size; i++) {
       users.add(new User(user.getJSONObject(i)));
     }
     long previousCursor = jsonUsers.getLong("previous_curosr");
     long nextCursor = jsonUsers.getLong("next_cursor");
     if (nextCursor == -1) { // 兼容不同标签名称
       nextCursor = jsonUsers.getLong("nextCursor");
     }
     return new UserWapper(users, previousCursor, nextCursor);
   } catch (JSONException jsone) {
     throw new WeiboException(jsone);
   }
 }
Exemple #10
0
  public static List<User> constructUser(Response res) throws WeiboException {

    JSONObject json = res.asJSONObject();
    JSONArray list = null;
    try {
      if (!json.isNull("users")) {
        list = json.getJSONArray("users");
      } else {
        list = res.asJSONArray();
      }
      int size = list.length();
      List<User> users = new ArrayList<User>(size);
      for (int i = 0; i < size; i++) {
        users.add(new User(list.getJSONObject(i)));
      }
      return users;
    } catch (JSONException je) {
      throw new WeiboException(je);
    }
  }
 public static StatusWapper constructWapperStatus(Response res) throws WeiboException {
   JSONObject jsonStatus = res.asJSONObject(); // asJSONArray();
   JSONArray statuses = null;
   try {
     if (!jsonStatus.isNull("statuses")) {
       statuses = jsonStatus.getJSONArray("statuses");
     }
     if (!jsonStatus.isNull("reposts")) {
       statuses = jsonStatus.getJSONArray("reposts");
     }
     int size = statuses.length();
     List<Status> status = new ArrayList<Status>(size);
     for (int i = 0; i < size; i++) {
       status.add(new Status(statuses.getJSONObject(i)));
     }
     long previousCursor = jsonStatus.getLong("previous_curosr");
     long nextCursor = jsonStatus.getLong("next_cursor");
     long totalNumber = jsonStatus.getLong("total_number");
     String hasvisible = jsonStatus.getString("hasvisible");
     return new StatusWapper(status, previousCursor, nextCursor, totalNumber, hasvisible);
   } catch (JSONException jsone) {
     throw new WeiboException(jsone);
   }
 }