<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Extempore &#187; code</title>
	<atom:link href="http://michaelhendrickx.com/tag/code/feed" rel="self" type="application/rss+xml" />
	<link>http://michaelhendrickx.com</link>
	<description>random ramblings of the disturbed mind</description>
	<lastBuildDate>Tue, 20 Jul 2010 05:39:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Find &#8220;similar things&#8221; in Ruby</title>
		<link>http://michaelhendrickx.com/200908_find-similar-things-in-ruby.html</link>
		<comments>http://michaelhendrickx.com/200908_find-similar-things-in-ruby.html#comments</comments>
		<pubDate>Sat, 29 Aug 2009 12:46:07 +0000</pubDate>
		<dc:creator>Michael Hendrickx</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ror]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://michaelhendrickx.com/?p=189</guid>
		<description><![CDATA[For several Ruby on Rails projects I had to come up with &#8220;similar&#8221; results.  These are often results (video&#8217;s, products,places, hobbies, etc) with the greatest number of tags.
Say, you are tagging car pictures on a website, and have the following:
image1.jpg -> ["honda","s2000","convertible","black"]
image2.jpg -> ["honda","civic","blue"]
image3.jpg -> ["lexus","is300","blue"]
image4.jpg -> ["s2000","honda","convertible","silver"]
image5.jpg -> ["toyota","starlet","black"]
Seeing this, you&#8217;d know that [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://michaelhendrickx.com/wp-content/uploads/2009/08/ruby.jpeg" alt="ruby" title="ruby" width="123" height="96" class="alignright size-full wp-image-193" />For several Ruby on Rails projects I had to come up with &#8220;similar&#8221; results.  These are often results (video&#8217;s, products,<a href="http://places.ae/">places</a>, hobbies, etc) with the greatest number of tags.</p>
<p>Say, you are <em>tagging</em> car pictures on a website, and have the following:</p>
<p>image1.jpg -> ["honda","s2000","convertible","black"]<br />
image2.jpg -> ["honda","civic","blue"]<br />
image3.jpg -> ["lexus","is300","blue"]<br />
image4.jpg -> ["s2000","honda","convertible","silver"]<br />
image5.jpg -> ["toyota","starlet","black"]</p>
<p>Seeing this, you&#8217;d know that image1.jpg and image4.jpg are similar pictures.  Or rather &#8220;more similar&#8221; than , say, image1.jpg and image3.jpg.  For this, I wrote below snippet of code.  This goes in the model file, and can be called as &#8220;object.similar&#8221;.  It returns an array of similar &#8220;things&#8221;, sorted on most similar to less similar (hence the results.reverse at the end) </p>
<p>For example:<br />
<strong>
<pre>
  img = Image.find(params[:id])
  @similar_images = img.similar[0..10]
</pre>
<p></strong></p>
<p>Will give you the 10 &#8220;most similar&#8221; images as img.  Well, it gives you the files with the most similar tags.</p>
<pre>
def similar
  tags = self.tags
  results = []
  tags.each do |tag|
    results = results + tag.pictures # or tag.things, tag.products, ...
    results.delete(self)
  end

  # make array into hash
  h = Hash.new
  results.each do |r|
    h[r] = h[r].to_i + 1
  end

  # sort on values
  tmp = h.sort {|a,b| a[1]<=>b[1]}
  results = []
  tmp.each do |t|
    results << t[0]
  end

  results.reverse # return all items, products, ...
end
</pre>
<p>This was written for a new <a href="http://habibi.ae">project coming up</a>, and will be used to do better "similarities matching" for <a href="http://places.ae">places.ae</a>, though for the latter we also had to sort on distance.  (For it's vicinity)</p>
]]></content:encoded>
			<wfw:commentRss>http://michaelhendrickx.com/200908_find-similar-things-in-ruby.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
