Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Forming the base of a tag cloud (See related posts)

This code was executed from an irb session.

a = "curtains in the window I said curtains on the chimney"

# retrieve each word
c = a.split(/\s+/)

# retrieve each unique word
b = c.uniq


#prepare the tags xml document
file_template = File.new('tags_file_template.xml','r')
doc = Document.new(file_template)

#load the tag template
tag_template = doc.root.elements['records/tag'].to_s

b.each do |word|
  tag = Document.new(tag_template)
  tag.root.elements['keyword'].text = word
  tag.root.elements['count'].text = a.scan(/\b#{word}\b/).length
  doc.root.elements['records'].add_element(tag)
end

# count the no of unique words
doc.root.elements['summary/no_of_uniqe_words'].text  = b.length

# get the average no. of words
norm = c.length / b.length.to_f
doc.root.elements['summary/average_word_count'].text  = norm

doc.root.elements.each('//tag') do |node|
  count = node.elements['count'].text.to_f
  weight = 100 / (c.length /  count)
  node.elements['weight'].text = weight
  norm_diff = count - norm
  node.elements['norm_diff'].text = norm_diff > 0 ? '+' + norm_diff.to_s : norm_diff
  node.elements['gauge'].text = (weight / 10).to_i
end

#delete the blank template tag
node = doc.root.elements['records/tag']
node.parent.delete(node)

file_write = File.new('decorating.xml','w')
file_write.puts doc
file_write.close



file: tags_file_template.xml
<tags>
  <summary>
    <no_of_uniqe_words/>
    <average_word_count/>     <!-- the norm -->
  </summary>
  <records>
    <tag>
      <keyword></keyword>
      <count></count>         <!-- e.g. 5 // no of instances of that word -->
      <weight></weight>       <!-- e.g. 50% -->
      <norm_diff></norm_diff> <!-- e.g. +3 // a positive or negative offset from the norm -->
      <gauge></gauge>         <!-- 1 .. 10 -->
    </tag>
  </records>
</tags>

Comments on this post

songyue posts on Nov 05, 2009 at 10:48

I have read through this article, replica handbags
replica bags
ed hardy
ed hardy clothing
ralph lauren polo
juicy coutureI do not understand what it meant ah

You need to create an account or log in to post comments to this site.


Click here to browse all 7309 code snippets

Related Posts