Google - Find Most People in Chat Log

本文介绍了一个用于分析聊天日志的算法,该算法能够读取聊天记录文件,统计每个用户发送消息中单词的数量,并找出说话最多的K个人。文章提供了一段Java代码实现,包括文件读取、数据处理和结果输出。
1. 给你一个chatting log file,format大概是这样的:
A: bla
B: bla bla
C: bla bla bla
要你找出说话最多(看word number) 的K个人
而且代码要从读file开始写
/*
1. 给你一个chatting log file,format大概是这样的:
A: bla
B: bla bla
C: bla bla bla
要你找出说话最多(看word number) 的K个人
而且代码要从读file开始写

*/
public class Main {
    public static void main(String[] args) {
        List<String> lines =new ArrayList<>();
        lines.add("A: bla");
        lines.add("A: bla");
        lines.add("B: bla bla");
        lines.add("C: bla bla bla");
        lines.add("A: bla");
        
        
        List<String> list = new Solution().findMostPeopleTest(lines);
        for(String name : list){
            System.out.println(name);
        }
    }
}

class Solution{
    /*
    public List<String> findMostPeople (String filePath) throws Exception {
        HashMap<String, Integer> map = new HashMap<>();
        int maxCounts = 0;
        List<String> list = new ArrayList<>();
        //read from file
        File file = new File(filePath);
        BufferedReader br = new BufferedReader(new FileReader(filePath));
        String st;
        while((st = br.readLine()) != null){
            //can be replaced by other string related functions
            String[] strs = st.split(": ");
            String name = strs[0];
            String[] words =strs[1].split(" ");
            int wordsCount = words.length;
            int totalCounts = map.getOrDefault(name, 0)+wordsCount;
            map.put(name, totalCounts);
            if(totalCounts > maxCounts){
                list.clear();
                list.add(name);
            }
            else if (totalCounts == maxCounts){
                list.add(name);
            }   
        }
        return list;
    }
    */
    public List<String> findMostPeopleTest (List<String> lines) {
        HashMap<String, Integer> map = new HashMap<>();
        int maxCounts = 0;
        List<String> list = new ArrayList<>();
        //read from file
        //File file = new File(filePath);
        //BufferedReader br = new BufferedReader(new FileReader(filePath));
        for(String st : lines){
            //can be replaced by other string related functions
            String[] strs = st.split(": ");
            String name = strs[0];
            String[] words =strs[1].split(" ");
            int wordsCount = words.length;
            
            int totalCounts = map.getOrDefault(name, 0)+wordsCount;
            System.out.println(name + " : "+totalCounts);
            map.put(name, totalCounts);
            if(totalCounts > maxCounts){
                list.clear();
                list.add(name);
                maxCounts = totalCounts;
            }
            else if (totalCounts == maxCounts){
                list.add(name);
            }   
        }
        return list;
    }
    
}

 

转载于:https://www.cnblogs.com/incrediblechangshuo/p/10010643.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值