7-2 jmu-Java&Python-统计文字中的单词数量并按出现次数排序 (30分)

现在需要统计若干段文字(英文)中的单词数量,并且还需统计每个单词出现的次数。

注1:单词之间以空格(1个或多个空格)为间隔。
注2:忽略空行或者空格行。

基本版:
统计时,区分字母大小写,且不删除指定标点符号。

进阶版:
统计前,需要从文字中删除指定标点符号!.,?。 注意:所谓的删除,就是用1个空格替换掉相应字符。
统计单词时需要忽略单词的大小写。

输入说明
若干行英文,最后以!!!为结束。

输出说明
单词数量
出现次数排名前10的单词(次数按照降序排序,如果次数相同,则按照键值的字母升序排序)及出现次数。

输入样例1

failure is probably the fortification in your pole

it is like a peek your wallet as the thief when you
are thinking how to spend several hard-won lepta

when you are wondering whether new money it has laid
background because of you then at the heart of the

most lax alert and most low awareness and left it

godsend failed
!!!

输出样例1

46
the=4
it=3
you=3
and=2
are=2
is=2
most=2
of=2
when=2
your=2

输入样例2

Failure is probably The fortification in your pole!

It is like a peek your wallet as the thief when You
are thinking how to. spend several hard-won lepta.

when yoU are? wondering whether new money it has laid
background Because of: yOu?, then at the heart of the
Tom say: Who is the best? No one dare to say yes.
most lax alert and! most low awareness and* left it

godsend failed
!!!

输出样例2

54
the=5
is=3
it=3
you=3
and=2
are=2
most=2
of=2
say=2
to=2

import java.util.*;
import java.util.Map.Entry;

public class Main {
     
	public static void main(String[] args) {
     
		Scanner se=new Scanner (System.in);
		HashMap <String,Integer> h=new HashMap<String,Integer>();
		while(true) {
     
			 String s=se.nextLine();
			 if(s.equals("!!!!!")) {
     
				 break;
			 }else if(s==null||s.length()==0||(s != null && s.equals(""))) {
     
				 continue;
			 }else {
     
				 String a[]=s.split("\\s+");
				 for(int i=0;i<a.length;i++) {
     
					 a[i]=removingRedundantSymbols(a[i]);
					 a[i]=uniformWordCase(a[i]);
					 if (a[i] == null || a[i].length() == 0)
							continue;
					 if (!h.containsKey(a[i])) {
     
							h.put(a[i], 1);
						} else {
     
							int num = h.get(a[i]);
							num++;
							h.put(a[i], num);
						}
				 }
			 }
		}
		ArrayList <Map.Entry<String, Integer>> al=new ArrayList<Map.Entry<String,Integer>>(h.entrySet());
		Collections.sort(al,new Sorting_Tools());
		System.out.println(al.size());
		int x=0;
		for(Map.Entry<String, Integer> e:al) {
     
			if(x<10) {
     			
				System.out.println(e.getKey()+"="+e.getValue());
				x++;
			}
		}
	}

	public static String removingRedundantSymbols(String s) {
     
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<s.length();i++){
     
			if(s.charAt(i)=='!'||s.charAt(i)==','||s.charAt(i)=='.'||s.charAt(i)==':'||s.charAt(i)=='*'||s.charAt(i)=='?')
				continue;
			else
				sb.append(s.charAt(i));
		}
		String a=sb.toString();
		return a;
	}
	public static String  uniformWordCase(String s) {
     
		String a=s.toLowerCase();
		return a;
	}
}
class Sorting_Tools implements Comparator<Map.Entry<String,Integer>>{
     

	@Override
	public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
     
		if(o1.getValue()>o2.getValue()) {
     
			return -1;
		}else if(o1.getValue()<o2.getValue()) {
     
			return 1;
		}else {
     
			if(o1.getKey().compareTo(o2.getKey())>0) {
     
				return 1;
			}else if(o1.getKey().compareTo(o2.getKey())<0) {
     
				return -1;
			}else {
     
				return 0;
			}
		}
	}
}

你可能感兴趣的:(java,正则表达式,字符串,hashmap)