基于javaGUI的文档识别工具制作
生活随笔
收集整理的這篇文章主要介紹了
基于javaGUI的文档识别工具制作
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
基于javaGUI的文檔識別工具制作
對于某些文本,其中富含了一些標志,需要去排除,以及去獲得段落字數,以下是我個人寫的一個比較簡單的文檔識別工具,含導入文件、導出文件以及一個簡單的識別功能。
1、功能實現
以下功能實現純屬于個人想法,可能會有bug,僅供參考
/*** 去除字符串中的"。",";","?","!",“,”* 將所有其他多余的符號轉換為" "* @param str 獲取到的每行文字*/
public static String replaceStr(String str){String new_str = str.replaceAll(";|。|,|?|!|:|”|“|∶|:|,|!|;|—"," ");return new_str;}
將文本中的一些斷句的中文標點和英文字符用兩個空格來替換(為什么用兩個空格呢?因為在我的測試文本中可能因為一些個人原因,存在一些單空格來,可能是打字太快導致的),然后利用splite(“ ”)來將句子切割,代碼如下:
String new_tempString = replaceStr(tempString);
String[] strings = new_tempString.split(" ");
再通過識別中文,進行獲取段落中的字數,代碼如下:
// 使用Unicode編碼范圍來判斷漢字;這個方法不準確,因為還有很多漢字不在這個范圍之內public static boolean isChineseByRange(String str) {if (str == null) {return false;}Pattern pattern = Pattern.compile("[\\u4E00-\\u9FCC]+");return pattern.matcher(str.trim()).find();}
2、布局設置
在這里,我使用的是GridBagLayout來進行布局,同時使用JPanel和JScrollPane來作為中間容器。
上圖是我所設計的圖形用戶界面,整體代碼:
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.regex.Pattern;public class GUIdesign extends JFrame {public static void main(String[] args) {GUIdesign jf = new GUIdesign();jf.setTitle("文檔識別工具1.0");jf.setSize(400,300);jf.setLocation(400,200);GridBagLayout gridBagLayout1 = new GridBagLayout();GridBagConstraints c1 = new GridBagConstraints();jf.setLayout(gridBagLayout1);c1.gridheight = 1;c1.gridwidth = GridBagConstraints.REMAINDER;c1.weightx = 2;c1.weighty = 2;GridBagLayout gridBagLayout2 = new GridBagLayout();GridBagConstraints c2 = new GridBagConstraints();JPanel jPanel1 = new JPanel();jPanel1.setLayout(gridBagLayout2);/** 第一欄 請選擇文件 + textFiled + 導入按鈕*/c2.gridwidth = 1;c2.gridheight = 1;c2.weightx = 3;c2.weighty = 3;JLabel jLabel1 = new JLabel("請選擇文件");gridBagLayout2.setConstraints(jLabel1,c2);jPanel1.add(jLabel1);c2.gridwidth = 1;c2.gridheight = 1;c2.weightx = 3;c2.weighty = 3;JTextField jTextField1 = new JTextField(10);gridBagLayout2.setConstraints(jTextField1,c2);jPanel1.add(jTextField1);c2.gridwidth = GridBagConstraints.REMAINDER;c2.gridheight = 1;c2.weightx = 3;c2.weighty = 3;JButton jButton1 = new JButton("導入");gridBagLayout2.setConstraints(jButton1,c2);jPanel1.add(jButton1);/** 第二欄 請輸入關鍵字 + textFiled + 識別按鈕*/c2.gridwidth = 1;c2.gridheight = 1;c2.weightx = 3;c2.weighty = 3;JLabel jLabel2 = new JLabel("請輸入關鍵字");gridBagLayout2.setConstraints(jLabel2,c2);jPanel1.add(jLabel2);c2.gridwidth = 1;c2.gridheight = 1;c2.weightx = 3;c2.weighty = 3;JTextField jTextField2 = new JTextField(10);gridBagLayout2.setConstraints(jTextField2,c2);jPanel1.add(jTextField2);c2.gridwidth = GridBagConstraints.REMAINDER;c2.gridheight = 1;c2.weightx = 3;c2.weighty = 3;JButton jButton2 = new JButton("識別");gridBagLayout2.setConstraints(jButton2,c2);jPanel1.add(jButton2);/** 第三欄 導出按鈕*/c2.gridwidth = GridBagConstraints.REMAINDER;c2.gridheight = 1;c2.weightx = 3;c2.weighty = 3;JButton jButton3 = new JButton("導出");gridBagLayout2.setConstraints(jButton3,c2);jPanel1.add(jButton3);gridBagLayout1.setConstraints(jPanel1,c1);jf.add(jPanel1);/*文本框,設置垂直滾動條,取消水平滾動條*/c1.gridheight = 2;c1.gridwidth = GridBagConstraints.REMAINDER;c1.weightx = 3;c1.weighty = 3;JScrollPane jScrollPane = new JScrollPane();jScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);JTextArea jTextArea = new JTextArea(10,30);jScrollPane.setViewportView(jTextArea);gridBagLayout1.setConstraints(jScrollPane,c1);jf.add(jScrollPane);//導入按鈕事件,選擇文件,獲取路徑jButton1.addActionListener(ActionListener-> {jTextArea.setText("");JFileChooser chooser = new JFileChooser();chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);chooser.showDialog(new JLabel(), "選擇");File file = chooser.getSelectedFile();jTextField1.setText(file.getAbsolutePath());});//識別按鈕事件,獲取textfield文本,獲取文件文本,并判斷jButton2.addActionListener(ActionListener->{jTextArea.setText("");File file = new File(jTextField1.getText());System.out.println(file);BufferedReader br = null;try {String keyWord = jTextField2.getText();System.out.println(keyWord);br = new BufferedReader(new FileReader(file));String tempString = null;int line = 1;while ((tempString = br.readLine())!=null){String new_tempString = replaceStr(tempString);String[] strings = new_tempString.split(" ");int count = 0;for (String str : strings){int code = 0;if (str.contains(keyWord)){char[] chars = str.toCharArray();for (char aChar : chars) {if (isChineseByRange(String.valueOf(aChar))){code++;}}count++;jTextArea.append("line"+line+",第"+count+"個"+",字數:"+code+"\n");System.out.println(str);System.out.println("line"+line+",第"+count+"個"+",字數:"+code);}}line++;}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {if (br!=null){try {br.close();} catch (IOException e) {e.printStackTrace();}}}});//導出按鈕事件,將顯示的文字導出成txt文件jButton3.addActionListener(ActionListener->{int result = 0;File file = null;String path = null;Component chatFrame = null;JFileChooser fileChooser = new JFileChooser();FileSystemView fsv = FileSystemView.getFileSystemView(); //注意了,這里重要的一句System.out.println(fsv.getHomeDirectory()); //得到桌面路徑fileChooser.setCurrentDirectory(fsv.getHomeDirectory());fileChooser.setDialogTitle("請選擇要上傳文件的路徑");fileChooser.setApproveButtonText("確定");fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);result = fileChooser.showOpenDialog(chatFrame);if (JFileChooser.APPROVE_OPTION == result) {path=fileChooser.getSelectedFile().getPath();System.out.println("path: "+path);file = new File(path);PrintWriter pw = null;try {pw=new PrintWriter(file);pw.write(jTextArea.getText());pw.flush();} catch (FileNotFoundException e) {e.printStackTrace();}finally {pw.close();}}});windowsClose(jf);jf.setVisible(true);}//關閉窗體事件private static void windowsClose(JFrame jFrame){jFrame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});}/*** 去除字符串中的"。",";","?","!",“,”* 將所有其他多余的符號轉換為" "* @param str 獲取到的每行文字*/public static String replaceStr(String str){String new_str = str.replaceAll(";|。|,|?|!|:|”|“|∶|:|,|!|;|—"," ");return new_str;}// 使用Unicode編碼范圍來判斷漢字;這個方法不準確,因為還有很多漢字不在這個范圍之內public static boolean isChineseByRange(String str) {if (str == null) {return false;}Pattern pattern = Pattern.compile("[\\u4E00-\\u9FCC]+");return pattern.matcher(str.trim()).find();}
}
以上是我寫該小工具的所有代碼,也許功能并不全面,僅為一些java初學GUI的朋友們提供圖形界面設計和功能結合的思路。如有錯誤,請指正。
總結
以上是生活随笔為你收集整理的基于javaGUI的文档识别工具制作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用0到9十个数字,每个数字使用一次,构成
- 下一篇: 使用Spring容器