个人作业

本文介绍了一个使用Java实现的敏感词分析程序,该程序能够读取指定文本文件,并对照敏感词库进行匹配分析,最终输出敏感词出现次数及替换后的文本。

题目:      敏感词汇分析                                 

 

 

学校:齐鲁工业大学             

学院:信息学院                 

班级:计科(高职)13-3        

学号: 201303014099           

姓名: 王希敏              

 

 

一、题目简介

利用Java常用基础类API、输入输出流常用类API、Java异常处理等完成所选项目的程序设计。

现在各大软件公司都有自己的、各种高级敏感词汇的程序,但是这些公司都有自己的版权,我们也不可能得到这些源代码!而且这些公司的程序也相当复杂,仅从日常应用方面来说,对于初学java的我们也无法理解,就需要我们开发一个简单易懂的敏感词汇分析程序。

二、源码的github链接

https://github.com/wangximin/test/blob/master/敏感词汇分析.md

三.程序运行结果及截图

 

 

 

四.总体设计

本程序要构建敏感词汇分析程序参照了Windows操作系统的文本编辑器工具,其功能有以下几个方面: 

(1).菜单中有“文件” , “分析”,“帮助”三个主菜单。

(2) “文件”有“新建” “打开”二个子菜单:分别用于新建文件,打开文件。

(3)分析中有“确定” 、“取消”。

五、核心算法详细设计     

5.1.初始化组件

initTextContent();

    initMenu();

    initHelpDialog(); 

5.2.构建菜单栏及其下拉菜单

JMenu[] menus=new JMenu[]{

new JMenu("文件"),

new JMenu("分析"),

new JMenu("帮助")

};

JMenuItem optionofmenu[][]=new JMenuItem[][]{{

new JMenuItem("文件"),

new JMenuItem("新建"), 

new JMenuItem("打开"),

}

5.3.功能分析

第一个功能:

 import java.awt.*; 

import java.awt.event.*;

public class Mingan1

{

public static void main(String args[])

{ Frame f1=new Frame("敏感词分析程序");

f1.setBounds(500, 100, 300, 200);

f1.setBackground(Color.lightGray);

f1.setLayout(new GridLayout(3,1));

Panel p1=new Panel();

f1.add(p1); 

Panel p2=new Panel();

f1.add(p2); 

Panel p3=new Panel();

f1.add(p3); 

f1.setVisible(true);

Label l1=new Label("请点击输入含有敏感词的文本文件:");

p3.add(l1); 

Button b1=new Button("确定");

p3.add(b1); 

b1.addActionListener(new ActionListener()

{public void actionPerformed(ActionEvent e)

{TestJMenu jmFrame=new TestJMenu();

} }); 

Label l2=new Label("敏感词汇保存在sensitive.txt文件中");

p1.add(l2); 

p2.add(new Label("请查看sensitive.txt文件"));

} }

第二个功能:

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

import java.io.File; 

import java.io.FileInputStream;

public class TestJMenu extends JFrame implements ActionListener

    JTextArea jta=new JTextArea();

      TestJMenu() 

      {     

        this.setSize(400,300);

        JMenuBar jmb=new JMenuBar();

        JMenu jmFile=new JMenu("文件");

        JMenuItem jmiNew=new JMenuItem("新建");

       JMenuItem jmiOpen=new JMenuItem("打开");

        jmiOpen.addActionListener(this);

        jmFile.add(jmiNew);

        jmFile.add(jmiOpen);

        JMenu jmFenxi=new JMenu("分析");

        JMenuItem jmiQue=new JMenuItem("确定");

        JMenuItem jmiQu=new JMenuItem("取消");

        jmFenxi.add(jmiQue);

        jmFenxi.add(jmiQu);  

        jmiQue.addActionListener(new ActionListener()

         { 

            public void actionPerformed(ActionEvent e)

            { 

             String filterText = jta.getText();

                try

                 { 

                     jta.setText(Fenx.getFilterText(filterText));

                  }  

                catch(Exception exx)

                { 

                    exx.printStackTrace();

                 } 

             }                      

   }); 

 

 

第三个功能

import java.io.BufferedReader;

import java.io.FileReader;

import java.util.ArrayList;

import java.util.List; 

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class Fenx 

{     

    public static void main(String[] args) throws Exception 

    {         

        String filterText = new String(); 

        System.out.println(getFilterText(filterText));  

     }

      public static String getFilterText(String filterText) throws Exception 

      { 

        List listWord = new ArrayList();       

        FileReader reader = new FileReader("d:/sensitive.txt");

        BufferedReader br = new BufferedReader(reader);

        String s = null; 

       while ((s = br.readLine())!= null) 

        { 

            listWord.add(s.trim()     } 

        br.close(); 

        reader.close(); 

        Matcher m = null; 

        String str1=new String();     

        for (int i = 0; i < listWord.size(); i++) 

       { 

        int num=0;

        Pattern 

p=Pattern.compile(listWord.get(i).toString(),Pattern.CASE_INSENSITIVE);     

            StringBuffer sb = new StringBuffer();

            m = p.matcher(filterText);

            while (m.find()) 

            { 

               m.appendReplacement(sb, "口"); 

               num++;          

            } 

            str1='\n'+"敏感词 "+p.toString()+" 出现:"+num+"次"; 

            //System.out.println('\n'+"敏感词 "+p.toString()+" 出现:"+num+"次"); 

            m.appendTail(sb); 

            filterText = sb.toString(); 

            filterText+=str1;

        }        

        return filterText;        

     }     }

 

在本程序中,要分析的文本和敏感词汇库分别保存在了analy.txt和sentive.txt两个文档中,要分析的文档中的内容为 “I like java very much.I also like speak English.I like swimming very much.My name is MrH.This  java programe is developed by me.I think it is OK.Thank you for every person who ever helped me.”,敏感词汇分别是I,like,java,you,OK,is,very,me,thank,much,通过程序进行分析,得出正确结论,程序运行成功。具体运行结果见下图:

 

 

四、问题及解决方案、心得体会

       过这次的课程设计,使将我从书本上学习到的理论知识用到了实践上,从而进一步巩固和丰富了我所学过的知识,让我更深层次地认识到Java及其强大的功能。同时,做这门课程设计也进一步加强了我的动手能力。基本上完成我的Java课程设计—简单的敏感词汇分析程序,也基本上实现了我在需求分析时所预期的功能。

 

转载于:https://www.cnblogs.com/wangximin/p/4466933.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值