Exemplo n.º 1
0
 public MainForm() {
   alfa.setValue(1);
   beta.setValue(1);
   gamma.setValue(1);
   loadDatabase.addActionListener(
       e -> {
         fileChooser.setCurrentDirectory(new File(database.getText()));
         int returnVal = fileChooser.showOpenDialog(panel1);
         if (returnVal == JFileChooser.APPROVE_OPTION) {
           File file = fileChooser.getSelectedFile();
           database.setText(file.getPath());
         }
       });
   loadKeyword.addActionListener(
       e -> {
         fileChooser.setCurrentDirectory(new File(keywords.getText()));
         int returnVal = fileChooser.showOpenDialog(panel1);
         if (returnVal == JFileChooser.APPROVE_OPTION) {
           File file = fileChooser.getSelectedFile();
           keywords.setText(file.getPath());
         }
       });
   search.addActionListener(
       e -> {
         isKeywordsEnabled = keywordsEnabled.getSelectedIndex() != 0;
         DatabaseCollection.clear();
         if (isKeywordsEnabled) {
           new KeywordParser(keywords.getText()).parse();
         }
         new DocumentParser(database.getText()).parse();
         Method method1 = Method.valueOf((String) MainForm.this.method.getSelectedItem());
         Document query1 = new Document(MainForm.this.query.getText(), "", false);
         showResults(method1, query1);
       });
   oznacz.addActionListener(
       e -> {
         for (Result result : results.getSelectedValuesList()) {
           result.setMarkedAsGood(!result.isMarkedAsGood());
         }
         results.repaint();
       });
   newQuestion.addActionListener(
       e -> {
         ResultModel model = (ResultModel) results.getModel();
         Document query1 = model.getQuery();
         calculateRelevance(
             model.getResultList(),
             query1,
             (Integer) alfa.getValue(),
             (Integer) beta.getValue(),
             (Integer) gamma.getValue());
         queryHelp.setText(query1.getQueryText());
         Method method1 = Method.valueOf((String) MainForm.this.method.getSelectedItem());
         showResults(method1, query1);
       });
 }
Exemplo n.º 2
0
 private void calculateRelevance(
     List<Result> resultList, Document query, double alfa, double beta, double gama) {
   List<Document> goodDocuments = new ArrayList<>();
   List<Document> badDocuments = new ArrayList<>();
   for (Result result : resultList) {
     if (result.isMarkedAsGood()) {
       goodDocuments.add(result.getDocument());
     } else {
       badDocuments.add(result.getDocument());
     }
   }
   for (Term term : DatabaseCollection.getTermMap().values()) {
     double relevance =
         alfa * query.getTermRelevance(term)
             + (avg(term, goodDocuments)) * beta
             - avg(term, badDocuments) * gama;
     query.setTermRelevance(term, relevance);
   }
 }
Exemplo n.º 3
0
 private List<Result> getQuerySimilarity(Method method, Document query) {
   List<Result> resultList = new ArrayList<>();
   List<Result> list = new ArrayList<>();
   for (Document document : DatabaseCollection.getDocumentList()) {
     double similarity = document.similarity(query, method);
     if (similarity != -1) {
       list.add(new Result(document, similarity));
     }
   }
   list.stream()
       .sorted(
           (o1, o2) -> {
             double diff = o1.getSimilarity() - o2.getSimilarity();
             if (diff < 0) {
               return 1;
             } else if (diff > 0) {
               return -1;
             }
             return 0;
           })
       .limit(5)
       .forEach(resultList::add);
   return resultList;
 }