Java: 使用HashMap统计文件中单词个数

思路:将除单引号的英文标点换成空格,然后逐词解决计数问题。

直接上代码:

// Coding starts here

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主类。
 * @author Hippo
 *
 */
public class Main
{
    private static char[] punctuations = new char[] {',', '.', '/', '\\', '?', '!', '@', '#', '$', '%',
            '&', '*', '(', ')', '|', ':', ';', '-', '=', '+', '[', ']', '{', '}', '_', '\"'};
    
    public static void main(String[] strings) throws IOException
    {
        List all = Files.readAllLines(Paths.get("D:\\hippo.txt"));
        Map map = new HashMap<>();
        String checked;
        for(String current : all)
        {
            checked = check(current);
            String[] words = checked.split(" ");
            for(String word : words)
            {
                word = word.trim().toLowerCase();
                if(word.equals(" "))
                    continue;
                if(!map.containsKey(word))
                    map.put(word, 1);
                else
                {
                    int times = map.get(word) + 1;
                    map.remove(word);
                    map.put(word, times);
                }
            }
        }
        
        System.out.println(map);
    }
    
    /**
     * 优化字符串,将除单引号外的英文标点符号替换成空格。
     * @param s 待优化的字符串
     * @return 优化后的结果
     */
    public static String check(String s)
    {
        for(int i = 0; i < s.length(); i++)
            if(isPunctuation(s.charAt(i)))
                s = s.replace(s.charAt(i), ' ');
        return s;
    }
    
    public static boolean isPunctuation(char c)
    {
        for(char each : punctuations)
            if(c == each)
                return true;
        return false;
    }
}

分析文章:

The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.

Namespaces are one honking great idea -- let's do more of those!


分析结果:

{explain=2, silently=1, peters=1, preferably=1, bad=1, counts=1, simple=1, rules=1, do=2, nested=1, good=1, dutch=1, beautiful=1, that=1, ambiguity=1, complex=2, than=8, should=2, only=1, tim=1, if=2, those=1, zen=1, python=1, pass=1, in=1, it's=1, let's=1, ugly=1, often=1, is=10, it=2, easy=1, dense=1, explicit=1, never=3, practicality=1, at=1, sparse=1, temptation=1, guess=1, aren't=1, errors=1, namespaces=1, silenced=1, implicit=1, explicitly=1, be=3, purity=1, idea=3, not=1, refuse=1, unless=2, are=1, flat=1, and=1, by=1, you're=1, now=2, of=3, hard=1, a=2, cases=1, may=2, break=1, more=1, implementation=2, one=3, beats=1, enough=1, right=1, honking=1, great=1, way=2, special=2, better=8, the=6, readability=1, face=1, although=3, complicated=1, there=1, obvious=2, to=5, first=1}


在进阶的路上,欢迎各位大神指正。

你可能感兴趣的:(Java: 使用HashMap统计文件中单词个数)