南邮大作业----文本处理与加密软件

来源:互联网 发布:河南网络危机公关 编辑:程序博客网 时间:2024/06/07 22:02

(一)课题内容

文本处理与加密软件要求首先把一个文本中满足特定特征的文本提取出来,然后对提取出来的文本进行加密处理。

(二)课题要求

(1)设计满足特征的文本的文法;

(2)词法分析模块结构清晰,可以借助Lex或JavaCC、Antlr等工具自动生成词法分析程序;

(3)选择某种加密算法对所提取的文本进行加密;

(4)界面美观。

这里我们采用hanlp的汉语言处理包http://hanlp.linrunsoft.com/

hanlp中文分词后还提供了关键字提取的功能(基于textrank),一般关键字采用tf-idf算法或者textrank,其中textrank可以在单文本中使用

使用Hanlp需要导入jar包和依赖数据集,并在src下提供配置文件

编码和解码采用的是Base64,Base64并不算是加密算法,只能算是编码算法,不过这里足够用了。

本程序所使用的jar包如下:hanlp-1.2.8 jar ,common-codec-1.8 jar ,JTattoo-1.611 jar


show.java

import java.awt.Container;import java.awt.Font;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.UIManager;public class show  extends JFrame implements ActionListener{JMenuBar menuBar;JMenu menu,menu2;JMenuItem menuitem,menuitem2,menuitem3,menuitem4,menuitem5;JTextArea jta;public show(){setTitle("我的关键字加密软件");setLayout(null);setBounds(800,200,400,500);  Container container=getContentPane(); menuBar=new JMenuBar();setJMenuBar(menuBar);menu=new JMenu("开始");menuBar.add(menu);menuitem=new JMenuItem("读取文件");  menu.add(menuitem);  menuitem.addActionListener(this);menuitem2=new JMenuItem("获得关键字");  menu.add(menuitem2);  menuitem2.addActionListener(this);menuitem3=new JMenuItem("加密文件");  menu.add(menuitem3);  menuitem3.addActionListener(this);menuitem5=new JMenuItem("解密文件");  menu.add(menuitem5);  menuitem5.addActionListener(this);menu2=new JMenu("退出");menuBar.add(menu2);menuitem4=new JMenuItem("退出");  menu2.add(menuitem4);  menuitem4.addActionListener(this);jta=new JTextArea();jta.setBounds(15,20,350,380);jta.setLineWrap(true);        //激活自动换行功能 jta.setWrapStyleWord(true);            // 激活断行不断字功能jta.setFont(new Font("微软雅黑",0,15));JScrollPane jsp = new JScrollPane(jta); jsp.setBounds(15,20,350,380);   container.add(jsp);setVisible(true);  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  } public void actionPerformed(ActionEvent e){   if (e.getSource()==menuitem) {   String s=my_filechooser.filechooser(); try {jta.setText(Helper.Read(s));} catch (IOException e1) {System.out.println("打开文件失败");e1.printStackTrace();} } if (e.getSource()==menuitem2) {   String s=jta.getText(); jta.setText(Helper.getKeyWord(s)); } if (e.getSource()==menuitem3) {   String s=jta.getText(); jta.setText(Helper.commonsEnCodesBase64(s)); } if (e.getSource()==menuitem4) {   System.out.println("4"); System.exit(1); } if (e.getSource()==menuitem5) {   String s=jta.getText(); jta.setText(Helper.commonsDeCodesBase64(s)); } }public static void main(String[] args) {try {          UIManager.setLookAndFeel("com.jtattoo.plaf.mint.MintLookAndFeel");          show s=new show();    }      catch (Exception ex) {          ex.printStackTrace();      }  }     }

my_filechooser.java

import java.io.File;import javax.swing.JFileChooser;import javax.swing.JLabel;public class my_filechooser { public  static String filechooser(){  JFileChooser jfc=new JFileChooser();        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );        jfc.showDialog(new JLabel(), "选择");       jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {              public boolean accept(File f) { //设定可用的文件的后缀名                  if(f.getName().endsWith(".txt")||f.isDirectory()){                    return true;                  }                  return false;                }                public String getDescription() {                  return "以txt结尾";                }    });       File file=jfc.getSelectedFile();        if(file.isDirectory()){            System.out.println("文件夹:"+file.getAbsolutePath());        }else if(file.isFile()){            System.out.println("文件:"+file.getAbsolutePath());       }       return file.getAbsolutePath();     } }

Helper.java

import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.util.List;import org.apache.commons.codec.binary.Base64;import com.hankcs.hanlp.HanLP;public class Helper { public static String Read(String path)throws IOException { BufferedReader br=null; String content=""; try{ br=new BufferedReader(new FileReader(path)); String line=null; while((line=br.readLine())!=null){     content=content+line;  } }finally{ if (br!=null){ br.close(); } } return content; } public static String commonsEnCodesBase64(String content){//编码 byte[] encodeBytes=Base64.encodeBase64(content.getBytes()); return new String(encodeBytes); } public static String commonsDeCodesBase64(String content){//解码 byte[] decodeBytes=Base64.decodeBase64(content.getBytes()); return new String(decodeBytes); } public static String getKeyWord(String s){ List<String> keywordList = HanLP.extractKeyword(s,1);  return keywordList.get(0); } }

提取关键字:

加密:

解密:


后记:不是很难的题目,一个上午就写完了,只是api的堆砌。没想到大学过的这么快,真的只剩下一年了么。想想这些年的疯狂,心头真不知是什么滋味,做完最后一个实验,努力去工作吧,gogo。最后,宝贝,爱你,又想你了。




原创粉丝点击