⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lucenepathindexretrievalengine.java

📁 基于MPEG 7 标准,符合未来语义网架构,很值得参考
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            IndexWriter pathIndexWriter = new IndexWriter(pathIndexDir, new GraphAnalyzer(), createPathIndexFlag);

            for (Iterator<Graph> iterator = graph2document.keySet().iterator(); iterator.hasNext();) {
                Graph graph = iterator.next();
                HashSet<String> files = graph2document.get(graph);
                Document idxDocument = new Document();
                // adding the file itself ...
                for (Iterator<String> it = files.iterator(); it.hasNext();) {
                    String s = it.next();
                    idxDocument.add(Field.UnIndexed("file", s));
                }
                // adding the graph ...
                idxDocument.add(Field.Text("graph", graph.toString(), true));
//                idxDocument.add(Field.UnIndexed("graph", graph.toString()));
                // adding the paths
                StringBuilder sb = new StringBuilder(256);
                sb.append(graph.toString());
                List<Path> pathList = (new LabeledGraph(graph)).get2Paths();
                if (pathList.size() > 0) sb.append(' ');
                for (Iterator<Path> iterator1 = pathList.iterator(); iterator1.hasNext();) {
                    Path path = iterator1.next();
                    sb.append(path.toString());
                    if (iterator1.hasNext()) sb.append(' ');
                }
                idxDocument.add(Field.Text("paths", sb.toString(), true));
                pathIndexWriter.addDocument(idxDocument);
            }
            // now optimize and close the index:
            pathIndexWriter.optimize();
            pathIndexWriter.close();


        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * Creates and returns index directory for node index ...
     *
     * @param pathToIndex
     * @return
     */
    public static String parseSemanticIndexDirectory(String pathToIndex) {
        String indexDir = pathToIndex;
        if (!indexDir.endsWith(System.getProperty("file.separator"))) indexDir += System.getProperty("file.separator");
        indexDir += "idx_semantic";
        File indexDirFile = new File(indexDir);
        if (!indexDirFile.exists()) indexDirFile.mkdir();
        return indexDir;
    }

    /**
     * Creates and returns the index directory for path index ...
     *
     * @param pathToIndex
     * @return
     */
    public static String parsePathIndexDirectory(String pathToIndex) {
        String indexDir = pathToIndex;
        if (!indexDir.endsWith(System.getProperty("file.separator"))) indexDir += System.getProperty("file.separator");
        indexDir += "idx_paths";
        File indexDirFile = new File(indexDir);
        if (!indexDirFile.exists()) indexDirFile.mkdir();
        return indexDir;
    }

    /**
     * Creates and returns the index directory for 2-path index ...
     *
     * @param pathToIndex
     * @return
     * @deprecated
     */
    public static String parse2PathIndexDirectory(String pathToIndex) {
        String indexDir = pathToIndex;
        if (!indexDir.endsWith(System.getProperty("file.separator"))) indexDir += System.getProperty("file.separator");
        indexDir += "idx_2paths";
        File indexDirFile = new File(indexDir);
        if (!indexDirFile.exists()) indexDirFile.mkdir();
        return indexDir;
    }

    private String getFileListFromNode(Collection<String> list) {
        StringBuilder files = new StringBuilder(64);
        for (Iterator<String> it2 = list.iterator(); it2.hasNext();) {
            files.append(it2.next());
            if (it2.hasNext()) {
                files.append('|');
            }
        }
        return files.toString();
    }



    /**
     * Eliminates all inverse relations to simplify retrieval
     *
     * @param relation
     * @return the normalized relation
     */
    public static Relation eliminateInverse(Relation relation) {
        Relation result = relation;
        if (Relation.relationMapping.containsKey(relation.getType())) {
            result = new Relation(relation.getTarget(), relation.getSource(), Relation.relationMapping.get(relation.getType()));
        }
        return result;
    }

    /**
     * Creates a query String from a graph ...
     * @param g
     * @return
     */
    public static String createLucenePathQuery(Graph g) {
        StringBuilder sb = new StringBuilder(256);
        List<Node> nodes = g.getNodes();
        Collections.sort(nodes);
        List<Relation> relations = g.getRelations();
        Collections.sort(relations);
        for (Iterator<Node> iterator = nodes.iterator(); iterator.hasNext();) {
            Node n = iterator.next();
            // if no anonymous node:
            if (n.getNodeID()>-1) {
                sb.append("_");
                sb.append(n.getNodeID());
                sb.append(" ");
            }
        }
        for (Iterator<Relation> iterator = relations.iterator(); iterator.hasNext();) {
            Relation relation = iterator.next();
            sb.append(" ");
            sb.append(createLucenePathQuery(relation));
            sb.append(" ");
        }

        // 2-path generation:
        LabeledGraph lg = new LabeledGraph(g);
        sb.append(create2PathQuery(lg));
        return sb.toString().trim();
    }

    public static String create2PathQuery(LabeledGraph lg) {
        StringBuilder sw = new StringBuilder(64);
        List<Path> paths = lg.get2Paths();
        for (Iterator<Path> it = paths.iterator(); it.hasNext();) {
            Path p = it.next();
            sw.append(createPathQueryString(p));
            sw.append(" ");
        }
        return sw.toString();
    }

    public static String createPathQueryString(Path p) {
        StringBuilder sw = new StringBuilder(128);
        LinkedList<String> nodes = p.getNodes();
        LinkedList<String> relations = p.getRelations();

        if (nodes.getFirst().equals('*') || nodes.getLast().equals('*')) {
            // we have to implement both directions.
            sw.append("(");
            sw.append('_');
            for (int i = 0; i < nodes.size(); i++) {
                sw.append(deAnonymize(nodes.get(i)));
                if (i < nodes.size() - 1) {
                    sw.append('_');
                    sw.append(relations.get(i));
                    sw.append('_');
                }
            }
            sw.append(" OR _");
            for (int i = nodes.size() - 1; i >= 0; i--) {
                sw.append(deAnonymize(nodes.get(i)));
                if (i > 0) {
                    sw.append('_');
                    sw.append(Relation.invertRelationType(relations.get(i - 1)));
                    sw.append('_');
                }
            }
            sw.append(')');
        } else {
            // we have to implement only one direction :)
            sw.append('_');
            // if the first is smaller then do not change the order
            if (nodes.getFirst().compareTo(nodes.getLast()) < 0) {
                for (int i = 0; i < nodes.size(); i++) {
                    sw.append(deAnonymize(nodes.get(i)));
                    if (i < nodes.size() - 1) {
                        sw.append('_');
                        sw.append(relations.get(i));
                        sw.append('_');
                    }
                }
            } else { // switch order of nodes ..
                for (int i = nodes.size() - 1; i >= 0; i--) {
                    sw.append(deAnonymize(nodes.get(i)));
                    if (i > 0) {
                        sw.append('_');
                        sw.append(Relation.invertRelationType(relations.get(i - 1)));
                        sw.append('_');
                    }
                }
            }
        }
        return sw.toString();
    }

    public static String createLucenePathQuery(Relation r) {
        StringBuilder sb = new StringBuilder(64);
        if (r.getType().equals("*")) {
            String source = deAnonymize(r.getSource());
            String target = deAnonymize(r.getTarget());
            sb.append("(");
            sb.append("_*_");
            sb.append(source);
            sb.append('_');
            sb.append(target);
            sb.append(" OR ");
            sb.append("_*_");
            sb.append(target);
            sb.append('_');
            sb.append(source);
            sb.append(")");
        } else {
            // check if not inverse :)
            r.eliminateInverse();
            sb.append('_');
            sb.append(r.getType());
            sb.append('_');
            sb.append(deAnonymize(r.getSource()));
            sb.append('_');
            sb.append(deAnonymize(r.getTarget()));
        }
        return sb.toString().trim();
    }

    private static String deAnonymize(int nodeID) {
        if (nodeID > -1) return (nodeID + "");
        else return "*";
    }

    private static String deAnonymize(String nodeID) {
        if (!nodeID.equals("-1")) return (nodeID);
        else return "*";
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -