本文主要是介绍Java练习-使用数组统计一段英语文章中的单词频数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Java练习-使用数组统计一段英语文章中的单词频数
- 一、分析
- 1.1 基本要求
- 1.2 进阶要求
- 二、代码实现(分步骤)
- 2.1 赋值,变小写,正则表达式及分割
- 2.2 频数统计
- 排序输出:冒泡
- 2.3 进阶要求实现
- 2.3.1 介词的处理
- 2.3.2 单复数处理
- 2.3.3 时态处理
- 三、完整代码
- 输出结果
要求:基于java
找一段较长的英文文章(500个单词左右),赋值给一字符串,设计以标点符号和空格换行作为分隔符的正则表达式,用split()方法来分割此字符串。
基本要求:忽略单词大小写,统计出出现频率最高的前五位单词,排序
进阶要求:去掉介词,单复数、时态等情况下来分析统计。
一、分析
首先是先将我们要分析的文本赋值给一个字符串,接下来使用正则表达式与spilt()方法来分割此字符串。
1.1 基本要求
忽略大小写,进行字符统计。
此处我们使用两个列表进行统计,一个列表用来存储单词,另一个列表的对应位置存储频数。
进行排序时,使用冒泡排序即可。
1.2 进阶要求
对于介词,单复数以及时态的处理,均在进行统计单词频数之前,在原字符串中修改。
二、代码实现(分步骤)
以下是可能会用到的package
:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
2.1 赋值,变小写,正则表达式及分割
String ss = "On the morning of August 2nd, SHI Daimin, the Vice President of SWUFE, inspected the logistics work in Liulin Campus during the summer vacation and visited the logistics staff who persisted in their work during the vacation. All members of the leading group of the Logistics Service Company and the directors of relevant departments accompanied him in the inspection. SHI Daimin went to the construction sites of student canteen gas pipeline renovation, graduate dormitory maintenance in student apartment, SWUFE-UD Institute of Data Science office renovation, school cooking skills labor education base renovation, and he inquired in detail about the preparatory work of the special project of improving basic school operation conditions in universities in 2020. He visited the study and living places of students such as Tongbo Building, student canteen and other places in summer vacation, and chatted with students about the use of the study room reservation system and the study and life staying at school during the vacation. SHI Daimin pointed out that in every summer vacation, the logistics work was heavy, with tight schedule and great pressure, and the cadres and staff of the Logistics Department worked very hard. On behalf of the school, he expressed his thanks to all the logistics cadres and staff who worked hard in the summer, and hoped that they could continue to carry forward the spirit of fearlessness of hardship and heat, work earnestly, and insist safety management throughout the whole process of summer work, make high-quality and excellent projects to provide solid logistical support for the smooth development of the university’s work in the new semester. After the inspection, members of the logistics leading group and the directors of relevant departments immediately convene a special working meeting to arrange and deploy the renovation project, summer logistics support, special project construction, etc., according to the work requirements of SHI Daimin.";String ss1 = ss.toLowerCase();// 首先将字符串全部转换为小写
String regx = "[\\s\\p{Punct}]+"; // []表示里面任意一个,所以是空格或者标点出现多次
String[] sss = ss1.split(regx);
此处我们主要探讨一下正则表达式,下表给除了我们可能使用到的元字符以及正则表达式的写法
在正则表达式中可以用方括号括起若干个字符来表示一个字符,该元字符代表方括号中的任意一个字符。
此外括号中允许还有括号,可以进行交并差运算,如
[^abc] // 代表除了abc以外的任何字符
[a-d[m-p]] // 表示a~d或者m~p的任何一个字符(并)
[a-z&&[def]] // 掉膘d e f中的任何一个(交)
[a-f&&[^bc]] // 代表a d e f(差)
另外还需要注意的是,“.”表示任何一个字符,如果正则表达式中想要使用这个点字符,那么要使用[.]或者\56
2.2 频数统计
按照我们上面的分析,首先创建两个列表
// 首先按照初始化两个一维数组,数组的长度可能不可能大于原数组的长度。
// 并且第一个数组存储单词,第二个数组的对应位置存储出现的次数
String ss2 [] = new String[sss.length];
int num[] = new int[sss.length];
接下来进行频数的统计,代码如下
// 做两个循环,第一个数是所有的遍历,此时初始化count
int p = 0; // 用这个数向ss2和num中添加元素,第一层for之后加1for (int i = 0; i < sss.length; i++) {if ("".equals(sss[i])) {continue;}else{ss2[p] = sss[i];int count = 1;for (int j = i+1; j < sss.length; j++) {// 判断出现的次数,如果重复出现count就加1,并把后面重复的那个单词删掉if (sss[i].equals(sss[j])) {count += 1;sss[j] = "";}}num[p
这篇关于Java练习-使用数组统计一段英语文章中的单词频数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!