利用Lucene编写的文档向量相似度计算程序

import java.io.IOException;

import org.apache.lucene.analysis.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;

public class Exec {
        public static double computeAngle(String[] doc1, String[] doc2) {
                // assume words are unique and only occur once

                int dotProduct = 0;
                double denominator;

                for (int i = 0; i < doc1.length; i++) {
                        String word = doc1[i];
                        for (int j = 0; j < doc2.length; j++) {
                                if (word.equals(doc2[j])) {
                                        dotProduct += 1;
                                        break;
                                }
                        }
                }
                denominator = Math.sqrt(doc1.length) * Math.sqrt(doc2.length);
                double ratio = dotProduct / denominator;
                return Math.acos(ratio);
        }

        // For simple, only to get one document which must be belong to the matching
        // document
        public static Document docsLike(IndexReader reader, IndexSearcher searcher,
                        int id) throws IOException {
                Document doc = reader.document(id);
                String[] s1 = (doc.get("subjects")).split(" ");

                int matchid = 0;
                double bestAngle = Double.MAX_VALUE;

                for (int i = 0; i < reader.maxDoc(); i++) {
                        if (i != id) {
                                Document tempdoc = reader.document(i);
                                String[] s2 = (tempdoc.get("subjects")).split(" ");
                                double angle = computeAngle(s1, s2);
                                // System.out.print("***"+angle);
                                if (angle < bestAngle) {
                                        bestAngle = angle;
                                        matchid = i;
                                }
                        }
                }
                // System.out.println("/n***********"+bestAngle+"***********"+matchid);
                return reader.document(matchid);
        }

        public static void main(String[] args) throws IOException {

                IndexReader reader;
                IndexSearcher searcher;

                // Writer
                Directory directory = new RAMDirectory();
                IndexWriter writer = new IndexWriter(directory,
                                new WhitespaceAnalyzer(), true);
                writer.setUseCompoundFile(true);

                String[] isbns = new String[] { "1", "2", "3", "4", "5", "6", "7", "8",
                                "9", "10", "11" };

                String[] titles = new String[] { "Modern Art of Education",
                                "Imperial Secrets of Health", "Tao Te Ching",
                                "G.del, Escher, Bach", "Mindstorms",
                                "Java Development with Ant", "JUnit in Action",
                                "Lucene in Action", "Tapestry in Action",
                                "Extreme Programming Explained", "The Pragmatic Programmer" };
                String[] pubmonths = new String[] { "198106", "199401", "198810",
                                "197903", "198001", "200208", "200310", "200406", "199910",
                                "200403", "199910" };
                String[] categories = new String[] { "/education/pedagogy",
                                "/health/alternative/chinese", "/philosophy/eastern",
                                "/technology/computers/ai",
                                "/technology/computers/programming/education",
                                "/technology/computers/programming",
                                "/technology/computers/programming",
                                "/technology/computers/programming",
                                "/technology/computers/programming",
                                "/technology/computers/programming/methodology",
                                "/technology/computers/programming" };
                String[] subjects = new String[] {
                                "education philosophy psychology practice Waldorf",
                                "diet chinese medicine qi gong health herbs",
                                "taoism chinese ideas",
                                "artificial intelligence number theory mathematics music",
                                "children computers powerful ideas LOGO education",
                                "apache jakarta ant build tool junit java development",
                                "junit unit testing mock objects",
                                "lucene search programming",
                                "tapestry web user interface components",
                                "extreme programming agile test driven development methodology",
                                "pragmatic agile methodology developer tools" };

                for (int i = 0; i < titles.length; i++) {
                        Document doc = new Document();
                        doc.add(Field.Keyword("isbns", isbns[i]));
                        doc.add(Field.Text("titles", titles[i]));
                        doc.add(Field.Text("pubmonths", pubmonths[i]));
                        doc.add(Field.Text("categories", categories[i]));
                        doc.add(Field.Text("subjects", subjects[i], true));
                        writer.addDocument(doc);
                }
                writer.optimize();
                writer.close();

                // Reader
                reader = IndexReader.open(directory);
                searcher = new IndexSearcher(reader);
                int numDocs = reader.maxDoc();

                for (int i = 0; i < numDocs; i++) {
                        System.out.println();
                        Document doc = reader.document(i);
                        System.out.println(doc.get("titles"));
                        Document docs = docsLike(reader, searcher, i);
                        System.out.println("  -> " + docs.get("titles"));
                }
        }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leeshuqing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值