Advertisement
Guest User

Untitled

a guest
May 16th, 2017
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.66 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package plagiarism;
  7. import com.sun.org.apache.bcel.internal.generic.InstructionConstants;
  8. import java.awt.ScrollPane;
  9. import java.io.BufferedReader;
  10. import java.io.File;
  11. import java.io.FileNotFoundException;
  12. import java.io.FileOutputStream;
  13. import java.io.FileReader;
  14. import java.io.FileWriter;
  15. import java.io.IOException;
  16. import java.io.PrintStream;
  17. import java.io.PrintWriter;
  18. import java.sql.Connection;
  19. import java.sql.DriverManager;
  20. import java.sql.ResultSet;
  21. import java.sql.SQLException;
  22. import java.sql.Statement;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import javax.swing.JFileChooser;
  28. import javax.swing.JOptionPane;
  29. import javax.swing.JScrollPane;
  30. import javax.swing.JTextArea;
  31. import org.apache.pdfbox.cos.COSDocument;
  32. import org.apache.pdfbox.io.RandomAccessFile;
  33. import org.apache.pdfbox.pdfparser.PDFParser;
  34. import org.apache.pdfbox.pdmodel.PDDocument;
  35. import org.apache.pdfbox.pdmodel.interactive.measurement.PDNumberFormatDictionary;
  36. import org.apache.pdfbox.text.PDFTextStripper;
  37. import java.io.BufferedReader;
  38. import java.io.File;
  39. import java.io.FileReader;
  40. import java.io.FileWriter;
  41. import java.io.IOException;
  42. import java.io.PrintWriter;
  43. import static java.lang.System.out;
  44. import java.sql.SQLException;
  45. import java.util.Scanner;
  46. import javax.swing.SwingUtilities;
  47. import javax.swing.filechooser.FileNameExtensionFilter;
  48.  
  49.  
  50.  
  51. /**
  52. *
  53. * @author Munkhdelger
  54. */
  55.  
  56. public class UI extends javax.swing.JFrame {
  57.  
  58. JFileChooser filechooser = new JFileChooser("D:");
  59. FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "txt", "text");
  60.  
  61.  
  62. String url = "jdbc:mysql://localhost:3306/sys?autoReconnect=true&useSSL=false";
  63. String user = "root";
  64. String password = "1234";
  65. PDFParser parser = null;
  66. PDDocument pdDoc = null;
  67. COSDocument cosDoc = null;
  68. PDFTextStripper pdfStripper;
  69. String outs="";
  70.  
  71. int a = 0;
  72. int c = 0;
  73. String array[] = new String [1000000];
  74. String parsedText;
  75. String fileName = "C:\\Users\\Munkhdelger\\Documents\\NetBeansProjects\\pdf.pdf";
  76.  
  77. String name;
  78.  
  79. /**
  80. * @param args
  81. * Creates new form UI
  82. */
  83. public UI() throws FileNotFoundException, IOException {
  84. initComponents();
  85.  
  86.  
  87. }
  88.  
  89.  
  90. /**
  91. * This method is called from within the constructor to initialize the form.
  92. * WARNING: Do NOT modify this code. The content of this method is always
  93. * regenerated by the Form Editor.
  94. */
  95. @SuppressWarnings("unchecked")
  96. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  97. private void initComponents() {
  98.  
  99. jScrollPane6 = new javax.swing.JScrollPane();
  100. jTextArea1 = new javax.swing.JTextArea();
  101. jScrollPane7 = new javax.swing.JScrollPane();
  102. jTextArea2 = new javax.swing.JTextArea();
  103. jButton1 = new javax.swing.JButton();
  104. jButton2 = new javax.swing.JButton();
  105. jButton3 = new javax.swing.JButton();
  106. jButton4 = new javax.swing.JButton();
  107. jButton5 = new javax.swing.JButton();
  108. jTextField1 = new javax.swing.JTextField();
  109.  
  110. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  111.  
  112. jTextArea1.setColumns(20);
  113. jTextArea1.setRows(5);
  114. jScrollPane6.setViewportView(jTextArea1);
  115.  
  116. jTextArea2.setColumns(20);
  117. jTextArea2.setRows(5);
  118. jScrollPane7.setViewportView(jTextArea2);
  119.  
  120. jButton1.setText("баазаас авах");
  121. jButton1.addActionListener(new java.awt.event.ActionListener() {
  122. public void actionPerformed(java.awt.event.ActionEvent evt) {
  123. jButton1ActionPerformed(evt);
  124. }
  125. });
  126.  
  127. jButton2.setText("шалгах");
  128. jButton2.addActionListener(new java.awt.event.ActionListener() {
  129. public void actionPerformed(java.awt.event.ActionEvent evt) {
  130. jButton2ActionPerformed(evt);
  131. }
  132. });
  133.  
  134. jButton3.setText("Шалгах файл сонгох");
  135. jButton3.addActionListener(new java.awt.event.ActionListener() {
  136. public void actionPerformed(java.awt.event.ActionEvent evt) {
  137. jButton3ActionPerformed(evt);
  138. }
  139. });
  140.  
  141. jButton4.setText("File унших");
  142. jButton4.addActionListener(new java.awt.event.ActionListener() {
  143. public void actionPerformed(java.awt.event.ActionEvent evt) {
  144. jButton4ActionPerformed(evt);
  145. }
  146. });
  147.  
  148. jButton5.setText("Цэвэрлэгч");
  149. jButton5.addActionListener(new java.awt.event.ActionListener() {
  150. public void actionPerformed(java.awt.event.ActionEvent evt) {
  151. jButton5ActionPerformed(evt);
  152. }
  153. });
  154.  
  155. jTextField1.addActionListener(new java.awt.event.ActionListener() {
  156. public void actionPerformed(java.awt.event.ActionEvent evt) {
  157. jTextField1ActionPerformed(evt);
  158. }
  159. });
  160.  
  161. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  162. getContentPane().setLayout(layout);
  163. layout.setHorizontalGroup(
  164. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  165. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  166. .addContainerGap()
  167. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  168. .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)
  169. .addGroup(layout.createSequentialGroup()
  170. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  171. .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  172. .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE))
  173. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  174. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  175. .addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
  176. .addComponent(jButton4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE))))
  177. .addGap(23, 23, 23)
  178. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  179. .addComponent(jScrollPane6)
  180. .addComponent(jTextField1)
  181. .addComponent(jButton5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  182. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  183. );
  184. layout.setVerticalGroup(
  185. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  186. .addGroup(layout.createSequentialGroup()
  187. .addGap(25, 25, 25)
  188. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  189. .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)
  190. .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE))
  191. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  192. .addGroup(layout.createSequentialGroup()
  193. .addGap(18, 18, 18)
  194. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  195. .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  196. .addComponent(jButton3))
  197. .addGap(7, 7, 7)
  198. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  199. .addComponent(jButton5)
  200. .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  201. .addGroup(layout.createSequentialGroup()
  202. .addGap(18, 18, 18)
  203. .addComponent(jButton2)
  204. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  205. .addComponent(jButton1)))
  206. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  207. );
  208.  
  209. pack();
  210. }// </editor-fold>
  211.  
  212. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
  213.  
  214.  
  215. try {
  216.  
  217. Connection conn = DriverManager.getConnection(url, user, password);
  218. Statement st = conn.createStatement();
  219. String sql = "SELECT pdfpath FROM pdfbase";
  220.  
  221. ResultSet rs = st.executeQuery(sql);
  222. while(rs.next()){
  223. String w = rs.getString(1);
  224. array[c] = w;
  225. c++;
  226. }
  227. System.out.println(c);
  228. for(int q = 0; q < c; q++){
  229. fileName = array[q];
  230. System.out.println(fileName);
  231. File file = new File(fileName);
  232. String file_name = "Source";
  233.  
  234. parser = new PDFParser(new RandomAccessFile(file, "r"));
  235. parser.parse();
  236. cosDoc = parser.getDocument();
  237. pdfStripper = new PDFTextStripper();
  238. pdDoc = new PDDocument(cosDoc);
  239. parsedText = pdfStripper.getText(pdDoc);
  240. name = file_name + (q + 1);
  241. File f = new File("D:\\DataSource\\" + name + ".txt");
  242. if(f.exists() && !f.isDirectory()) {
  243. continue;
  244. } else {
  245. PrintStream out = new PrintStream(new FileOutputStream("D:\\DataSource\\" + name + ".txt",true));
  246. out.print(parsedText);
  247. System.setOut(out);
  248. out.close();
  249. }
  250. }
  251. }
  252. catch (IOException e) {
  253. try {
  254. if (cosDoc != null)
  255. cosDoc.close();
  256. if (pdDoc != null)
  257. pdDoc.close();
  258. }
  259. catch (IOException e1) {}
  260. }
  261. catch (SQLException e){
  262. }
  263. // TODO add your handling code here:
  264. }
  265.  
  266. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
  267. // TODO add your handling code here:
  268. {
  269.  
  270.  
  271.  
  272. String ptrnLine, textLine,inpLine,sFilePath,srcLine;
  273. int srcLineIndex=1, inpLineIndex=1;
  274. RabinKarp rkComponent;
  275. int inputLen,srcLen,patterntextLength;
  276. int rkNumberOfMatches;
  277. int fullTextLength=0, fullPatternLength=0;
  278. int mainTimeCount= 0, rkComponentCount=0;
  279. boolean rkPlagarismStatus = false;
  280. final File folder = new File("D:\\DataSource");
  281. int coun = 0;
  282. //folder dotroos txt file uudiig shalgaj unshih
  283. for (final File fileEntry : folder.listFiles()) {
  284. sFilePath = fileEntry.getPath();
  285. srcLineIndex=1;
  286. File sourceFile = new File(sFilePath);
  287. File inputFile = new File(jTextField1.getText());
  288. @SuppressWarnings("resource")
  289. BufferedReader sReader = null;
  290. try {
  291. sReader = new BufferedReader( new FileReader(sourceFile));
  292. } catch (FileNotFoundException ex) {
  293. Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
  294. }
  295. try {
  296. while((srcLine = sReader.readLine())!=null){
  297.  
  298. BufferedReader reader = new BufferedReader( new FileReader(inputFile));
  299. inpLineIndex=1;
  300. fullTextLength = fullTextLength+srcLine.length();
  301. while((inpLine = reader.readLine())!=null){
  302. inputLen = inpLine.length();
  303. srcLen = srcLine.length();
  304. if(inputLen>0 && srcLen>0) //muriig unshin 0 esehiig shalgah
  305. {
  306. if(srcLen>inputLen)
  307. { textLine = srcLine;
  308. ptrnLine = inpLine;
  309. }
  310. else
  311. { textLine = inpLine;
  312. ptrnLine = srcLine;
  313. }
  314.  
  315. patterntextLength = ptrnLine.length();
  316.  
  317.  
  318. if(coun<1)
  319. {
  320. fullPatternLength = fullPatternLength+ ptrnLine.length();
  321. }
  322. //algorithm ehlel
  323. rkComponent = new RabinKarp();
  324. if(patterntextLength!=0)
  325. {
  326.  
  327. rkNumberOfMatches = rkComponent.search(ptrnLine,textLine);
  328. if(rkNumberOfMatches>0)
  329. {
  330.  
  331. rkPlagarismStatus = true;
  332. }
  333. else
  334. {
  335. rkPlagarismStatus =false;
  336. }
  337. if(rkPlagarismStatus)
  338. {
  339.  
  340. outs = outs + "Үр дүн\n";
  341. outs = outs + "Оруулсан файлын мөрийн дугаар = " +inpLineIndex + " хуулагдсан байна. \nДараах шалгаж буй файлаас = " +
  342. fileEntry.getName()+ ".\nХуулагдсан мөр = "+srcLineIndex+" Давхардсан илэрц = "+rkNumberOfMatches + "\n";
  343.  
  344. System.out.println("------------------------------------------------------------------------------------------------------------------------------------------");
  345.  
  346.  
  347. }
  348. rkComponentCount = rkComponentCount+rkComponent.getTimeCount();
  349. System.out.println("Rabin karp: textnii urt "+textLine.length()+" bolon patternii urt " +ptrnLine.length()+" is "+
  350. rkComponentCount);
  351. mainTimeCount = rkComponentCount+mainTimeCount;
  352.  
  353.  
  354. }
  355. inpLineIndex++;
  356.  
  357. }
  358.  
  359. }
  360. coun++;
  361. srcLineIndex++;
  362.  
  363. }
  364. } catch (IOException ex) {
  365. Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
  366. }
  367.  
  368. }
  369.  
  370.  
  371. mainTimeCount = rkComponentCount;
  372. System.out.println("Rabin karp: textnii urt "+fullTextLength);
  373. jTextArea2.setText("Үр дүн");
  374. jTextArea2.setText(outs);
  375. jTextArea2.update(jTextArea2.getGraphics());
  376.  
  377. }
  378.  
  379.  
  380.  
  381.  
  382.  
  383. }
  384.  
  385. private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
  386.  
  387. final JFileChooser fc = new JFileChooser();
  388. fc.showOpenDialog(this);
  389.  
  390. try {
  391. // Open an input stream
  392. Scanner reader = new Scanner(fc.getSelectedFile());
  393. String input = reader.nextLine();
  394. jTextField1.setText(fc.getSelectedFile().getAbsolutePath());
  395. } catch(IOException e) {
  396. e.printStackTrace();
  397. }
  398.  
  399.  
  400.  
  401. }
  402.  
  403. private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
  404. // TODO add your handling code here:
  405. JOptionPane.showMessageDialog(null, "Серверээс датагаа уншлаа");
  406.  
  407.  
  408. File folder = new File("D:\\DataSource");
  409. File[] listofFiles = folder.listFiles();
  410. for (int i = 0; i< listofFiles.length; i++){
  411. if(listofFiles[i].isFile()){
  412. System.out.println("file" + listofFiles[i].getName());
  413. jTextArea1.append(String.valueOf(listofFiles[i]+"\n"));
  414. }
  415.  
  416. }
  417. jButton4.setEnabled(false);
  418. }
  419.  
  420. private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
  421. // TODO add your handling code here:
  422. File folder = new File("D:\\DataSource");
  423. for(File file : folder.listFiles()){
  424. if(!file.isDirectory())
  425. file.delete();
  426. }
  427. jTextArea2.setText("");
  428. jTextField1.setText("");
  429. jTextArea1.setText("");
  430. jButton4.setEnabled(true);
  431.  
  432.  
  433. }
  434.  
  435. private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
  436. // TODO add your handling code here:
  437. }
  438.  
  439. /**
  440. * @param args the command line arguments
  441. */
  442. public static void main(String args[]) {
  443. /* Set the Nimbus look and feel */
  444. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  445. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  446. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  447. */
  448. try {
  449. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  450. if ("Nimbus".equals(info.getName())) {
  451. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  452. break;
  453. }
  454. }
  455. } catch (ClassNotFoundException ex) {
  456. java.util.logging.Logger.getLogger(UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  457. } catch (InstantiationException ex) {
  458. java.util.logging.Logger.getLogger(UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  459. } catch (IllegalAccessException ex) {
  460. java.util.logging.Logger.getLogger(UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  461. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  462. java.util.logging.Logger.getLogger(UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  463. }
  464. //</editor-fold>
  465.  
  466. /* Create and display the form */
  467.  
  468. java.awt.EventQueue.invokeLater(new Runnable() {
  469. public void run() {
  470. try {
  471. new UI().setVisible(true);
  472. } catch (IOException ex) {
  473. Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
  474. }
  475. }
  476. });
  477. }
  478. private javax.swing.JFileChooser jFileChooser1;
  479. private javax.swing.JFileChooser jFileChooser2;
  480. private javax.swing.JScrollPane jScrollPane1;
  481. private javax.swing.JScrollPane jScrollPane2;
  482. private javax.swing.JScrollPane jScrollPane3;
  483. private javax.swing.JScrollPane jScrollPane4;
  484. private javax.swing.JScrollPane jScrollPane5;
  485. private javax.swing.JTabbedPane jTabbedPane2;
  486. private javax.swing.JTextArea jTextAreaLcss;
  487. private javax.swing.JTextArea jTextAreaNaive;
  488. private javax.swing.JTextArea jTextAreaKmp;
  489. private javax.swing.JTextArea jTextAreaBoyerMoore;
  490. private javax.swing.JTextArea jTextAreaRabinKarp;
  491. // Variables declaration - do not modify
  492. private javax.swing.JButton jButton1;
  493. private javax.swing.JButton jButton2;
  494. private javax.swing.JButton jButton3;
  495. private javax.swing.JButton jButton4;
  496. private javax.swing.JButton jButton5;
  497. private javax.swing.JScrollPane jScrollPane6;
  498. private javax.swing.JScrollPane jScrollPane7;
  499. private javax.swing.JTextArea jTextArea1;
  500. private javax.swing.JTextArea jTextArea2;
  501. private javax.swing.JTextField jTextField1;
  502. // End of variables declaration
  503. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement