自动生成关键字-Automatically Generate Keywords

 

One of the most populair Ruby on Rails plugins is acts_as_taggable. More specifically the enhanced version acts_as_taggable on steriods. It allows you to easily add tags to any model and helps you generate tag clouds.

After reading Nate Koechley article on Yahoo’s term extractor API i was inspired to connect it to acts_as_taggable_on_steroids. The goal is to parse any peace of content (article, blog post, review etc.) and  let Yahoo return a list of terms or tags.

 

Setting up

The first thing you need to do is install the acts_as_taggable_on_steroids plugin.

 

Rails 2.*

  ruby script/plugin install git://github.com/jviney/acts_as_taggable_on_steroids.git

Rails 3:

  rails plugin install git://github.com/jviney/acts_as_taggable_on_steroids.git

 

Prepare the database

Next we generate and apply the migration:

ruby script/generate acts_as_taggable_migration
rake db:migrate

Define which model you want to use

class Post <ActiveRecord::Base
acts_as_taggable
 
belongs_to :userend

You can now use the tagging methods provided by acts_as_taggable, #tag_list and #tag_list=. Both these methods work like regular attribute accessors.

p = Post.find(:first)
p.tag_list # []
p.tag_list = “Funny, Silly”
p.save
p.tag_list # ["Funny", "Silly"]

You can also add or remove arrays of tags.
p.tag_list.add(“Great”, “Awful”)
p.tag_list.remove(“Funny”)

Sign up at Yahoo

You need to sign up for an application ID at Yahoo! Web Services. You need the application ID as an identifier when making API call.

Add parse_tags function

I have written a small function that you can place inside your Posts model and use to create the API call for you.

class Post <ActiveRecord::Base
acts_as_taggable
 
belongs_to :user
 
defself.parse_tags(context, query)
tags = []
 
#yahoo parsing of tags
url = URI.parse('http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction')
 
post_args = {'appid'=>'your application ID',
'context'=> context,
'query'=> query
}
 
resp, data = Net::HTTP.post_form(url, post_args)
 
# extract event information
doc = REXML::Document.new(data)
doc.elements.each('ResultSet/Result')do|element|
tags &lt;&lt; element.textend
 
return tags
endend

Context is the text part you would like to scan and query is an optional query to help with the extraction process. (usually a title or subject)

Lets say you have a post model with a body and title column. Now you can do the following:

p = Post.new
p.body = “Italian sculptors and painters of the renaissance favored the Virgin Mary for inspiration.”
p.title = “madonna”
p.tag_list  = Post.parse(p.body, p.title)

p.tag_list # “italian sculptors, virgin mary, painters, renaissance, inspiration”

Note: Rate Limit
The Term Extraction service is limited to 5,000 queries per IP address per day.

Sources: Yahoo! Search Web Servicesacts_as_taggable on steroids

 

https://github.com/jviney/acts_as_taggable_on_steroids

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值