0

我在 lucene 中编写了一个代码,它首先索引 xml 文档,并找到索引中唯一术语的数量。

假设有 n 个唯一项。

我想生成一个维度为 nXn 的矩阵,其中

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

术语 (i, j) 的共现 = 否。在其中第 i 个术语和第 j 个术语都出现的文档中,术语 j 的出现是第。出现术语 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];
    }
}

请任何人建议我任何有效的方法来实现它。

4

1 回答 1

0

我认为您可以将 term_one 和 term_two 的查找过程放在一个for循环中。您可以使用两个哈希集来保存您找到的 docid。然后用于termOneSet.retainAll(termTwoSet)获取同时具有 term_one 和 term_two 的文档。

于 2013-06-17T07:23:50.797 回答