ERP俱乐部
ERP爱好者、ERP从业者互相交流、互相学习的乐园;我们的愿景是成为全球一流的中文ERP(Enterprise Resource Planning)交流平台
网站首页 论坛首页 搜索 用户列表 FAQ 注册 登录  
ERP俱乐部 -> Microsoft专栏 -> 亚可审批工作流、CRM专栏 -> Re: 使用Ruby实现社交网站好友推荐
  Re: 使用Ruby实现社交网站好友推荐
帖子发起人: 半神   发起时间: 2012-02-14 10:30 上午   回复数: 1
? 上一主题 下一主题 ?
楼主
  2012-02-14, 10:30 上午
半神 离线,最后访问时间: 2013/6/5 18:59:05 半神

发帖数前25位

超级管理员
职务: 超级管理员
80级
等级: 80级
注册: 2008年1月6日
区域: 华南
经验: 1,267
积分: 1,131
精华: 2
发贴: 590
排名: 26
Site AdministratorsGlobal ModeratorsSite ModeratorsSite Registered Users培训学员(MM学员服务区-北京200708班) 培训学员(FI学员服务区-深圳200805班) 每日发帖之星
使用Ruby实现社交网站好友推荐
 
Neo4j是一个面向网络的数据库,即一个嵌入式的、基于磁盘的、具备完全的事务特性的Java持久化引擎,但是它将结构化数据存储在网络上而不是 表中。Neo4j也可以被看作是一个高性能的图引擎,该引擎具有成熟数据库的所有特性。程序员工作在一个面向对象的、灵活的网络结构下而不是严格、静态的 表中——但是他们可以享受到具备完全的事务特性、企业级的数据库的所有好处。

  Neo4j因其嵌入式、高性能、轻量级等优势,越来越受到关注。今天将为大家介绍如何开始学习Neo4j和Ruby,当然这是非常简单的开始。按照下面的步骤进行,你将在很短的时间内学会Neo4j。

  首先,需要安装neography gem:

  使用Bundler工具,代码如下:

echo "source 'http://rubygems.org'
gem 'neography' " > Gemfile
bundle install

  不使用Bundler工具,代码如下:

  gem install neography

  然后,将任务添加到Rakefile里,下载Neo4j并启动:

echo "require 'neography/tasks'" >> Rakefile
rake neo4j:install
rake neo4j:start

  图形数据库应用

  比如,在社交网站上好友推荐就是个不错的应用,代码如下:

require 'rubygems'
require 'neography'

@neo
= Neography::Rest.new

def create_person(name)
  @neo.create_node(
"name" => name)
end

def make_mutual_friends(node1, node2)
  @neo.create_relationship(
"friends", node1, node2)
  @neo.create_relationship(
"friends", node2, node1)
end

def suggestions_for(node)
  @neo.traverse(node,
                
"nodes",
                {
"order" => "breadth first",
                
"uniqueness" => "node global",
                
"relationships" => {"type"=> "friends",
                                    
"direction" => "in"},
                
"return filter" => {"language" => "javascript",
                                    
"body" => "position.length() == 2;"},
                
"depth" => 2}).map{|n| n["data"]["name"]}.join(', ')
end

johnathan
= create_person('Johnathan')
mark      = create_person('Mark')
phil      = create_person('Phil')
mary      = create_person('Mary')
luke      = create_person('Luke')

make_mutual_friends(johnathan, mark)
make_mutual_friends(mark, mary)
make_mutual_friends(mark, phil)
make_mutual_friends(phil, mary)
make_mutual_friends(phil, luke)

puts
"Johnathan should become friends with #{suggestions_for(johnathan)}"

# RESULT
# Johnathan should become friends
with Mary, Phil

  我们将一步步的介绍以上代码:

  首先提取gems,得到Neography连接到Neo4j服务器的一个实例,代码如下:

require 'rubygems'
require 'neography'

@neo
= Neography::Rest.new

  然后编写函数,方便创建拥有name属性的节点,代码如下:

def create_person(name)
  @neo.create_node(
"name" => name)
end

  另外创建一个函数,将两个节点关联起来。

  在Neo4j中所有的关系都有方向,因此,在社交网站中如果两个人有共同的好友,就需要创建了两个关系——传入和传出,代码如下:

def make_mutual_friends(node1, node2)
  @neo.create_relationship(
"friends", node1, node2)
  @neo.create_relationship(
"friends", node2, node1)
end

  接下来是最有趣的部分,代码如何实现为你推荐好友的好友:

  将一个用户作为起点,沿着两个好友关系找到他们的好友并返回结果。

def suggestions_for(node)
  @neo.traverse(node,
                
"nodes",
                {
"order" => "breadth first",
                
"uniqueness" => "node global",
                
"relationships" => {"type"=> "friends",
                                    
"direction" => "in"},
                
"return filter" => {"language" => "javascript",
                                    
"body" => "position.length() == 2;"},
                
"depth" => 2}).map{|n| n["data"]["name"]}.join(', ')
end

  创建小规模的好友集合并将它们联系在一起,来进行此次测试:

johnathan = create_person('Johnathan')
mark      = create_person('Mark')
phil      = create_person('Phil')
mary      = create_person('Mary')
luke      = create_person('Luke')

make_mutual_friends(johnathan, mark)
make_mutual_friends(mark, mary)
make_mutual_friends(mark, phil)
make_mutual_friends(phil, mary)
make_mutual_friends(phil, luke)

puts
"Johnathan should become friends with #{suggestions_for(johnathan)}"

# RESULT
# Johnathan should become friends
with Mary, Phil

  这很简单吧?neography工作在Neo4j REST API的水平,但也提供了一个额外的层,涉及ruby的语法糖。

require 'rubygems'
require 'neography'

def create_person(name)
  Neography::Node.create(
"name" => name)
end

johnathan
= create_person('Johnathan')
mark      = create_person('Mark')
phil      = create_person('Phil')
mary      = create_person('Mary')
luke      = create_person('Luke')

johnathan.both(:friends)
<< mark
mark.both(:friends)
<< mary
mark.both(:friends)
<< phil
phil.both(:friends)
<< mary
phil.both(:friends)
<< luke

def suggestions_for(node)
  node.incoming(:friends).
       order(
"breadth first").
       uniqueness(
"node global").
      
filter("position.length() == 2;").
       depth(
2).
       map{|n| n.name }.join(
', ')
end
puts
"Johnathan should become friends with #{suggestions_for(johnathan)}"

# RESULT
# Johnathan should become friends
with Mary, Phil

开源时代的到来,对与技术人员是一个巨大的考验



QQ:876162454


分享按钮 IP 地址: 已登录   来自: 已登录    返回顶部
第 2 楼
  2012-02-28, 10:38 上午
tr 离线,最后访问时间: 2013/6/5 15:41:48 tr

发帖数前500位

80级
等级: 80级
注册: 2011年1月4日
经验: 1,658
积分: 1,598
精华: 0
发贴: 74
排名: 20
Site Registered Users
Re: 使用Ruby实现社交网站好友推荐
 
介绍很好
IP 地址: 已登录   来自: 已登录    返回顶部
 第 1 页 总共 1 页 [共有 2 条记录]
ERP俱乐部 -> Microsoft专栏 -> 亚可审批工作流、CRM专栏 -> Re: 使用Ruby实现社交网站好友推荐
(C)Copyright 2005-2020 www.erpclub.org All Rights Reserved.
Tel:+86-755-26444630
Email:webmaster@yok.com.cn