Java练习-使用数组统计一段英语文章中的单词频数

要求:基于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之后加1
  for (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] = count;
          p += 1;
      }
  }

排序输出:冒泡

// 按照频数的大小进行输出排序:冒泡
// 想法与前面相同,对第二个列表num进行冒泡排序,同时将第一个列表相应位置也排序
for (int i = 0; i <<
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值