<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: tagging code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 12:00:47 GMT</pubDate>
    <description>DZone Snippets: tagging code</description>
    <item>
      <title>quote-delineated tag sets</title>
      <link>http://snippets.dzone.com/posts/show/5228</link>
      <description>snippet to surround a tag with &#8220;smart quotes&#8221; if it contains any odd characters. i.e. to display tags entered in a flickr-like style (space separated except inside quotes)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;tag.gsub(/^/,'&amp;ldquo;').gsub(/$/,'&amp;rdquo;') if tag[0].match(/[^A-Za-z0-9\-_.]/)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 13 Mar 2008 21:16:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5228</guid>
      <author>elliottcable (elliott cable)</author>
    </item>
    <item>
      <title>Tag Cloud in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/2251</link>
      <description>The options.delete with the default looks mildly interesting. Just wanted to remember that.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def font_size_for_tag_cloud( total, lowest, highest, options={} )&lt;br /&gt; return nil if total.nil? or highest.nil? or lowest.nil?&lt;br /&gt; #&lt;br /&gt; # options&lt;br /&gt; maxf = options.delete( :max_font_size ) || 14&lt;br /&gt; minf = options.delete( :min_font_size ) || 11&lt;br /&gt; maxc = options.delete( :max_color ) || [ 0, 0, 0 ]&lt;br /&gt; minc = options.delete( :min_color ) || [ 156, 156, 156 ]&lt;br /&gt; hide_sizes = options.delete( :hide_sizes )&lt;br /&gt; hide_colours = options.delete( :hide_colours )&lt;br /&gt; #&lt;br /&gt; # function to work out rgb values&lt;br /&gt; def rgb_color( a, b, i, x)&lt;br /&gt;  return nil if i &lt;= 1 or x &lt;= 1&lt;br /&gt;  if a &gt; b&lt;br /&gt;   a-(Math.log(i)*(a-b)/Math.log(x)).floor&lt;br /&gt;  else&lt;br /&gt;   (Math.log(i)*(b-a)/Math.log(x)+a).floor&lt;br /&gt;  end&lt;br /&gt; end&lt;br /&gt; #&lt;br /&gt; # work out colours&lt;br /&gt; c = []&lt;br /&gt; (0..2).each { |i| c &lt;&lt; rgb_color( minc[i], maxc[i], total, highest ) || nil }&lt;br /&gt; colors = c.compact.empty? ? minc.join(',') : c.join(',')&lt;br /&gt; #&lt;br /&gt; # work out the font size&lt;br /&gt; spread = highest.to_f - lowest.to_f&lt;br /&gt; spread = 1.to_f if spread &lt;= 0&lt;br /&gt; fontspread = maxf.to_f - minf.to_f&lt;br /&gt; fontstep = spread / fontspread&lt;br /&gt; size = ( minf + ( total.to_f / fontstep ) ).to_i&lt;br /&gt; size = maxf if size &gt; maxf&lt;br /&gt; #&lt;br /&gt; # display the results&lt;br /&gt; size_txt = "font-size:#{ size.to_s }px;" unless hide_sizes&lt;br /&gt; color_txt = "color:rgb(#{ colors });" unless hide_colours&lt;br /&gt; return [ size_txt, color_txt ].join&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 06 Jul 2006 17:01:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2251</guid>
      <author>jnunemaker ()</author>
    </item>
    <item>
      <title>My ColdFusion Tagging Engine/Library</title>
      <link>http://snippets.dzone.com/posts/show/1700</link>
      <description>//via:http://www.whatspop.com/blog/2005/12/my-coldfusion-tagging-enginelibrary.cfm&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;cfset tags_list = " CaMELCaSE comma, 'singlequoted' &lt;b&gt;bold&lt;/b&gt; ""doublequoted"" double double this should be over the limit now " /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;cfset amount = 10 /&gt;&lt;br /&gt;&lt;cfset length = 20 /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;cfset tags_list = trim(tags_list) /&gt;&lt;br /&gt;&lt;cfset tags_list = lcase(tags_list) /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;cfset tags_struct = structnew() /&gt;&lt;br /&gt;&lt;cfloop index="tag" list="#tags_list#" delimiters=" "&gt;&lt;br /&gt;&lt;cfset tags_struct[tag] = "" /&gt;&lt;br /&gt;&lt;/cfloop&gt;&lt;br /&gt;&lt;cfset tags_list = structkeylist(tags_struct," ") /&gt;&lt;br /&gt;&lt;cfset tags_list = listsort(tags_list, "textnocase", "asc", " ") /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;cfif tags_list neq ""&gt;&lt;br /&gt;&lt;cfif listlen(tags_list," ") lte amount&gt;&lt;br /&gt;&lt;cfset amount = listlen(tags_list," ") /&gt;&lt;br /&gt;&lt;/cfif&gt;&lt;br /&gt;&lt;cfloop from="1" to="#amount#" index="tag"&gt;&lt;br /&gt;/* Perform a database upload here */&lt;br /&gt;&lt;cfoutput&gt;#htmleditformat(left(listgetat(tags_list,tag," "),length))#&lt;/cfoutput&gt;&lt;br /&gt;&lt;/cfloop&gt;&lt;br /&gt;&lt;cfelse&gt;&lt;br /&gt;Sorry, you need to provide at least one tag&lt;br /&gt;&lt;/cfif&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 15 Mar 2006 07:59:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1700</guid>
      <author>Mozier (Kenny Xiao)</author>
    </item>
    <item>
      <title>Find items with similar (or as many as possible) relationships - for a 'related posts' box etc</title>
      <link>http://snippets.dzone.com/posts/show/35</link>
      <description>If we have the id for a post in &lt;em&gt;postid&lt;/em&gt; and a limit of &lt;em&gt;num&lt;/em&gt; and we want to find posts which share as many tags as possible with &lt;em&gt;postid&lt;/em&gt;'s post, the following SQL will get you there.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;SELECT p.*, COUNT(pt2.post_id) AS count FROM posts p, posts_tags pt, tags t, posts_tags pt2 WHERE pt.post_id=#{postid} AND t.id = pt.tag_id AND pt2.post_id != pt.post_id AND pt2.tag_id=pt.tag_id AND p.id = pt2.post_id GROUP BY pt2.post_id ORDER BY count DESC LIMIT #{num};")&lt;/code&gt;</description>
      <pubDate>Mon, 04 Apr 2005 02:00:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/35</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>Find all many-to-many relationships which are tied to an arbitrary number of other many-to-many relationships</title>
      <link>http://snippets.dzone.com/posts/show/34</link>
      <description>Yet more code from Snippets itself. When you narrow down posts by tags, I only want you to see tags which are shared by other posts which also have the same current tags. This is more difficult than it sounds, and requires use of a subselect:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;SELECT *, COUNT(pt.post_id) AS count FROM posts_tags pt, tags t WHERE pt.post_id IN (SELECT pt.post_id FROM posts_tags pt, tags t WHERE pt.tag_id = t.id AND (t.name IN ('rails', 'ruby')) GROUP BY pt.post_id HAVING COUNT(pt.post_id)=2) AND t.id = pt.tag_id GROUP BY pt.tag_id ORDER BY count DESC;&lt;/code&gt;&lt;br /&gt;The first query (inside the subselect) finds all post IDs for posts which definitely contain any of the current tags. The outer query finds all OTHER tags associated with these posts, therefore we find all (and the quantity of) the tags related to the input tags.</description>
      <pubDate>Mon, 04 Apr 2005 01:30:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/34</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>Return items with data intersections over a many-to-many join</title>
      <link>http://snippets.dzone.com/posts/show/32</link>
      <description>If you have three tables, 'posts', 'tags', and a join table 'posts_tags', and you wish to find all posts which are all associated with a number of different tags, you can use this SQL trick:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;SELECT p.* FROM posts_tags pt, posts p, tags t WHERE pt.tag_id = t.id AND (t.name IN ('tag1', 'tag2', 'tag3')) AND p.id=pt.post_id GROUP BY p.id HAVING COUNT(p.id) = 3;&lt;/code&gt;&lt;br /&gt;.. where 3 is the number of tags in total. In Ruby/Rails, if you have an array called 'tags' containing the tags, you could use this code:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;@posts = Post.find_by_sql ("SELECT p.* FROM posts_tags pt, posts p, tags t WHERE pt.tag_id = t.id AND (t.name = '" + tags.uniq.join ('\' OR t.name=\'') + "') AND p.id=pt.post_id GROUP BY p.id HAVING COUNT(p.id) = " + tags.uniq.length.to_s)&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This is how Snippets itself works. You can also add a p.user_id check to the HAVING operator to only find posts with certain tags by a certain user.</description>
      <pubDate>Sun, 03 Apr 2005 22:42:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/32</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
  </channel>
</rss>
