简体   繁体   English

在句子的字符串数组中查找特定单词并返回特定单词在整个数组中的频率

[英]Find a particular word in a string array of sentences and return the frequency of a particular word throughout the array

The input is String array as below,输入是字符串数组,如下所示,

{"1112323 400 error","1112323 400 error","9988778 400 error"} {"1112323 400 错误","1112323 400 错误","9988778 400 错误"}

I need to print the timestamp ie the number at the start of the sentences and its frequency throughout the array我需要打印时间戳,即句子开头的数字及其在整个数组中的频率

I've come only this far as of now.我到现在才到这一步。 Only have been able to find the string if it is known already.只有在已知字符串的情况下才能找到该字符串。

    int count = 0;

  for(int i=str1.length-1;i>=0;i--)
  {
      String[] ElementOfArray = str1[i].split(" ");
      
      for(int j=0;j<ElementOfArray.length-1;j++)
      {
          if(ElementOfArray[j].equals("Hi"))
          {
              count++;
          }
      }
      
  }
  System.out.println(count);

One approach is to keep track of the number of entries, and increment.一种方法是跟踪条目的数量并增加。

    public static void main(String[] args)
    {
        String[] inp = {"1112323 400 error",
                "1112323 400 error",
                "9988778 400 error"};
                
        
        Map<String,Integer> results = new HashMap<>();
        
        for (String one : inp) {
            String[] parts = one.split(" ");
            
            String ts = parts[0];
            
            int val = results.computeIfAbsent(ts, v-> 0);
            results.put(ts, ++val);
        }
        
        System.out.println(results);
    }

Note: there are other ways to handle the map incrementing.注意:还有其他方法可以处理 map 递增。 This is just one example.这只是一个例子。

Sample Output:样品 Output:

{1112323=2, 9988778=1}

Now, if in the future one might want to perform other operations, using objects might be of interest.现在,如果将来可能想要执行其他操作,那么使用对象可能会很有趣。

So a class might be:所以 class 可能是:

   private static class Entry
    {
        private final String ts;
        private final String code;
        private final String desc;
        
        public Entry(String ts, String code, String desc)
        {
           
            // NOTE: error handling is needed
            this.ts = ts;
            this.code = code;
            this.desc = desc;
        }
        
        
        public String getTs()
        {
            return ts;
        }
        
        
        public static Entry fromLine(String line)
        {
            Objects.requireNonNull(line, "Null line input");
            
            // NOTE: other checks would be good
            String[] parts = line.split(" ");
            
            // NOTE: should verify the basic parts
            return new Entry(parts[0], parts[1], parts[2]);
        }
        
        // other getter methods
    }

And then one could do something like:然后可以执行以下操作:

        List<Entry> entries = new ArrayList<>();
        for (String one : inp) {
            entries.add(Entry.fromLine(one));
        }
        
        Map<String,Integer> res2 = entries.stream()
                .collect(Collectors.groupingBy(x->x.getTs(),
                                               Collectors.summingInt(x -> 1)));
        
        System.out.println(res2);

(same sample output at the moment). (目前相同的样品 output)。 But if one needs to extend to count the number of 400 codes or whatever, it is trivial to change the stream since the object has the data.但是,如果需要扩展以计算 400 个代码的数量或其他什么,更改 stream 是微不足道的,因为 object 有数据。 Of course, there are even more extensions to this approach.当然,这种方法还有更多的扩展。

You can use HashMap to solve count the frequency of timestamps.您可以使用HashMap来解决计算时间戳的频率。

import java.util.HashMap;

public class test {
    public static void main(String[] args) {
        // Create a HashMap object called timeFrequency
        HashMap<String, Integer> timeFrequency = new HashMap<String, Integer>();

        String []str1 = {"1112323 400 error","1112323 400 error","9988778 400 error"};
        for(int i=0;i<str1.length;i++)
        {
            String[] ElementOfArray = str1[i].split(" ");
            if(timeFrequency.containsKey(ElementOfArray[0])){
                timeFrequency.put(ElementOfArray[0], timeFrequency.get(ElementOfArray[0]) + 1);
            }else{
                timeFrequency.put(ElementOfArray[0], 1);
            }

        }
        System.out.println(timeFrequency);
    }
}
Output:
{1112323=2, 9988778=1}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM