コード例 #1
0
  public void testSearch(String[] record, int topDocLimit, int resultLimit) {
    SynonymRecordSearcher recSearcher = new SynonymRecordSearcher(record.length);
    for (int i = 0; i < record.length; i++) {
      initIdx(
          PluginUtil.getPluginInstallPath(HandLuceneImplTest.PLUGIN_ID).concat("data/idx")
              + (i + 1));
      SynonymIndexSearcher searcher =
          new SynonymIndexSearcher(
              PluginUtil.getPluginInstallPath(HandLuceneImplTest.PLUGIN_ID).concat("data/idx")
                  + (i + 1));
      searcher.setTopDocLimit(topDocLimit);
      recSearcher.addSearcher(searcher, i);
    }

    try {
      TopDocs topDocs;
      int hits = 1;
      for (int i = 0; i < record.length; i++) {
        topDocs = recSearcher.getSearcher(i).searchDocumentBySynonym(record[i]);
        hits *= topDocs.totalHits;
      }

      List<OutputRecord> results = recSearcher.search(resultLimit, record);
      Assert.assertNotNull(results);
      Assert.assertFalse(results.isEmpty());
      for (OutputRecord outputRecord : results) {
        Assert.assertNotNull(outputRecord);
        String[] resultingRecord = outputRecord.getRecord();
        Assert.assertEquals(record.length, resultingRecord.length);
        System.out.println(StringUtils.join(resultingRecord, '|'));
        System.out.println("\t" + outputRecord.getScore());
      }
      Assert.assertEquals(Math.min(hits, resultLimit), results.size());

      for (int i = 0; i < record.length; i++) {
        recSearcher.getSearcher(i).close();
      }
    } catch (ParseException e) {
      e.printStackTrace();
      fail("should not get an exception here");
    } catch (IOException e) {
      e.printStackTrace();
      fail("should not get an exception here");
    }
    System.out.println("");
  }
コード例 #2
0
  private int testRecordResultCompute(String[][] wordresults) {
    int nbDuplicateFound = 0;

    // prepare wordresults
    List<List<WordResult>> wordResults = new ArrayList<List<WordResult>>();
    for (String[] elts : wordresults) {
      List<WordResult> wrs = new ArrayList<WordResult>();
      for (String elt : elts) {
        WordResult wr = new WordResult();
        wr.input = "input " + elt;
        wr.word = "word " + elt;
        wr.score = Integer.valueOf(elt);
        wrs.add(wr);
      }
      wordResults.add(wrs);
    }

    // --- compute output results as a list
    RecordResult recRes = new RecordResult();
    recRes.record = initializeRecordToSearch(wordresults);
    recRes.wordResults.addAll(wordResults);

    List<OutputRecord> expectedOutputRows = null;
    expectedOutputRows = new ArrayList<OutputRecord>();
    SynonymRecordSearcher.RecordResult.computeOutputRows(
        wordresults.length, new ArrayList<WordResult>(), recRes.wordResults, expectedOutputRows);
    for (OutputRecord outputRecord : expectedOutputRows) {
      System.out.println(outputRecord);
    }

    // --- test that duplicates are removed when using a set instead of a list
    Set<OutputRecord> uniques = new HashSet<OutputRecord>();
    uniques.addAll(expectedOutputRows);
    Assert.assertTrue(uniques.size() <= expectedOutputRows.size());
    if (uniques.size() < expectedOutputRows.size()) {
      nbDuplicateFound++;
    }

    List<OutputRecord> outputRows = recRes.computeOutputRows();

    // --- check some assertions

    // verify number of results
    int expectedNbOutput = 1;
    for (String[] in : wordresults) {
      expectedNbOutput *= Math.max(in.length, 1);
    }

    Assert.assertEquals(expectedNbOutput, expectedOutputRows.size());
    Assert.assertTrue(expectedOutputRows.size() >= outputRows.size());

    for (OutputRecord outputRecord : outputRows) {
      boolean found = false;
      for (OutputRecord expectedRecord : expectedOutputRows) {
        if (expectedRecord.equals(outputRecord)) {
          found = true;
          break;
        }
      }
      Assert.assertTrue("Record not found: " + outputRecord, found);
    }
    return nbDuplicateFound;
  }