2013-05-28 40 views
3

我有這個長的字符串在這裏,有像這樣的1000行在一個文本文件中。我想計算每個日期在該文本文件中出現的頻率。任何想法如何可以我那樣做?字符串匹配與最大出現次數

{"interaction":{"author":{"id":"53914918","link":"http:\/\/twitter.com\/53914918","name":"ITTIA","username":"s8c"},"content":"RT @fubarista: After thousands of years of wars I am not an optimist about peace. The US economy is totally reliant on war. It is the on ...","created_at":"Sun, 10 Jul 2011 08:22:16 +0100","id":"1e0aac556a44a400e07497f48f024000","link":"http:\/\/twitter.com\/s8c\/statuses\/89957594197803008","schema":{"version":2},"source":"oauth:258901","type":"twitter","tags":["attretail"]},"language":{"confidence":100,"tag":"en"},"salience":{"content":{"sentiment":4}},"twitter":{"created_at":"Sun, 10 Jul 2011 08:22:16 +0100","id":"89957594197803008","mentions":["fubarista"],"source":"oauth:258901","text":"RT @fubarista: After thousands of years of wars I am not an optimist about peace. The US economy is totally reliant on war. It is the on ...","user":{"created_at":"Mon, 05 Jan 2009 14:01:11 +0000","geo_enabled":false,"id":53914918,"id_str":"53914918","lang":"en","location":"Mouth of the abyss","name":"ITTIA","screen_name":"s8c","time_zone":"London","url":"https:\/\/thepiratebay.se"}}}

+3

這是一個JSON字符串,你可以使用一些圖書館將其轉換爲一個JSON對象,這將使您的生活更輕鬆。 – NINCOMPOOP

回答

1

利用類型的RandomAccessFile和BufferedReader在部分讀取數據,你可以使用字符串解析計算每個日期的頻率...

1

每一日期,有一些穩定的格局,像\ d \ d(Jan | Feb | ...)20 \ d \ d 因此,您可以使用正則表達式(Java中的模式類) 提取這些日期,然後您可以使用HashMap來增加某些鍵的值,其中鍵是找到的日期。對不起,沒有代碼,但我希望可以幫助你:)

+0

每個日期還以''created_at'爲前綴:'... – MadProgrammer

0

我就是它的一個JSON字符串你應該解析它而不是匹配。 看到這個例子HERE

0

複製所需的字符串test.text,並將其放在C盤 工作的代碼,我已經使用Pattern和Matcher類

的模式,我給你問日期的模式,你可以檢查這裏的模式

「(太陽|星期一|星期二|星期三|星期四|星期五|星期六] [,] \ d \ d(Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec)\ d \ d \ d \ d「

檢查代碼

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

class Test{ 
public static void main(String[] args) throws Exception { 

    FileReader fw=new FileReader("c:\\test.txt"); 
    BufferedReader br=new BufferedReader(fw); 
    int i; 
    String s=""; 
    do 
    { 

     i=br.read(); 
     if(i!=-1) 
     s=s+(char)i; 


    }while(i!=-1); 

    System.out.println(s); 

    Pattern p=Pattern.compile 
      (
        "(Sun|Mon|Tue|Wed|Thu|Fri|Sat)[,] \\d\\d (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \\d\\d\\d\\d" 
       ); 

    Matcher m=p.matcher(s); 
    int count=0; 
    while(m.find()) 
    { 
     count++; 
     System.out.println("Match number "+count); 
     System.out.println(s.substring(m.start(), +m.end())); 


    } 
    } 


} 

非常好的描述在這裏Link 1Link 2

0

你輸入的字符串是JSON格式,因此,我建議你使用JSON解析器,這使得分析很多容易,更重要的強勁!儘管進入JSON解析可能需要幾分鐘的時間,但它是值得的。

之後,解析「created_at」標籤。創建您的日期鍵和值的計數一個地圖和寫類似:

int estimatedSize = 500; // best practice to avoid some HashMap resizing 
Map<String, Integer> myMap = new HashMap<>(estimatedSize); 
String[] dates = {}; // here comes your parsed data, draw it into the loop later 
for (String nextDate : dates) { 
    Integer oldCount = myMap.get(nextDate); 
    if (oldCount == null) { // not in yet 
     myMap.put(nextDate, Integer.valueOf(1)); 
    } 
    else { // already in 
     myMap.put(nextDate, Integer.valueOf(oldCount.intValue() + 1)); 
    } 
} 
相關問題