2013-06-12 2 views
0

Я написал код в lucene, который сначала индексирует XML-документы, и находит количество уникальных терминов в индексе.Lucene: - индексирование и поиск уникальных терминов

Скажите, что существует число (число) уникальных терминов.

Я хочу, чтобы генерировать матрицу размеров NxN, где

m[i][j] = (co_occurrence value of terms (i, j))/ (occurrence value of term i) 

co_occurence терминов (I, J) = нет. документов, в которых i-ый термин и j-ый термины, оба происходят . Возникновение термина j является no. документов, в которых происходит член j.

Мой код работает нормально. Но это неэффективно. для больших нет. файлов, где нет. терминов более 2000, его занимает более 10 минут.

вот мой код для поиска co_occurence -

int cooccurrence(IndexReader reader, String term_one, String term_two) throws IOException { 

    int common_doc_no = 0, finaldocno_one = 0, finaldocno_two = 0; 
    int termdocid_one[] = new int[6000]; 
    int termdocid_two[] = new int[6000]; 
    int first_docids[] = new int[6000]; 
    int second_docids[] = new int[6000]; 
    int k = 0; 
    for (java.util.Iterator<String> it = reader.getFieldNames(
      FieldOption.ALL).iterator(); it.hasNext();) { 
     String fieldname = (String) it.next(); 

     TermDocs t = reader.termDocs(new Term(fieldname, term_one)); 

     while (t.next()) { 

      int x = t.doc(); 

      if (termdocid_one[x] != 1) { 
       finaldocno_one++; 
       first_docids[k] = x; 
       k++; 
      } 
      termdocid_one[x] = 1; 
     } 

    } 

    /* 
    * System.out.println("value of finaldoc_one - " + finaldocno_one); for 
    * (int i = 0; i < finaldocno_one; i++) { System.out.println("" + 
    * first_docids[i]); } 
    */ 
    k = 0; 
    for (java.util.Iterator<String> it = reader.getFieldNames(
      FieldOption.ALL).iterator(); it.hasNext();) { 
     String fieldname = (String) it.next(); 

     TermDocs t = reader.termDocs(new Term(fieldname, term_two)); 

     while (t.next()) { 
      int x = t.doc(); 

      if (termdocid_two[x] != 1) { 
       finaldocno_two++; 
       second_docids[k] = x; 
       k++; 
      } 
      termdocid_two[x] = 1; 
     } 

    } 
    /* 
    * System.out.println("value of finaldoc_two - " + finaldocno_two); 
    * 
    * for (int i = 0; i < finaldocno_two; i++) { System.out.println("" + 
    * second_docids[i]); } 
    */ 
    int max; 
    int search = 0; 
    if (finaldocno_one > finaldocno_two) { 
     max = finaldocno_one; 
     search = 1; 
    } else { 
     max = finaldocno_two; 
     search = 2; 
    } 

    if (search == 1) { 
     for (int i = 0; i < max; i++) { 
      if (termdocid_two[first_docids[i]] == 1) 
       common_doc_no++; 
     } 
    } else if (search == 2) { 
     for (int i = 0; i < max; i++) { 
      if (termdocid_one[second_docids[i]] == 1) 
       common_doc_no++; 
     } 
    } 
    return common_doc_no; 

} 

код для вычисления матрицы знаний: -

void knowledge_matrix(double matrix[][], IndexReader reader, double avg_matrix[][]) throws IOException { 

    ArrayList<String> unique_terms_array = new ArrayList<>(); 
    int totallength = unique_term_count(reader, unique_terms_array); 
    int co_occur_matrix[][] = new int[totallength + 3][totallength + 3]; 
    double rowsum = 0; 
    for (int i = 1; i <= totallength; i++) { 
     rowsum = 0; 
     for (int j = 1; j <= totallength; j++) { 
      int co_occurence; 
      int occurence = docno_single_term(reader, 
        unique_terms_array.get(j - 1)); 
      if (i > j) { 
       co_occurence = co_occur_matrix[i][j]; 
      } else { 
       co_occurence = cooccurrence(reader, 
         unique_terms_array.get(i - 1), 
         unique_terms_array.get(j - 1)); 
       co_occur_matrix[i][j] = co_occurence; 
       co_occur_matrix[j][i] = co_occurence; 
      } 

      matrix[i][j] = (float) co_occurence/(float) occurence; 
      rowsum += matrix[i][j]; 

      if (i > 1) 

      { 
       avg_matrix[i - 1][j] = matrix[i - 1][j] - matrix[i - 1][0]; 
      } 
     } 
     matrix[i][0] = rowsum/totallength; 

    } 

    for (int j = 1; j <= totallength; j++) { 
     avg_matrix[totallength][j] = matrix[totallength][j] 
       - matrix[totallength][0]; 
    } 
} 

Пожалуйста, кто-нибудь предложить мне любой эффективный метод для его реализации.

ответ

0

Я думаю, вы можете поместить процесс поиска term_one и term_two в один цикл for. И вы можете использовать два хэшета, чтобы сохранить найденный docid. И затем используйте termOneSet.retainAll(termTwoSet), чтобы получить документ, который имеет как term_one, так и term_two.