【问题标题】:String matching with maximum number of occurrence与最大出现次数匹配的字符串
【发布时间】:2013-05-23 00:31:57
【问题描述】:

我在这里有这个长字符串,并且在文本文件中有类似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"}}}

【问题讨论】:

  • 这是一个 JSON 字符串,您可以使用一些库将其转换为 JSON 对象,这将使您的生活更轻松。

标签: java string string-matching


【解决方案1】:

使用 RandomAccessFile 和 BufferedReader 类分批读取数据,可以使用字符串解析来统计每个日期出现的频率...

【讨论】:

    【解决方案2】:

    每个日期都有一些稳定的模式,例如 \d\d​​ (Jan|Feb|...) 20\d\d 因此您可以使用正则表达式提取这些日期(Java 中的模式类) 然后你可以使用 HashMap 来增加一些键是找到日期的对的值。抱歉没有代码,但我希望对你有帮助:)

    【讨论】:

      【解决方案3】:

      我认为它是一个JSON 字符串,你应该解析它而不是匹配它。 看这个例子HERE

      【讨论】:

        【解决方案4】:

        把需要的字符串复制到test.text,放到c盘 工作代码,我使用了 Pattern 和 Matcher 类

        在 Pattern 中我给出了你所要求的日期 Pattern,你可以在这里查看模式

        "(周日|周一|周二|周三|周四|周五|周六)[,] \d\d​​ (一月|二月|三月|四月|五月|六月|七月|八月|九月|十月|十一月|十二月) \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 这里的描述非常好

        【讨论】:

          【解决方案5】:

          您的输入字符串是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));
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-12-30
            • 1970-01-01
            • 2011-10-25
            • 1970-01-01
            相关资源
            最近更新 更多