/src/org/ictclas4j/run/FrmMain.java

http://ictclas4j.googlecode.com/ · Java · 289 lines · 217 code · 52 blank · 20 comment · 21 complexity · 2478815698e5cb28a12c873449ffe1a3 MD5 · raw file

  1. package org.ictclas4j.run;
  2. import java.awt.BorderLayout;
  3. import java.awt.GridLayout;
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.util.ArrayList;
  10. import javax.swing.JButton;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.JPanel;
  14. import javax.swing.JScrollPane;
  15. import javax.swing.JTabbedPane;
  16. import javax.swing.JTextArea;
  17. import org.ictclas4j.bean.SegResult;
  18. public class FrmMain {
  19. private static FrmMain frmMain;
  20. private JFrame jFrame = null;
  21. private JPanel jContentPane = null;
  22. private JTabbedPane jTabbedPane = null;
  23. private JPanel jpSplit = null;
  24. private JLabel jlSrcMsg = null;
  25. private JLabel jlSplitMsg = null;
  26. private JLabel jlSegPathCount = null;
  27. private JTextArea jtSegPathCount = null;
  28. private JTextArea jtSrcMsg = null;
  29. private JTextArea jtSplitMsg = null;
  30. private JTextArea jtFor=null;
  31. private JButton jbSplitOK = null;
  32. private JButton jbTest = null;
  33. private FrmMain() {
  34. }
  35. public static FrmMain getInstance() {
  36. if (frmMain == null)
  37. frmMain = new FrmMain();
  38. return frmMain;
  39. }
  40. private JPanel getJpSplit() {
  41. if (jpSplit == null) {
  42. jpSplit = new JPanel();
  43. jpSplit.add(getJpSplit0(), BorderLayout.CENTER);
  44. jpSplit.add(getJbSplitOK(), BorderLayout.SOUTH);
  45. jpSplit.add(getJbTest(), BorderLayout.SOUTH);
  46. }
  47. return jpSplit;
  48. }
  49. private JPanel getJpSplit0() {
  50. JPanel jpSplit0 = new JPanel();
  51. jpSplit0.add(getJpSegPathCount(), null);
  52. jpSplit0.add(getJpSrcMsg(), null);
  53. jpSplit0.add(getJpSplitMsg(), null);
  54. jpSplit0.setSize(400, 200);
  55. jpSplit0.setLayout(new GridLayout(3, 1));
  56. return jpSplit0;
  57. }
  58. private JPanel getJpSplitMsg() {
  59. jlSplitMsg = new JLabel();
  60. jlSplitMsg.setText("?????");
  61. JPanel jpSplitMsg = new JPanel();
  62. jpSplitMsg.add(jlSplitMsg);
  63. jpSplitMsg.add(getJtSplitMsg());
  64. JScrollPane jsp = new JScrollPane(jtSplitMsg);
  65. jpSplitMsg.add(jsp, jsp.getName());
  66. return jpSplitMsg;
  67. }
  68. private JPanel getJpSegPathCount() {
  69. jlSegPathCount = new JLabel();
  70. jlSegPathCount.setText("?????");
  71. JLabel jtf=new JLabel();
  72. jtf.setText("?????????");
  73. JPanel jpSegPath = new JPanel();
  74. jpSegPath.add(jlSegPathCount);
  75. jpSegPath.add(getJtSegPathCount());
  76. JPanel jpFor=new JPanel();
  77. jpFor.add(jtf);
  78. jpFor.add(getJtForCount());
  79. JPanel jpanel=new JPanel();
  80. jpanel.add(jpSegPath);
  81. jpanel.add(jpFor);
  82. return jpanel;
  83. }
  84. private JTextArea getJtSegPathCount() {
  85. jtSegPathCount = new JTextArea(1, 4);
  86. jtSegPathCount.setText("1");
  87. return jtSegPathCount;
  88. }
  89. private JTextArea getJtForCount() {
  90. jtFor = new JTextArea(1, 4);
  91. jtFor.setText("1000");
  92. return jtFor;
  93. }
  94. private JTextArea getJtSrcMsg() {
  95. jtSrcMsg = new JTextArea(6, 40);
  96. jtSrcMsg.setText("??????????");
  97. jtSrcMsg.setLineWrap(true);
  98. return jtSrcMsg;
  99. }
  100. private JTextArea getJtSplitMsg() {
  101. jtSplitMsg = new JTextArea(6, 40);
  102. jtSplitMsg.setLineWrap(true);
  103. return jtSplitMsg;
  104. }
  105. private JPanel getJpSrcMsg() {
  106. jlSrcMsg = new JLabel();
  107. jlSrcMsg.setText("?????");
  108. JPanel jpSrcMsg = new JPanel();
  109. jpSrcMsg.add(jlSrcMsg);
  110. jpSrcMsg.add(getJtSrcMsg());
  111. JScrollPane jsp = new JScrollPane(jtSrcMsg);
  112. jpSrcMsg.add(jsp, jsp.getName());
  113. return jpSrcMsg;
  114. }
  115. /**
  116. * This method initializes jbSendOK
  117. *
  118. * @return javax.swing.JButton
  119. */
  120. private JButton getJbSplitOK() {
  121. if (jbSplitOK == null) {
  122. jbSplitOK = new JButton();
  123. jbSplitOK.setText("??");
  124. jbSplitOK.addActionListener(new java.awt.event.ActionListener() {
  125. public void actionPerformed(java.awt.event.ActionEvent e) {
  126. int segPathCount = Integer.parseInt(jtSegPathCount.getText());
  127. SegMain.seg.setSegPathCount(segPathCount);
  128. SegResult sr = SegMain.seg.split(jtSrcMsg.getText());
  129. jtSplitMsg.setText("[Time:" + sr.getSpendTime() + "ms]:\n" + sr.getFinalResult());
  130. }
  131. });
  132. }
  133. return jbSplitOK;
  134. }
  135. private JButton getJbTest() {
  136. if (jbTest == null) {
  137. jbTest = new JButton();
  138. jbTest.setText("????");
  139. jbTest.addActionListener(new java.awt.event.ActionListener() {
  140. public void actionPerformed(java.awt.event.ActionEvent e) {
  141. try {
  142. int count=0;
  143. long times=0;
  144. long bytes=0;
  145. int segPathCount = Integer.parseInt(jtSegPathCount.getText());
  146. SegMain.seg.setSegPathCount(segPathCount);
  147. int forCount=Integer.parseInt(jtFor.getText());
  148. ArrayList<String> testCases = readTxtFile2("test\\case1.txt");
  149. for (int i = 0; i < forCount; i++) {
  150. for (String src : testCases) {
  151. SegResult sr = SegMain.seg.split(src);
  152. count++;
  153. bytes+=src.getBytes().length;
  154. times+=sr.getSpendTime();
  155. jtSrcMsg.setText("total_count:"+count+"\ntotal_time:"+times+"\ntotal_bytes:"+bytes
  156. +"\navg_time:"+(times/count)+"\navg_bytes:"+(bytes/times)+"b/ms");
  157. jtSplitMsg.setText("[time:" + sr.getSpendTime() + "ms]:\n" + sr.getFinalResult());
  158. }
  159. }
  160. } catch (IOException e1) {
  161. e1.printStackTrace();
  162. }
  163. }
  164. });
  165. }
  166. return jbTest;
  167. }
  168. public static ArrayList<String> readTxtFile2(String fileName) throws IOException {
  169. ArrayList<String> result = null;
  170. FileInputStream fin = null;
  171. InputStreamReader in = null;
  172. BufferedReader br = null;
  173. File file = null;
  174. String value = null;
  175. if (fileName != null) {
  176. file = new File(fileName);
  177. if (file.exists()) {
  178. result = new ArrayList<String>();
  179. try {
  180. fin = new FileInputStream(file);
  181. in = new InputStreamReader(fin);
  182. br = new BufferedReader(in);
  183. while ((value = br.readLine()) != null) {
  184. result.add(value);
  185. }
  186. } catch (IOException e) {
  187. throw new IOException();
  188. }
  189. }
  190. }
  191. return result;
  192. }
  193. /**
  194. * This method initializes jFrame
  195. *
  196. * @return javax.swing.JFrame
  197. */
  198. public JFrame getJFrame() {
  199. if (jFrame == null) {
  200. jFrame = new JFrame();
  201. jFrame.setSize(new java.awt.Dimension(624, 215));
  202. jFrame.setTitle("ICTCLAS4J????");
  203. jFrame.setContentPane(getJContentPane());
  204. jFrame.addWindowListener(new java.awt.event.WindowAdapter() {
  205. public void windowClosing(java.awt.event.WindowEvent e) {
  206. System.exit(0);
  207. }
  208. });
  209. }
  210. return jFrame;
  211. }
  212. /**
  213. * This method initializes jContentPane
  214. *
  215. * @return javax.swing.JPanel
  216. */
  217. private JPanel getJContentPane() {
  218. if (jContentPane == null) {
  219. jContentPane = new JPanel();
  220. jContentPane.setLayout(new BorderLayout());
  221. jContentPane.add(getJTabbedPane(), java.awt.BorderLayout.CENTER);
  222. }
  223. return jContentPane;
  224. }
  225. /**
  226. * This method initializes jTabbedPane
  227. *
  228. * @return javax.swing.JTabbedPane
  229. */
  230. private JTabbedPane getJTabbedPane() {
  231. if (jTabbedPane == null) {
  232. jTabbedPane = new JTabbedPane();
  233. jTabbedPane.addTab("??", null, getJpSplit(), null);
  234. }
  235. return jTabbedPane;
  236. }
  237. }