写在开头:本次的小练习主要会运用一些字典的排序,由于对JAVA不是很熟悉,所以有的地方会不太能够解读,搜索了部分网上的资料。
英文文本词频统计
任务目标:统计英文文本中出现频率最高的5个单词,需要处理介词、时态和复数
任务自述:这个任务首先需要对文本进行分隔,然后还需要处理介词时态和复数、并转化为字典进行统计,最后再排序输出,对于我这样的小白来说着实难度挺大的,不过今天死磕下来,查阅了不少小函数的资料,整体是能跑出来了,不过可以更好的优化,特别是对于排序的地方。
任务实现:
笔者就不对每段函数单独进行讲解了,我们直接上手完整的程序,笔者会在完整的程序中添加注释和说明。代码如下,可能代码横向比较宽,可以拖过去看一下,
package test4;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
public class NewClass {
public static void main(String[] args) {
//加载数据
String words = "The Delegation of Sichuan Tourism University Visited SWUFE\n"
+ "On May 31st, the delegation of Sichuan Tourism University, including YAN Qipeng, Secretary of the Party Committee, BAI Jie, Vice President, and representatives of related departments visited SWUFE. ZHAO Dewu, Chairman of the University Council, LI Yongqiang, Vice President as well as representatives of President's Office, Office of Organization and Personnel, Office of Academic Affairs, School of Accounting, School of Marxism, and School of Economic Information Engineering had a discussion in the meeting room 604 of Tengxiang Building.\n"
+ " ZHAO Dewu extended a sincere welcome to YAN's visit. He introduced the general situation, historical development, development status, strategic objectives and development ideas of SWUFE. ZHAO Dewu pointed out that with Chinese Socialism's entrance into a new era, financial and economic higher education has also entered the \"new financial and economic\" era. In this context, profound changes have taken place in the discipline, organizational members and university functions. Colleges and universities should actively adapt to the changes and enhance their subjective initiatives. He hopes that both sides will further strengthen exchanges and cooperation and jointly promote development in the future.\n"
+ "YAN Qipeng expressed thanks for SWUFE's warm reception. He introduced the development history and faculty of Sichuan Tourism University, hoping to further learn from SWUFE's good practices and experience in discipline construction and talent training and further establish a long-term mechanism to strengthen in-depth exchanges and cooperation.\n"
+ "At the meeting, both sides had in-depth exchanges on talent training, grass-roots party building and comprehensive management etc.\n"
+ "After the meeting, the delegation visited the History Museum of SWUFE and the Museum of Money and Finance. (Office of the University Council)\n"
+ "Senior Diplomat of Ministry of Foreign Affairs ZHANG Limin Delivered A Lecture in SWUFE\n"
+ "In the afternoon of On May 28th, ZHANG Limin, a senior diplomat and former ambassador of the Ministry of Foreign Affairs, gave a lecture on the theme of \"China's diplomacy and the International Situation\" in the Conference Room 101 of Hongyuan Building in SWUFE. OU Bing, Vice Chairman of the University Council presided over the lecture. Student representatives from the fifth training course for college student cadres as well as Head Start classrooms of talent training for international organizations presented the lecture.\n"
+ "Before the lecture, OU Bing gave a brief introduction ZHANG. On behalf of SWUFE, he expressed a warm welcome to ZHANG Limin. OU sincerely hoped that students would cherish the opportunity of face-to-face communication with ZHANG. SWUFE students are expected to understand the national conditions, have a global view and strive to become innovative talents who are familiar with international rules.\n"
+ "In the lecture, on the basis of his nearly four decades of diplomatic experience, ZHANG introduced some basic knowledge, diplomatic etiquette in the international diplomacy, the main tasks and work contents of embassies and consulates abroad. He further expounded the significance of diplomacy and the basic requirements of personal quality for a diplomat. He also combed the history of China's diplomatic development, the current international situations and China's foreign policy. He then shared his insights on recent hot topics. He stressed that students should pay attention to the changes in world economy as well as science and technology and to analyze problems by using professional knowledge. Meanwhile, he encouraged students to participate in international organizations' internship and employment to make China heard throughout the world.\n"
+ "In the Q&A session, ZHANG patiently responded to the questions from students. Students said that the lecture has greatly extended their horizon. They have been keenly aware of the patriotic feelings, and realized the glory of working as a diplomat for the country.\n"
+ "ZHANG Limin is a senior diplomat of the Ministry of Foreign Affairs, a former Ambassador of the Chinese Embassy in Italy, Consul General of the Consulate General in Milan, former Chinese Ambassador to Guyana, and now the member of the Council for Promoting South-South Cooperation as well as the special consultant to the Research Center for Latin America, SWUFE. (Student Affairs Department, School of International Business)";
String wordsL = words.toLowerCase(); //将文本转换为小写
String regex = "[\\p{Punct}\\s]+"; //定义正则表达式,排除控制和标点
String[] s = wordsL.split(regex); //对文本进行词的分隔储存
//删掉介词
//思路:将介词换为空值
String str[] = {
"of","the","and","in","a","to","as","s","for","on","with",