检索出文本中出现次数最多的单词

原创转载请注明出处:http://agilestyle.iteye.com/blog/2392423

 

具体思路,将文本中的段落进行split拆分成一个个单词,然后遍历,放入map,统计每个单词出现的次数,最后放入list,按出现次数进行降序排列

Note:此方法不是最优解,还有更高效的算法,欢迎拍砖

 

package org.fool.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        String str = "Ah, Ah, Ah, What the hell are you talking about, I have already told you what you should do, I really have no idea what you think about";
        String[] strings = str.replace('\'', ' ').replace(',', ' ').replace('.', ' ').split("\\s+");

        Map<String, Integer> map = new HashMap<>();
        for (String s : strings) {
            if (map.containsKey(s)) {
                map.put(s, map.get(s) + 1);
            } else {
                map.put(s, 1);
            }
        }

        List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());

        list.sort((o1, o2) -> o2.getValue() - o1.getValue());

        System.out.println(list.get(0).getKey() + ":" + list.get(0).getValue());
        System.out.println(list.get(1).getKey() + ":" + list.get(1).getValue());
    }
}

Console Output


 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以按照以下步骤实现: 1. 打开文本文件并读取其的内容。 2. 定义一个函数,用于统计给定单词文本出现的总次数。在函数,可以使用字符串处理函数 strtok() 将文本分割成单词,然后逐一比较每个单词是否与给定单词相同,若相同则累加计数器。 3. 定义另一个函数,用于检索某个单词出现文本的行号、在该行出现次数以及位置。在函数,可以使用字符串处理函数 strstr() 在每一行查找给定单词,若找到则输行号、出现次数以及位置。 4. 关闭文件并结束程序。 下面是一份简单的实现代码,仅供参考: ```c #include <stdio.h> #include <string.h> #define MAX_LINE_LEN 1024 // 最大行长 #define MAX_WORD_LEN 64 // 最大单词长 // 统计给定单词文本出现的总次数 int count_word(const char* filename, const char* word) { FILE* fp = fopen(filename, "r"); if (!fp) { perror("Failed to open file"); return -1; } char line[MAX_LINE_LEN]; int count = 0; while (fgets(line, MAX_LINE_LEN, fp)) { char* token = strtok(line, " \t\n\r"); // 分割单词 while (token) { if (strcmp(token, word) == 0) { count++; } token = strtok(NULL, " \t\n\r"); } } fclose(fp); return count; } // 检索某个单词出现文本的行号、在该行出现次数以及位置 void search_word(const char* filename, const char* word) { FILE* fp = fopen(filename, "r"); if (!fp) { perror("Failed to open file"); return; } char line[MAX_LINE_LEN]; int line_num = 1; while (fgets(line, MAX_LINE_LEN, fp)) { char* pos = line; int count = 0; while ((pos = strstr(pos, word))) { // 查找单词 count++; printf("Line %d, count %d, position %ld\n", line_num, count, pos - line + 1); pos += strlen(word); } line_num++; } fclose(fp); } int main() { const char* filename = "test.txt"; const char* word = "hello"; int count = count_word(filename, word); if (count >= 0) { printf("%s appears %d times in %s\n", word, count, filename); } search_word(filename, word); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值