迷你搜索引擎–使用Neo4j,Crawler4j,Graphstream和Encog的基础知识

继续执行正在实现搜索引擎的Programming Collection Intelligence (PCI)的第4章。

我可能比做一次运动所咬的东西要多。 我认为, 与其一直使用本书中使用的普通关系数据库结构,不如说我一直想看看Neo4J,所以现在是时候了。 只是说,这不一定是图数据库的理想用例,但是用1块石头杀死3只鸟可能有多难。

在尝试重置SQL Server的教程中,Oracle的想法花了比预期更长的时间,但是值得庆幸的是Neo4j周围有一些很棒的资源。

只是几个:

  • neo4j –学习
  • 忙碌的开发人员的图论
  • Graph数据库

由于我只是想作为一个小练习来运行它,所以我决定采用内存中的实现方式,而不是将其作为服务在我的机器上运行。 事后看来,这可能是一个错误,而工具和Web界面将帮助我从一开始就更快地可视化数据图。

由于您只能在内存中实现1个可写实例,因此我做了一些双锁单例工厂来创建和清除数据库。

package net.briandupreez.pci.chapter4;import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.kernel.impl.util.FileUtils;import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;public class CreateDBFactory {private static GraphDatabaseService graphDb = null;public static final String RESOURCES_CRAWL_DB = "resources/crawl/db";public static GraphDatabaseService createInMemoryDB() {if (null == graphDb) {synchronized (GraphDatabaseService.class) {if (null == graphDb) {final Map<String, String> config = new HashMap<>();config.put("neostore.nodestore.db.mapped_memory", "50M");config.put("string_block_size", "60");config.put("array_block_size", "300");graphDb = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(RESOURCES_CRAWL_DB).setConfig(config).newGraphDatabase();registerShutdownHook(graphDb);}}}return graphDb;}private static void registerShutdownHook(final GraphDatabaseService graphDb) {Runtime.getRuntime().addShutdownHook(new Thread() {@Overridepublic void run() {graphDb.shutdown();}});}public static void clearDb() {try {if(graphDb != null){graphDb.shutdown();graphDb = null;}FileUtils.deleteRecursively(new File(RESOURCES_CRAWL_DB));} catch (final IOException e) {throw new RuntimeException(e);}}}

然后使用Crawler4j创建了一个图形,其中包含以我的博客开头的所有URL,它们与其他URL的关系以及这些URL包含的所有单词和单词的索引。

package net.briandupreez.pci.chapter4;import edu.uci.ics.crawler4j.crawler.Page;
import edu.uci.ics.crawler4j.crawler.WebCrawler;
import edu.uci.ics.crawler4j.parser.HtmlParseData;
import edu.uci.ics.crawler4j.url.WebURL;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.index.Index;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class Neo4JWebCrawler extends WebCrawler {private final GraphDatabaseService graphDb;/*** Constructor.*/public Neo4JWebCrawler() {this.graphDb = CreateDBFactory.createInMemoryDB();}@Overridepublic boolean shouldVisit(final WebURL url) {final String href = url.getURL().toLowerCase();return !NodeConstants.FILTERS.matcher(href).matches();}/*** This function is called when a page is fetched and ready* to be processed by your program.*/@Overridepublic void visit(final Page page) {final String url = page.getWebURL().getURL();System.out.println("URL: " + url);final Index<Node> nodeIndex = graphDb.index().forNodes(NodeConstants.PAGE_INDEX);if (page.getParseData() instanceof HtmlParseData) {HtmlParseData htmlParseData = (HtmlParseData) page.getParseData();String text = htmlParseData.getText();//String html = htmlParseData.getHtml();List<WebURL> links = htmlParseData.getOutgoingUrls();Transaction tx = graphDb.beginTx();try {final Node pageNode = graphDb.createNode();pageNode.setProperty(NodeConstants.URL, url);nodeIndex.add(pageNode, NodeConstants.URL, url);//get all the wordsfinal List<String> words = cleanAndSplitString(text);int index = 0;for (final String word : words) {final Node wordNode = graphDb.createNode();wordNode.setProperty(NodeConstants.WORD, word);wordNode.setProperty(NodeConstants.INDEX, index++);final Relationship relationship = pageNode.createRelationshipTo(wordNode, RelationshipTypes.CONTAINS);relationship.setProperty(NodeConstants.SOURCE, url);}for (final WebURL webURL : links) {System.out.println("Linking to " + webURL);final Node linkNode = graphDb.createNode();linkNode.setProperty(NodeConstants.URL, webURL.getURL());final Relationship relationship = pageNode.createRelationshipTo(linkNode, RelationshipTypes.LINK_TO);relationship.setProperty(NodeConstants.SOURCE, url);relationship.setProperty(NodeConstants.DESTINATION, webURL.getURL());}tx.success();} finally {tx.finish();}}}private static List<String> cleanAndSplitString(final String input) {if (input != null) {final String[] dic = input.toLowerCase().replaceAll("\\p{Punct}", "").replaceAll("\\p{Digit}", "").split("\\s+");return Arrays.asList(dic);}return new ArrayList<>();}}

收集完数据后,我可以查询它并执行搜索引擎的功能。 为此,我决定使用Java Futures,因为这是我仅读过但尚未实现的另一件事。 在我的日常工作环境中,我们使用应用程序服务器中的Weblogic / CommonJ工作管理器来执行相同的任务。

final ExecutorService executorService = Executors.newFixedThreadPool(4);final String[] searchTerms = {"java", "spring"};List<Callable<TaskResponse>> tasks = new ArrayList<>();tasks.add(new WordFrequencyTask(searchTerms));tasks.add(new DocumentLocationTask(searchTerms));tasks.add(new PageRankTask(searchTerms));tasks.add(new NeuralNetworkTask(searchTerms));final List<Future<TaskResponse>> results = executorService.invokeAll(tasks);

然后,我开始为以下每个任务创建一个任务,计算单词频率,文档位置, 页面排名和神经网络(带有虚假输入/训练数据),以根据搜索条件对返回的页面进行排名。 所有代码都在我的公共github博客仓库中。

免责声明:神经网络任务要么没有足够的数据来有效,要么我没有正确实现数据标准化,所以它目前不是很有用,我将在完成while PCI的旅程后再返回书。

值得分享的一项任务是Page Rank,我很快就读懂了一些理论,认为我不那么聪明,然后去寻找实现它的图书馆。 我发现Graphstream是一个很棒的开源项目,它不仅可以完成PageRank的全部工作,还可以查看他们的视频。

因此,很容易实现本练习的PageRank任务。

package net.briandupreez.pci.chapter4.tasks;import net.briandupreez.pci.chapter4.NodeConstants;
import net.briandupreez.pci.chapter4.NormalizationFunctions;
import org.graphstream.algorithm.PageRank;
import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.SingleGraph;
import org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.cypher.javacompat.ExecutionResult;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.Callable;public class PageRankTask extends SearchTask implements Callable<TaskResponse> {public PageRankTask(final String... terms) {super(terms);}@Overrideprotected ExecutionResult executeQuery(final String... words) {final ExecutionEngine engine = new ExecutionEngine(graphDb);final StringBuilder bob = new StringBuilder("START page=node(*) MATCH (page)-[:CONTAINS]->words ");bob.append(", (page)-[:LINK_TO]->related ");bob.append("WHERE words.word in [");bob.append(formatArray(words));bob.append("] ");bob.append("RETURN DISTINCT page, related");return engine.execute(bob.toString());}public TaskResponse call() {final ExecutionResult result = executeQuery(searchTerms);final Map<String, Double> returnMap = convertToUrlTotalWords(result);final TaskResponse response = new TaskResponse();response.taskClazz = this.getClass();response.resultMap = NormalizationFunctions.normalizeMap(returnMap, true);return response;}private Map<String, Double> convertToUrlTotalWords(final ExecutionResult result) {final Map<String, Double> uniqueUrls = new HashMap<>();final Graph g = new SingleGraph("rank", false, true);final Iterator<Node> pageIterator = result.columnAs("related");while (pageIterator.hasNext()) {final Node node = pageIterator.next();final Iterator<Relationship> relationshipIterator = node.getRelationships().iterator();while (relationshipIterator.hasNext()) {final Relationship relationship = relationshipIterator.next();final String source = relationship.getProperty(NodeConstants.SOURCE).toString();uniqueUrls.put(source, 0.0);final String destination = relationship.getProperty(NodeConstants.DESTINATION).toString();g.addEdge(String.valueOf(node.getId()), source, destination, true);}}computeAndSetPageRankScores(uniqueUrls, g);return uniqueUrls;}/*** Compute score** @param uniqueUrls urls* @param graph      the graph of all links*/private void computeAndSetPageRankScores(final Map<String, Double> uniqueUrls, final Graph graph) {final PageRank pr = new PageRank();pr.init(graph);pr.compute();for (final Map.Entry<String, Double> entry : uniqueUrls.entrySet()) {final double score = 100 * pr.getRank(graph.getNode(entry.getKey()));entry.setValue(score);}}}

在所有这些之间,我发现了一种通过Stackoverflow上的值对映射进行排序的出色实现。

package net.briandupreez.pci.chapter4;import java.util.*;public class MapUtil {/*** Sort a map based on values.* The values must be Comparable.** @param map       the map to be sorted* @param ascending in ascending order, or descending if false* @param <K>       key generic* @param <V>       value generic* @return sorted list*/public static <K, V extends Comparable<? super V>> List<Map.Entry<K, V>> entriesSortedByValues(final Map<K, V> map, final boolean ascending) {final List<Map.Entry<K, V>> sortedEntries = new ArrayList<>(map.entrySet());Collections.sort(sortedEntries,new Comparator<Map.Entry<K, V>>() {@Overridepublic int compare(final Map.Entry<K, V> e1, final Map.Entry<K, V> e2) {if (ascending) {return e1.getValue().compareTo(e2.getValue());} else {return e2.getValue().compareTo(e1.getValue());}}});return sortedEntries;}}

用于实现所有这些功能的Maven依赖项

<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>14.0.1</version></dependency><dependency><groupId>org.encog</groupId><artifactId>encog-core</artifactId><version>3.2.0-SNAPSHOT</version></dependency><dependency><groupId>edu.uci.ics</groupId><artifactId>crawler4j</artifactId><version>3.5</version><type>jar</type><scope>compile</scope></dependency><dependency><groupId>org.neo4j</groupId><artifactId>neo4j</artifactId><version>1.9</version></dependency><dependency><groupId>org.graphstream</groupId><artifactId>gs-algo</artifactId><version>1.1.2</version></dependency>

现在进入关于PCI…优化的第5章。

参考: 迷你搜索引擎–只是基础知识,它们使用Zen的JCG合作伙伴 Brian Du Preez的Neo4j,Crawler4j,Graphstream和Encog撰写,涉及 IT博客领域 。

翻译自: https://www.javacodegeeks.com/2013/07/mini-search-engine-just-the-basics-using-neo4j-crawler4j-graphstream-and-encog.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/368059.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux人脸识别视频推流,RTMP推流协议视频智能分析/人脸识别/直播点播平台EasyDSS接口调用注意事项介绍...

TSINGSEE青犀视频目前推出了前端支持不同协议设备接入的视频智能分析平台&#xff0c;包括RTSP协议的EasyNVR、GB28181协议的EasyGBS&#xff0c;RTMP推流协议的EasyDSS&#xff0c;还有能够进行人脸识别、车牌识别的EasyCVR&#xff0c;这些平台均提供了视频转码分发的能力&am…

js高级写法

名称 一般写法优化取整(不四舍五入) parseInt(a,10); //Before Math.floor(a); //Before a>>0; //Before ~~a; //After a|0; //After 取整(四舍五入&#xff09; Math.round(a); //Beforenum.toFixed(0) a.5|0; //After未定义 undefined; //Before void 0; //After, 快…

IntersectionObserve API使用

why 之前图片懒加载的实现方法大多数为&#xff1a;给window添加scroll事件&#xff0c;滚动时获取元素的offset值&#xff0c;判断元素在viewport内的可见行。这样做的缺点是&#xff1a;频繁的计算dom节点的属性导致性能较差&#xff0c;对scroll绑定的回调函数进行节流能减少…

css小技巧(1)

1、-webkit-overflow-scrolling: touch; 解决ios滑动时无缓冲问题 2、::-webkit-scrollbar 设置ios滑动时是否显示滚动条 3、::selection 选中文字时文字颜色和选中色 <!doctype html> <html> <head> <meta charset"utf-8"> <meta cont…

在基于图论的Java程序中基于DSL的输入图数据的方法

我们大多数人已经编写了一些程序来处理图论算法&#xff0c;例如查找两个顶点之间的最短路径&#xff0c;查找给定图的最小生成树等等。 在这些算法的每一种中&#xff0c;表示图形的编程方式是使用邻接矩阵或邻接表 。 两者都不是定义图形输入的非常直观的方法。 例如&#xf…

linux 暴力删除文件,暴力删除文件

有些时候电脑上会有一些文件删除不了&#xff0c;例如&#xff1a;删除的时候&#xff0c;提示文件不存在&#xff0c;或者请求的安全信息不可用或无法显示。无法删除文件夹的原因:1.当文件夹中存在正在被占用的文件时,删除当然会失败。其中包括有一些病毒程序在运行时,删除文件…

Remmarguts' Date(POJ2449+最短路+A*算法)

题目链接&#xff1a;http://poj.org/problem?id2449 题目&#xff1a; 题意&#xff1a;求有向图两点间的k短路。 思路&#xff1a;最短路A*算法 代码实现如下&#xff1a; 1 #include <set>2 #include <map>3 #include <queue>4 #include <stack>5 …

点击文本框后页面变大

原因&#xff1a;HTML中默认是认为16px的字体&#xff0c;人才能看清楚&#xff0c;所以&#xff0c;当点击文本框的时候&#xff0c;当前文本框会以字体16px的大小显示&#xff08;即字体小于16px页面会变大&#xff09;。解决原理&#xff1a;设置文本框的的字体为16px&#…

通过外部文件覆盖打包的Spring应用程序属性文件

开发Spring应用程序时&#xff0c;最常见的用例是您希望拥有多个版本的配置属性&#xff0c;具体取决于要部署到的位置&#xff0c;例如&#xff0c;数据库URL或功能标志可能是特定于dev&#xff0c;qa&#xff0c;production等环境的。 像大多数Spring开发任务一样&#xff0…

usb3.0 linux无法识别,USB3.0接口不能识别U盘的解决方法

USB接口可以说是电脑的标配&#xff0c;现在基本上所有电脑都会搭载USB接口。而USB标准从1.0发展到现在的3.0&#xff0c;甚至更新的也已出来。不过&#xff0c;如果USB3.0无法识别U盘&#xff0c;那该怎么办呢?USB3.0是一种技术也是一种规范&#xff0c;现在很多笔记本都是默…

table 鼠标移上去改变单元格边框颜色。

表格定义了 border-collapse:collapse;边框会合并为一个单一的边框。会忽略 border-spacing 和 empty-cells 属性。用td:hover,显示不全 搜索了好久&#xff0c;没有找到好的方法&#xff0c;用左右边框也不完美。 于是就在想&#xff0c;移上去的时候给加个伪元素after&#…

PotPlayer安装与配置

目录 1.简介 2.安装 3.设置 基本选项设置&#xff1a; 播放选项设置&#xff1a; PotPlayer皮肤设置&#xff1a; 1.简介 PotPlayer一款小巧简单的视频播放软件&#xff0c;具有于强大的定制能力和个性化功能。 2.安装 官网下载 potplayer http://potplayer.daum.net/?langzh_…

如何使用反射来基于JPA注释记录数据模型

因此&#xff0c;当您仅可以注释Java类时&#xff0c;使用JPA&#xff0c;Hibernate或EBeans会很酷&#xff0c;但是您不是一直希望可以从代码“生成”数据模型的文档吗&#xff1f; 提取JPA / Hibernate和其他验证批注的信息&#xff1f; 假设您的bean中包含所有这些漂亮的注…

Hadoop的目录结构

转载于:https://www.cnblogs.com/wzlbigdata/p/8392162.html

css3制作广告栏效果的疑问?

【整理】原文&#xff1a;https://segmentfault.com/a/1190000007087701 本人新手&#xff0c;国庆苦逼加无用班&#xff0c;那是我在夕阳下的奔跑吗&#xff1f;闲来无聊整理以前学习的资料&#xff0c;关于广告栏的效果制作&#xff0c;详情观看[这里][1]。其中用了一个作者自…

linux 实验 ps,Linux实验室:监控命令iostat与ps_服务器x86服务器-中关村在线

4、iostat与上面的命令相似&#xff0c;很显然&#xff0c;这个linux系统监控命令是属于IO监控系列的&#xff0c;iostat(I/O statistics&#xff0c;输入输出统计)是一个用于收集显示系统存储设备输入和输出状态统计的简单工具。例如命令&#xff1a;iostat -m -x 1 1000。从结…

从分布式锁角度理解Java的synchronized关键字

分布式锁 分布式锁就以zookeeper为例,zookeeper是一个分布式系统的协调器,我们将其理解为一个文件系统,可以在zookeeper服务器中创建或删除文件夹或文件.设D为一个数据系统,不具备事务能力,在并发状态下可能出现对单个数据同时读写.客户端A,B是数据系统D提供的客户端,能够对其读…

基于浏览器的密钥生成以及与浏览器的密钥/证书存储的交互

想象以下情况&#xff1a; 您需要从访问您的网站的用户那里获取一个密钥&#xff08;在非对称情况下为用户的公共密钥 &#xff09;&#xff0c;并希望浏览器记住私有部分&#xff0c;而不会因冗长的导入过程而困扰用户。 老实说&#xff0c;实际上&#xff0c;您甚至不希望用…

一个简单的前端事件框架

参考网上的一个资料&#xff0c;做下备注。 <html><head><title>js event demo</title><meta http-equiv"pragma" content"no-cache"><meta http-equiv"cache-control" content"no-cache"><me…

linux制作一键恢复,Linux/Centos Mondo 一键部署、镜像恢复,快速部署

1.环境准备image.png2.安装mondocurl -o /etc/yum.repos.d/mondorescue.repo ftp://ftp.mondorescue.org/rhel/7/x86_64/mondorescue.reposed -i s#gpgcheck1#gpgcheck0#g /etc/yum.repos.d/mondorescue.repoyum -y install mondosed -i s#EXTRA_SPACE150000#EXTRA_SPACE650000…