From 70a9b024201e8b3853a1402f51c7c4fa739f10cf Mon Sep 17 00:00:00 2001 From: piraat <942905677@qq.com> Date: Sun, 7 Oct 2018 16:07:56 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20053=20086/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 053 086/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 053 086/.keep diff --git a/053 086/.keep b/053 086/.keep new file mode 100644 index 0000000..e69de29 -- Gitee From df5af0dcca9961e26685a2c694ec9c6fdc21bb93 Mon Sep 17 00:00:00 2001 From: piraat <942905677@qq.com> Date: Sun, 7 Oct 2018 16:37:58 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20src/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 053 086/src/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 053 086/src/.keep diff --git a/053 086/src/.keep b/053 086/src/.keep new file mode 100644 index 0000000..e69de29 -- Gitee From 75ab32e5599f0f6f5551b1ffc369aa2b4002f385 Mon Sep 17 00:00:00 2001 From: piraat <942905677@qq.com> Date: Sun, 7 Oct 2018 16:38:06 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20wordConut/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 053 086/src/wordConut/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 053 086/src/wordConut/.keep diff --git a/053 086/src/wordConut/.keep b/053 086/src/wordConut/.keep new file mode 100644 index 0000000..e69de29 -- Gitee From b0cf956380572cb924aad542d30d7508eeddfea5 Mon Sep 17 00:00:00 2001 From: piraat <942905677@qq.com> Date: Sun, 7 Oct 2018 16:38:34 +0800 Subject: [PATCH 4/5] Upload Main.java FileIO.java WordCount.java --- 053 086/src/wordConut/FileIO.java | 77 +++++++++++++ 053 086/src/wordConut/Main.java | 84 +++++++++++++++ 053 086/src/wordConut/WordCount.java | 155 +++++++++++++++++++++++++++ 3 files changed, 316 insertions(+) create mode 100644 053 086/src/wordConut/FileIO.java create mode 100644 053 086/src/wordConut/Main.java create mode 100644 053 086/src/wordConut/WordCount.java diff --git a/053 086/src/wordConut/FileIO.java b/053 086/src/wordConut/FileIO.java new file mode 100644 index 0000000..53bcea8 --- /dev/null +++ b/053 086/src/wordConut/FileIO.java @@ -0,0 +1,77 @@ +package wordCount; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.IOException; +import java.io.OutputStreamWriter; + +public class FileIO { + + + + public static String readFileByStream(String filePath)throws IOException{ + FileReader reader = new FileReader(filePath); + File file = new File(filePath); + long i = file.length() ; + char[] c = new char[(int) i]; + reader.read(c); + String str = String.valueOf(c); + reader.close(); + return str; + } + + public static String[] readFile(String filePath)throws IOException{ + BufferedReader reader = null; + int i = 0 ; + String[] str = new String[120000]; + File file = new File(filePath); + if(!file.exists()) { + return null; + } + try { + reader = new BufferedReader(new FileReader(filePath)); + String tempString ; + while ((tempString = reader.readLine()) != null) { + str[i++] = tempString; + } + reader.close(); + return str; + } catch (IOException e) { + e.printStackTrace(); + return null; + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e1) { + } + } + } + } + + + + public static String writeFile(String filePath, String str) throws IOException { + BufferedWriter out = null; + try { + out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath, true))); + out.write(str); + out.newLine(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + if(out != null){ + out.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + return ""; + } + +} diff --git a/053 086/src/wordConut/Main.java b/053 086/src/wordConut/Main.java new file mode 100644 index 0000000..aafdbcd --- /dev/null +++ b/053 086/src/wordConut/Main.java @@ -0,0 +1,84 @@ +package wordCount; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.regex.Pattern; + +public class Main { + + public static void main(String[] args) throws IOException { + // TODO Auto-generated method stub + String filePath = null; + int phraseNum = 1; + int wordLim = 10; + boolean flag = false; + String resultPath = "result.txt"; + if(args == null ) { + System.out.println("please input the operation like "); + System.out.println(" java wordCount.Main [-options] -i [file]"); + System.out.println(" options including:"); + System.out.println(" -n [number]"); + System.out.println(" -m [number]"); + System.out.println(" -o [file]"); + System.exit(0); + } + else { + for (int i = 0 ; i < args.length ; i ++ ){ + if(args[i].equals("-m")) { + phraseNum = Integer.parseInt(args[i+1]); + } + if(args[i].equals("-i")) { + filePath = args[i+1]; + flag = true; + } + if(args[i].equals("-n")) { + wordLim = Integer.parseInt(args[i+1]); + } + if(args[i].equals("-o")) { + resultPath = args[i+1]; + } + } + } + if(!flag) { + System.out.println("please input the operation like "); + System.out.println(" java wordCount.Main [-options] -i [file]"); + System.out.println(" options including:"); + System.out.println(" -n [number]"); + System.out.println(" -m [number]"); + System.out.println(" -o [file]"); + System.exit(0); + } + String[] strs = FileIO.readFile(filePath); + if(strs == null) { + System.out.println("couldn't find the file"); + System.exit(0); + } + String str = FileIO.readFileByStream(filePath); + ArrayList words = WordCount.countWords(strs); + ArrayList phrase = WordCount.countWords(strs, phraseNum); + System.out.println("characters: "+ WordCount.countChar(str)); + FileIO.writeFile(resultPath, "characters: "+ WordCount.countChar(str)); + FileIO.writeFile(resultPath, "words: "+ words.size()); + FileIO.writeFile(resultPath, "lines: "+ WordCount.countLines(strs)); + System.out.println("words: "+ words.size()); + System.out.println("lines: "+ WordCount.countLines(strs)); + for(int i = 0 ; i < wordLim ; i++ ) { + if(i == words.size()) { + break; + } + FileIO.writeFile(resultPath, words.get(i)); + System.out.println(words.get(i)); + } + if(phraseNum != 1) { + for(int i = 0 ; i < wordLim ; i++ ) { + if(i == phrase.size()) { + break; + } + FileIO.writeFile(resultPath, phrase.get(i)); + System.out.println(phrase.get(i)); + } + } + } + +} diff --git a/053 086/src/wordConut/WordCount.java b/053 086/src/wordConut/WordCount.java new file mode 100644 index 0000000..e9b5e44 --- /dev/null +++ b/053 086/src/wordConut/WordCount.java @@ -0,0 +1,155 @@ +package wordCount; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class WordCount { + + + + public static int countChar(String strs) { + char c ; + int sum = 0; + for (int i = 0; i < strs.length(); i++) { + c = strs.charAt(i); + if (c >= 32 && c <= 126 || c == '\r' || c == '\n'|| c == '\t') { + sum++; + } + } + return sum; + } + + public static int countLines(String[] strs) { + int sum = 0; + for (int i = 0; i < strs.length; i++) { + if ( strs[i] == null) { + break; + } + if (strs[i].trim().length() == 0 || strs[i].trim().equals("")) { + continue; + } + sum++; + } + return sum; + } + + public static ArrayList countWords(String[] strs) { + ArrayList result = new ArrayList(); + Map map = new HashMap(); + Pattern p = Pattern.compile("\\b[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z][A-Za-z0-9]*\\b"); + for (int i = 0; i < strs.length; i++) { + if ( strs[i] == null) { + continue; + } + if (strs[i].length() == 0 || strs[i].equals("")) { + continue; + } + Matcher m = p.matcher(strs[i]); + while (m.find()) { + String mstr = m.group(); + if (map.containsKey(mstr)) { + map.put(mstr, map.get(mstr) + 1); + } else { + map.put(mstr, 1); + } + } + } + List> list = new ArrayList>(map.entrySet()); + Collections.sort(list, new Comparator>() { + public int compare(Entry o1, Entry o2) { + if (o1.getValue() == o2.getValue()) { + return o1.getKey().compareTo(o2.getKey()); + } + return o2.getValue() - o1.getValue(); + } + + }); + for(Map.Entry mapping:list){ + result.add("<"+mapping.getKey().toLowerCase()+">" + ": " + mapping.getValue()); + } + + return result; + } + + + public static ArrayList countWords(String[] strs, int phraseNum) { + ArrayList result = new ArrayList(); + ArrayList wordTmp = new ArrayList(); + Map map = new HashMap(); + Pattern p = Pattern.compile("\\b[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z][A-Za-z0-9]*\\b"); + String[] phraseTmp = new String[phraseNum]; + for (int i = 0; i < phraseTmp.length; i++) { + phraseTmp[i] = ""; + } + Integer[] phraseTime = new Integer[phraseNum]; + for(int i = 0 ; i < phraseNum ; i ++ ) { + phraseTime[i] = 0 ; + } + for (int i = 0; i < strs.length; i++) { + if ( strs[i] == null) { + continue; + } + if (strs[i].length() == 0 || strs[i].equals("")) { + continue; + } + Matcher m = p.matcher(strs[i]); + while (m.find()) { + String mstr = m.group(); + wordTmp.add(mstr); + } + } + System.out.println(wordTmp.size()); + for(int i = 0 ; i < wordTmp.size() ; i ++ ) { + if(i < phraseNum) { + for(int k = 0 ; k < i + 1; k ++ ) { + phraseTmp[k] = phraseTmp[k] + " " + wordTmp.get(i); + phraseTime[k] ++; + } + } + if(i == phraseNum){ + map.put(phraseTmp[0], 1); + phraseTmp[0] = "" ; + phraseTime[0] = 0; + } + if(i >= phraseNum) { + for(int k = 0 ; k < phraseNum ; k ++ ) { + phraseTmp[k] = phraseTmp[k] + " " + wordTmp.get(i); + phraseTime[k] ++; + if(phraseTime[k] == phraseNum) { + if (map.containsKey(phraseTmp[k])) { + map.put(phraseTmp[k], map.get(phraseTmp[k]) + 1); + phraseTmp[k] = "" ; + phraseTime[k] = 0; + } else { + map.put(phraseTmp[k], 1); + phraseTmp[k] = "" ; + phraseTime[k] = 0; + } + } + } + } + } + List> list = new ArrayList>(map.entrySet()); + Collections.sort(list, new Comparator>() { + public int compare(Entry o1, Entry o2) { + if (o1.getValue() == o2.getValue()) { + return o1.getKey().compareTo(o2.getKey()); + } + return o2.getValue() - o1.getValue(); + } + + }); + for(Map.Entry mapping:list){ + result.add("<"+mapping.getKey().toLowerCase()+" >" + ": " + mapping.getValue()); + } + + return result; + } +} -- Gitee From 31ad581aad3d908b91f49680d8331135e71ec167 Mon Sep 17 00:00:00 2001 From: piraat <942905677@qq.com> Date: Mon, 8 Oct 2018 20:48:27 +0800 Subject: [PATCH 5/5] Upload Main.java Menu.java FileIO.java WordCount.java --- 053 086/src/wordConut/Main.java | 2 +- 053 086/src/wordConut/Menu.java | 473 +++++++++++++++++++++++++++ 053 086/src/wordConut/WordCount.java | 3 +- 3 files changed, 475 insertions(+), 3 deletions(-) create mode 100644 053 086/src/wordConut/Menu.java diff --git a/053 086/src/wordConut/Main.java b/053 086/src/wordConut/Main.java index aafdbcd..e9e6ee8 100644 --- a/053 086/src/wordConut/Main.java +++ b/053 086/src/wordConut/Main.java @@ -57,10 +57,10 @@ public class Main { String str = FileIO.readFileByStream(filePath); ArrayList words = WordCount.countWords(strs); ArrayList phrase = WordCount.countWords(strs, phraseNum); - System.out.println("characters: "+ WordCount.countChar(str)); FileIO.writeFile(resultPath, "characters: "+ WordCount.countChar(str)); FileIO.writeFile(resultPath, "words: "+ words.size()); FileIO.writeFile(resultPath, "lines: "+ WordCount.countLines(strs)); + System.out.println("characters: "+ WordCount.countChar(str)); System.out.println("words: "+ words.size()); System.out.println("lines: "+ WordCount.countLines(strs)); for(int i = 0 ; i < wordLim ; i++ ) { diff --git a/053 086/src/wordConut/Menu.java b/053 086/src/wordConut/Menu.java new file mode 100644 index 0000000..b3bfc60 --- /dev/null +++ b/053 086/src/wordConut/Menu.java @@ -0,0 +1,473 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package wordCount; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.JFileChooser; +import javax.swing.JLabel; + +/** + * + * @author Administrator + */ +public class Menu extends javax.swing.JFrame { + + /** + * Creates new form Menu + */ + public Menu() { + initComponents(); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // + private void initComponents() { + + jPanel1 = new javax.swing.JPanel(); + wordLimB = new javax.swing.JRadioButton(); + fileP = new javax.swing.JTextField(); + wordL = new javax.swing.JTextField(); + phraseN = new javax.swing.JTextField(); + resultF = new javax.swing.JTextField(); + phraseNumB = new javax.swing.JRadioButton(); + jPanel3 = new javax.swing.JPanel(); + operation = new javax.swing.JTextField(); + operationB = new javax.swing.JRadioButton(); + sourceFileChoose = new javax.swing.JButton(); + resultFileChoose = new javax.swing.JButton(); + flag = new javax.swing.JButton(); + jScrollPane2 = new javax.swing.JScrollPane(); + result = new javax.swing.JTextArea(); + jScrollBar1 = new javax.swing.JScrollBar(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + + jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("支持命令行与文件选择")); + + wordLimB.setText("单词排名限制"); + wordLimB.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + wordLimBActionPerformed(evt); + } + }); + + wordL.setText("10"); + wordL.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + wordLActionPerformed(evt); + } + }); + + phraseN.setText("1"); + + resultF.setText("result.txt"); + + phraseNumB.setText("词组单词数量"); + phraseNumB.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + phraseNumBActionPerformed(evt); + } + }); + + jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("命令行输入")); + + operation.setText("-n 10 -m 1 -i text.txt -o result.txt"); + operation.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + operationActionPerformed(evt); + } + }); + + operationB.setText("是否启用命令行输入"); + operationB.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + operationBActionPerformed(evt); + } + }); + + javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); + jPanel3.setLayout(jPanel3Layout); + jPanel3Layout.setHorizontalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap() + .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(jPanel3Layout.createSequentialGroup().addComponent(operationB).addGap(0, 0, + Short.MAX_VALUE)) + .addComponent(operation, javax.swing.GroupLayout.DEFAULT_SIZE, 236, Short.MAX_VALUE)) + .addContainerGap())); + jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap() + .addComponent(operation, javax.swing.GroupLayout.PREFERRED_SIZE, + javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(18, 18, 18).addComponent(operationB) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); + + sourceFileChoose.setText("选择输入文本"); + sourceFileChoose.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + sourceFileChooseActionPerformed(evt); + } + }); + + resultFileChoose.setText("选择输出文本"); + resultFileChoose.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + resultFileChooseActionPerformed(evt); + } + }); + + flag.setText("上传"); + flag.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + try { + flagActionPerformed(evt); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + }); + + javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); + jPanel1.setLayout(jPanel1Layout); + jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout + .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, + javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, + Short.MAX_VALUE) + .addGroup(jPanel1Layout.createSequentialGroup().addComponent(resultFileChoose).addGap(5, 5, 5) + .addComponent(resultF)) + .addGroup(jPanel1Layout.createSequentialGroup() + .addGroup(jPanel1Layout + .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) + .addComponent(sourceFileChoose, javax.swing.GroupLayout.DEFAULT_SIZE, + javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(wordLimB, javax.swing.GroupLayout.DEFAULT_SIZE, + javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent( + phraseNumB, javax.swing.GroupLayout.DEFAULT_SIZE, + javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(jPanel1Layout.createSequentialGroup().addGap(6, 6, 6) + .addGroup(jPanel1Layout + .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(wordL).addComponent(phraseN))) + .addGroup(jPanel1Layout.createSequentialGroup() + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(fileP)))) + .addComponent(flag, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, + Short.MAX_VALUE)) + .addContainerGap())); + jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() + .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(fileP, javax.swing.GroupLayout.PREFERRED_SIZE, + javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(sourceFileChoose)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(wordLimB).addComponent(wordL, javax.swing.GroupLayout.PREFERRED_SIZE, + javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(phraseN, javax.swing.GroupLayout.PREFERRED_SIZE, + javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(phraseNumB)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(resultF, javax.swing.GroupLayout.PREFERRED_SIZE, + javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(resultFileChoose)) + .addGap(18, 18, 18) + .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, + javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(flag) + .addContainerGap(11, Short.MAX_VALUE))); + + result.setColumns(20); + result.setRows(5); + result.setText( + "please input the operation like:\n\t[-options] -i [file]\n\t\toptions including:\n\t\t-n [number]\n\t\t-m [number]\n\t\t-o [file]"); + jScrollPane2.setViewportView(result); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup().addContainerGap() + .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, + javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 259, + javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jScrollBar1, javax.swing.GroupLayout.PREFERRED_SIZE, + javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(33, 33, 33))); + layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout + .createSequentialGroup().addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, + javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jScrollPane2).addComponent(jScrollBar1, javax.swing.GroupLayout.DEFAULT_SIZE, + javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addContainerGap())); + + pack(); + }// + + private void operationActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + } + + private void wordLActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + } + + private void sourceFileChooseActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + JFileChooser chooser = new JFileChooser(); + chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); + chooser.showDialog(new JLabel(), "选择"); + File file = chooser.getSelectedFile(); + fileP.setText(file.getAbsoluteFile().toString()); + + } + + private void wordLimBActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + wordL.setEditable(wordLimB.isSelected()); + } + + private void resultFileChooseActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + JFileChooser chooser = new JFileChooser(); + chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); + chooser.showDialog(new JLabel(), "选择"); + File file = chooser.getSelectedFile(); + resultF.setText(file.getAbsoluteFile().toString()); + } + + private void phraseNumBActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + phraseN.setEditable(phraseNumB.isSelected()); + } + + private void operationBActionPerformed(java.awt.event.ActionEvent evt) { + // TODO add your handling code here: + operation.setEditable(operationB.isSelected()); + } + + private void flagActionPerformed(java.awt.event.ActionEvent evt) throws IOException { + // TODO add your handling code here: + String filePath = null; + int phraseNum = 1; + int wordLim = 10; + boolean flag = false; + String resultPath = "result.txt"; + String r = null; + if (operationB.isSelected()) { + String[] op = operation.getText().split("\\s+"); + if (op == null) { + result.setText( + "[-options] -i [file]\n options including:\n -n [number]\n -m [number]\n -o [file]"); + } else { + for (int i = 0; i < op.length; i++) { + if (op[i].equals("-m")) { + phraseNum = Integer.parseInt(op[i + 1]); + } + if (op[i].equals("-i")) { + filePath = op[i + 1]; + flag = true; + } + if (op[i].equals("-n")) { + wordLim = Integer.parseInt(op[i + 1]); + } + if (op[i].trim().equals("-o")) { + resultPath = op[i + 1]; + } + } + } + if (!flag) { + result.setText( + "[-options] -i [file]\n options including:\n -n [number]\n -m [number]\n -o [file]"); + } + String[] strs = null; + String str = null; + try { + strs = FileIO.readFile(filePath); + str = FileIO.readFileByStream(filePath); + } catch (IOException ex) { + Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex); + result.setText("couldn't find the file"); + } + if (strs == null || str == null) { + result.setText("couldn't find the file"); + } + System.out.println(resultPath); + ArrayList words = WordCount.countWords(strs); + ArrayList phrase = WordCount.countWords(strs, phraseNum); + r = "characters: " + WordCount.countChar(str) + "\n"; + r += "words: " + words.size() + "\n"; + r += "lines: " + WordCount.countLines(strs) + "\n"; + FileIO.writeFile(resultPath, "characters: " + WordCount.countChar(str)); + try { + FileIO.writeFile(resultPath, "characters: " + WordCount.countChar(str)); + FileIO.writeFile(resultPath, "words: " + words.size()); + FileIO.writeFile(resultPath, "lines: " + WordCount.countLines(strs)); + } catch (IOException ex) { + Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex); + result.setText("couldn't write the file"); + } + for (int i = 0; i < wordLim; i++) { + if (i == words.size()) { + break; + } + try { + FileIO.writeFile(resultPath, words.get(i)); + } catch (IOException ex) { + Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex); + } + r += words.get(i) + "\n"; + } + if (phraseNum != 1) { + for (int i = 0; i < wordLim; i++) { + if (i == phrase.size()) { + break; + } + try { + FileIO.writeFile(resultPath, phrase.get(i)); + } catch (IOException ex) { + Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex); + } + r += phrase.get(i) + "\n"; + } + } + result.setText(r); + } else { + filePath = fileP.getText(); + wordLim = Integer.parseInt(wordL.getText()); + phraseNum = Integer.parseInt(phraseN.getText()); + resultPath = resultF.getText(); + String[] strs = null; + String str = null; + try { + strs = FileIO.readFile(filePath); + str = FileIO.readFileByStream(filePath); + } catch (IOException ex) { + Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex); + result.setText("couldn't find the file"); + } + if (strs == null || str == null) { + result.setText("couldn't find the file"); + } + ArrayList words = WordCount.countWords(strs); + ArrayList phrase = WordCount.countWords(strs, phraseNum); + r = "characters: " + WordCount.countChar(str) + "\n"; + r += "words: " + words.size() + "\n"; + r += "lines: " + WordCount.countLines(strs) + "\n"; + try { + FileIO.writeFile(resultPath, "characters: " + WordCount.countChar(str)); + FileIO.writeFile(resultPath, "words: " + words.size()); + FileIO.writeFile(resultPath, "lines: " + WordCount.countLines(strs)); + } catch (IOException ex) { + Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex); + result.setText("couldn't write the file"); + } + for (int i = 0; i < wordLim; i++) { + if (i == words.size()) { + break; + } + try { + FileIO.writeFile(resultPath, words.get(i)); + } catch (IOException ex) { + Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex); + } + r += words.get(i) + "\n"; + } + if (phraseNum != 1) { + for (int i = 0; i < wordLim; i++) { + if (i == phrase.size()) { + break; + } + try { + FileIO.writeFile(resultPath, phrase.get(i)); + } catch (IOException ex) { + Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex); + } + r += phrase.get(i) + "\n"; + } + } + result.setText(r); + } + } + + /** + * @param args + * the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + // + /* + * If Nimbus (introduced in Java SE 6) is not available, stay with the default + * look and feel. For details see + * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(Menu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new Menu().setVisible(true); + } + }); + } + + // Variables declaration - do not modify + private javax.swing.JTextField fileP; + private javax.swing.JButton flag; + private javax.swing.JPanel jPanel1; + private javax.swing.JPanel jPanel3; + private javax.swing.JScrollBar jScrollBar1; + private javax.swing.JScrollPane jScrollPane2; + private javax.swing.JTextField operation; + private javax.swing.JRadioButton operationB; + private javax.swing.JTextField phraseN; + private javax.swing.JRadioButton phraseNumB; + private javax.swing.JTextArea result; + private javax.swing.JTextField resultF; + private javax.swing.JButton resultFileChoose; + private javax.swing.JButton sourceFileChoose; + private javax.swing.JTextField wordL; + private javax.swing.JRadioButton wordLimB; + // End of variables declaration +} diff --git a/053 086/src/wordConut/WordCount.java b/053 086/src/wordConut/WordCount.java index e9b5e44..4d5ad53 100644 --- a/053 086/src/wordConut/WordCount.java +++ b/053 086/src/wordConut/WordCount.java @@ -105,8 +105,7 @@ public class WordCount { wordTmp.add(mstr); } } - System.out.println(wordTmp.size()); - for(int i = 0 ; i < wordTmp.size() ; i ++ ) { + for(int i = 0 ; i < wordTmp.size() ; i ++ ) { if(i < phraseNum) { for(int k = 0 ; k < i + 1; k ++ ) { phraseTmp[k] = phraseTmp[k] + " " + wordTmp.get(i); -- Gitee