APP Deep Search

Joe

Thành viên VIP
21/1/13
2,962
1,307
113
Hi

An application of PatternMining API (click HERE for more details or for downloading CDJ.zip) is to search for files (or URL links) that contain the specified patterns. I show you hereunder a SWING application that does the searching for some specified patterns in a
  • disk or directory
  • list of links to the actual sites.
The code: DeepSearch.java
Java:
import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.concurrent.*;
//
import datamining.PatternMining;  // <---the API
// Joe Nartca
public class DeepSearch extends JFrame {

  public DeepSearch(String... a) {
    list = new ArrayList<String>();
    JPanel pN = new JPanel();
    setTitle("Joe Nartca (C) Deep Search with PatternMining.");
    BROWSE = new JButton("BROWSE");
    BROWSE.setFont(new Font("Veranda",Font.BOLD, 12));
    BROWSE.addActionListener(t -> browse());
    String lib = System.getProperty("user.dir");
    dir = new JTextField(lib, 30);
    dir.setCaretPosition(lib.length());
    dir.setFont(new Font("Veranda",Font.BOLD, 12));
    dir.addActionListener(t -> getTerms());
    pN.add(BROWSE);
    pN.add(dir);

    jta = new JTextArea(22, 60);
    jta.setFont(new Font("Veranda",Font.BOLD, 10));
    jta.setText("How it works?\n\n"+
          "1. Input the file containing a list of URLs/files or"+
          "Directory name, then press [ENTER]\n"+
          "2. Click PATTERNS to load the patterns\n");
    jta.setBackground(Color.lightGray);
    jta.setEditable(false);

    JPanel pC = new JPanel();
    JScrollPane jsp = new JScrollPane(jta); 
    pC.setBorder(BorderFactory.createTitledBorder("Search with MapReduce Algorithm..."));
    jta.setWrapStyleWord(true);
    jsp.setAutoscrolls(true);
    jta.setLineWrap(true);
    pC.add(jsp);

    JPanel pS = new JPanel();
    JLabel lab1 = new JLabel("Click");
    JLabel lab2 = new JLabel("for Input");
    CONT = new JButton("PATTERNS");
    CONT.setFont(new Font("Veranda",Font.BOLD, 12));
    CONT.addActionListener(t -> getTerms( ));
    CONT.setForeground(Color.blue);
    pS.add(lab1); pS.add(CONT); pS.add(lab2);

    add("North", pN);
    add("Center", pC);
    add("South", pS);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 10, 710, 500);
    setVisible(true);
  }
  private void browse( ) {
    JFileChooser jC = new JFileChooser(System.getProperty("user.dir"));
    jC.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    jC.setMultiSelectionEnabled(false);
    if (jC.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
      dir.setText(jC.getSelectedFile().getAbsolutePath());
  }
  private void getTerms( ) {
    list.clear();
    while (true) {
      String term = JOptionPane.showInputDialog("MRA Pattern");
      if (term == null || term.length() == 0) break;
      list.add(term.trim().toLowerCase());
    }
    if (list.size() == 0) return;
    jta.setText("Starting MapReduce Algorithm...\nSearching Pattern"+(list.size() > 1?"s:":":"));
    for (String S : list) jta.append("\n- "+S);
    // start DeepSearch with PatternMining
    ForkJoinPool.commonPool().execute(()-> {
      dir.setEnabled(false);
      CONT.setEnabled(false);
      BROWSE.setEnabled(false);
      java.util.List<String> aList = null;
      String path = dir.getText().trim();
      File fi = new File(path);
      if (fi.isFile()) {
        try {
          aList = Arrays.asList((new String(java.nio.file.Files.readAllBytes(fi.toPath()))).
                                replace("\r","").split("\n"));
        } catch (Exception ex) { }
      }
      long time = System.currentTimeMillis();
      try { // instantiate PatternMining, NO residue, once = true
        pm = aList == null? new PatternMining(path, list, ' ', true):
                            new PatternMining(aList, list, ' ', true);
        ArrayList<String> docs = pm.miningResults( );
        time = System.currentTimeMillis()-time;
        jta.append(String.format("\nElapsed time: %5.3f sec. Size: %6.3f KB",
                                 ((double)time/1000), pm.bigDataSize( )));
        if (docs.size() > 0) {
          jta.append("\nFollowing Document"+(docs.size() > 1?"s are":" is")+" found");
          for (String d : docs) jta.append("\n- "+d);
        } else jta.append("\nNo document was found.");
      } catch (Exception ex) {
        ex.printStackTrace();
        System.exit(0);
      }
      jta.setCaretPosition(0);
      BROWSE.setEnabled(true);
      CONT.setEnabled(true);
      dir.setEnabled(true);
    });
  }
  //
  public static void main(String... a) throws Exception {
    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
    new DeepSearch(a);
  }
  private JTextArea jta;
  private JTextField dir;
  private PatternMining pm;
  private JButton CONT, BROWSE;
  private ArrayList<String> list;
}
The App starts with the JTextField which contains (by default) the current user directory. Button BROWSE allows you to navigate to the other directory. If you input this JTextField with a file that contains a list of file names or links to some websites and press ENTER (or button PATTERNS) a dialog pops up and allows you to input the searching pattern(s). To Quit the input procedure it can be done either by the dialog CANCEL button or by pressing ENTER with an empty dialog field. Then the searching process automatically starts...

Search in a Directory: pattern fuzzylogic
bigSearch.png

Search with a list of URL links: pattern Vietnam
bigSearch_URL.png

The measurement was made on Acer Aspire E15 desktop, 8GB RAM, i5-4210U 1.7 GHz with Turbo Boost up to 2.7 GHz.
 
Sửa lần cuối: