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

2023-11-07 06:50

本文主要是介绍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练习-使用数组统计一段英语文章中的单词频数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/361970

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

中文分词jieba库的使用与实景应用(一)

知识星球:https://articles.zsxq.com/id_fxvgc803qmr2.html 目录 一.定义: 精确模式(默认模式): 全模式: 搜索引擎模式: paddle 模式(基于深度学习的分词模式): 二 自定义词典 三.文本解析   调整词出现的频率 四. 关键词提取 A. 基于TF-IDF算法的关键词提取 B. 基于TextRank算法的关键词提取

使用SecondaryNameNode恢复NameNode的数据

1)需求: NameNode进程挂了并且存储的数据也丢失了,如何恢复NameNode 此种方式恢复的数据可能存在小部分数据的丢失。 2)故障模拟 (1)kill -9 NameNode进程 [lytfly@hadoop102 current]$ kill -9 19886 (2)删除NameNode存储的数据(/opt/module/hadoop-3.1.4/data/tmp/dfs/na

Hadoop数据压缩使用介绍

一、压缩原则 (1)运算密集型的Job,少用压缩 (2)IO密集型的Job,多用压缩 二、压缩算法比较 三、压缩位置选择 四、压缩参数配置 1)为了支持多种压缩/解压缩算法,Hadoop引入了编码/解码器 2)要在Hadoop中启用压缩,可以配置如下参数