为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

敏感词汇分析

2017-09-20 13页 doc 366KB 134阅读

用户头像

is_153723

暂无简介

举报
敏感词汇分析  软 件 学 院 课程设计报告书 课程名称          Java编程技术            设计题目          敏感词汇分析程序          专业班级          112052                      学    号          11205204                      姓    名            胡雨婷                            目录 一、需求分析..................................
敏感词汇分析
  软 件 学 院 课程设计报告书 课程名称          Java编程技术            设计题目          敏感词汇分析程序          专业班级          112052                      学    号          11205204                      姓    名            胡雨婷                            目录 一、需求分析.................................................................................2 二、概要设计.................................................................................3 三、详细设计.......................................................................5 四、调试分析...............................................................................18 五、用户手册...............................................................................18 六、测试数据................................................................................19 七、附录.............................................................................23 一、需求分析   通过此次课程设计,系统、全面地了解与重温了有关java界面生成、字节流的输入输出功能、循环语句的应用、以及包的生成和引用等。 4.1.1问题描述   在的sentive.txt文本和replace。txt中分别创建敏感词汇库,替换词汇库,即把要分析的敏感词汇,替换词汇分别写入敏感词汇库和替换词汇库,然后选择分析文档,进行分析并替换。 4.1.2基本要求 (1)使用图形用户界面实现。 (2)任意选择一个文本文件,分析该文件,如果存在敏感词汇,报告每个词出现的次数。并将敏感词汇用替换词汇替代了。 (3)敏感词汇保存在sentive.txt文件中,每个词占一行,至少设置10个敏感词汇。 (4)替换词汇保存在replace.txt文件中,每个词占一行,至少设置10个敏感词汇。 (5)系统使用的GUI组件不得少于5种,越全面越好,但要合适(和谐界面)。 二、概要设计 1.类的描述 JfileTextChecke主类 IntString    默认属性类,完成字符计数功能 CFileChooser默认属性类,完成文本选择功能 CFileChooseLis默认属性类,完成文本选择功能中的异常处理 CAnalyzeLis默认属性类,完成敏感词汇的分析   2.总体设计     这个程序是图形界面、流式布局、输入、输出流、包以及文件等技术的综合应用。考虑到程序的结构和易用性等各种性能,选择了awt组件进行窗口以及对话框的实现。在运行程序的时候,在规定的sentive.txt文本中创建敏感词汇库,即把要分析的敏感词汇写入,然后选择分析文档,进行分析。 文本选择的实现:在进行词汇分析之前要进行文本的选择,这期间通过查阅各种相关参考文献以及java中的帮助文档,用到了JfileChooser组件来实现对任意文本的选择,从而能顺利的选择各种文件,同时利用try,catch语句进行异常捕获。 词汇分析过程的实现:利用了字符文件流FileReader和FileWriter类来实现文本的读入和输出,利用RandomAccessFile来实现对敏感词汇的过滤分析,最终达到了任务要求的分析功能。 本程序首先创建了一个主类实现窗口以及主方法的调用,设置了4个默认属性类完成相关的功能性的各项功能。在主类中,实现了对窗口对象的实例化,注册了主方法,从而生成了调用其他默认属性类及对象和方法的接口,构成了整个程序的中间部分。其余的属性类也定义了各种方法和实例对象,分别完成了捕获异常,词汇分析,文件选择等功能。 三、详细设计 3.1.1 CFileChooseLis模块   设置监听器与触发器 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JTextField; class CFileChooseLis implements ActionListener {     public CFileChooseLis(JTextField rst) {         t = rst;     }     JTextField t;     public void actionPerformed(ActionEvent e) {         if (t == null) return; CFileChooser chooser = new CFileChooser();         if (chooser.choosedFile()) {             File f = chooser.getSelectedFile();             t.setText(f.getAbsolutePath());         }     } } 3.1.2 CFileChooser模块:   筛选txt文件 import java.io.File; import javax.swing.JFileChooser; class CFileChooser extends JFileChooser {     // @override     public boolean accept(File f) {         if (f == null) return false;         if (f.isDirectory()) return true;         if (f.getAbsolutePath().endsWith(".txt"))             return true;         return false;}     public boolean choosedFile() {         int state = this.showOpenDialog(null);         File f = this.getSelectedFile();         if (accept(f) && state == JFileChooser.APPROVE_OPTION) {return true;}         return false;} } 3.1.3 CAnalyzeLis模块:   检索文件中的敏感词汇,并显示各敏感词汇的次数 import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.*; public class CAnalyzeLis implements ActionListener {         File analyzeF;         File dictionaryF;         File replaceF;         JTextArea rst;         Window window;         public CAnalyzeLis(JFileTextChecke w, JTextArea tcomp) {             analyzeF = w.getAnalyzeFile();             dictionaryF = w.getDictionFile();             replaceF=w.getreplaceFile();             rst = tcomp;             window = w;         }         public void actionPerformed(ActionEvent e) {             ((JFileTextChecke)window).initFile();             analyzeF = ((JFileTextChecke)window).getAnalyzeFile();             dictionaryF = ((JFileTextChecke)window).getDictionFile();             replaceF=((JFileTextChecke)window).getreplaceFile();             if (analyzeF == null) {                 JOptionPane.showMessageDialog(window,                 "缺少分析文件.");                 return;             }             if (dictionaryF == null) {                 JOptionPane.showMessageDialog(window,                 "缺少词典文件.");                 return;             }             if (replaceF == null) {                 JOptionPane.showMessageDialog(window,                 "缺少替换文件.");                 return;             }     Set set = new TreeSet();     Set sat = new TreeSet();             String text = null;                       Hashtable table = new Hashtable();             try {                 text = readTextFormFile(analyzeF);                                 readWordFormFile(set, dictionaryF);                 readWordFormFile(sat,replaceF );                 Iterator t = set.iterator();                 Iterator a = set.iterator();                 while (t.hasNext()&&a.hasNext()) {                                     String s = t.next();                 String sa=a.next();             if (s.length() >= 1&&sa.length()>=1) {                     String reg = s;  String regex=sa;                                           Pattern pat = Pattern.compile(reg);                         Matcher mat = pat.matcher(text);                         while (mat.find() ) {                             if (table.get(s) != null) {                                 table.get(s).add();}                             else {                                                   table.put(s, new IntString(1));                                                                                           }                                                                                                                 }                                                     }                                         }                   Iterator t2 = table.keySet().iterator();     StringBuffer result = new StringBuffer();                             while (t2.hasNext()) {                     String r = t2.next();                     result.append(r);                     result.append(" : ");                     result.append("" + table.get(r).getNumber());                     result.append('\n');                 }                 rst.setText(result.toString());             } catch (Exception ev) {                 ev.printStackTrace();                 JOptionPane.showMessageDialog(window,                         "文件读取错误.");             }         }         public static void readWordFormFile(Set s, File f) {             if (s== null || f== null) return;             BufferedReader br;             String str;             try {                 br = new BufferedReader(                         new FileReader(f));                 while ((str = br.readLine()) != null) {                     s.add(str.trim());                 }                 br.close();             } catch (Exception e) {}         }                 public static String readTextFormFile(File f) {             if (f== null) return null;             StringBuffer bf = new StringBuffer();             BufferedReader br;             String str;             try {                 br = new BufferedReader(new FileReader(f));                 while ((str = br.readLine()) != null) {                     bf.append(str);                     bf.append('\n');                 }                 br.close();                 return bf.toString();             } catch (Exception e) {}             return null;         }     } 3.1.4 JFileTextChecke模块:   形成基本的图形界面 import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.*; import javax.swing.*; public class JFileTextChecke extends JFrame{     public static void main(String[] args) {             new JFileTextChecke("词汇检测");       }     JButton button[];     JLabel label[];     JTextArea text;     File f1,f2,f3;     JTextField text1,text2,text3;     public  JFileTextChecke(String s){           setTitle(s);         button=new JButton[4];         button[0]=new JButton("打开文件");         button[1]=new JButton("打开文件");         button[2]=new JButton("分析");         button[3]=new JButton("打开文件");         label=new JLabel[3];         label[0]=new JLabel("选择文本文件");         label[1]=new JLabel("选择词典文件");         label[2]=new JLabel("选择替换文件");         text1=new JTextField(20);         text2=new JTextField(20);         text3=new JTextField(20);         text=new JTextArea(20,20);         setSize(500,500);         setResizable(false);         setLocation(130, 100);         //add(button[0]);      add(button[1]);    add(button[2]);    add(button[3]);         //add(label[0]);      add(label[1]);    add(label[2]);         //add(text1);          add(text2);        add(text3);        add(text);         iniFrame();         setVisible(true);         }         void iniFrame() {             text1.setEditable(false);             text2.setEditable(false);             text3.setEditable(false);             new JScrollPane(text);             //add window listener             addWindowListener(new WindowAdapter() {                 public void windowClosing(WindowEvent e) {                     System.exit(0);                 }             });             setLayout(new GridLayout(2 , 1));             Panel p1 = new Panel();             Panel p0=new Panel();             p1.setLayout(new GridLayout(7 , 1));             Panel p10 = new Panel();             Panel p11 = new Panel();             Panel p12 = new Panel();             Panel p13 = new Panel();             Panel p14 = new Panel();             Panel p15 = new Panel();             Panel p16 = new Panel();             p10.setLayout(new FlowLayout());             p11.setLayout(new FlowLayout());             p12.setLayout(new FlowLayout());             p13.setLayout(new FlowLayout());             p14.setLayout(new FlowLayout());             p15.setLayout(new FlowLayout());             p16.setLayout(new FlowLayout());                   p10.add(label[0]);             p11.add(text1);             p11.add(button[0]);             p12.add(label[1]);             p13.add(text2);             p13.add(button[1]);             p14.add(label[2]);             p15.add(text3);              p15.add(button[3]);             p16.add(button[2]);             p1.add(p10);             p1.add(p11);             p1.add(p12);             p1.add(p13);             p1.add(p14);             p1.add(p15);             p1.add(p16);             p0.add(text);             add(p1);             add(p0);             validate();             initFile();               button[0].addActionListener(new CFileChooseLis(text1));             button[1].addActionListener(new CFileChooseLis(text2));             button[3].addActionListener(new CFileChooseLis(text3));             button[2].addActionListener(new CAnalyzeLis(this, text));         }           public File getAnalyzeFile() {return f1;}             public File getDictionFile() {return f2;}             public File getreplaceFile(){return f3;}             public void initFile() {             f1 = text1.getText().equals("") ? null :                 new File(text1.getText());             f2 = text2.getText().equals("") ? null :                 new File(text2.getText());             f3 = text3.getText().equals("") ? null :                 new File(text3.getText());                     }                 } 3.1.5 IntString模块 计算各敏感词汇所出现的次数 public class IntString extends Object {     int num = 0;     public IntString(int i) {num = i; }     public int getNumber() {return num;}     public void add() {num += 1;} } 3.2 函数的调用关系反映了本演示程序的层次结构 CAnalyzeLis默认属性类,完成敏感词汇的分析 四、调试分析 (1在JfileTextChecke类中,运行过程中出现按钮不显示的错误 原因:流式布局的默认分布模式是按着行,自动进行分布,沾满为止 解决:通过查找相关文献改变文本框,文本区组件的大小尺寸,最终调至合适尺寸,显示出了各个组件 (2)在CFileChooser类中,出现实例对象无法找到的错误    原因:类的继承过程中要继承的包没有引入    解决办法:引入java.io.File包解决问题 (3)在CanalyzeLis类中运行过程中出现异常无法正常捕获的错误    原因:try...catch语句搭配使用应注意顺序和异常事件监听    解决办法:改变catch语句的位置并加入finally关键字解决 五、用户手册 本程序的运行环境是win7操作系统,文件名为敏感词汇.txt. 六、测试数据                     菜单显示           调出被分析的文件所在的目录               分析过后的结果显示                   被分析的文件                         敏感词汇文件               替换词汇文件                   被替换后的原文件
/
本文档为【敏感词汇分析】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
热门搜索

历史搜索

    清空历史搜索