<?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>a work on process &#187; genre</title>
	<atom:link href="http://jystewart.net/process/tag/genre/feed/" rel="self" type="application/rss+xml" />
	<link>http://jystewart.net/process</link>
	<description>notes from another web developer</description>
	<lastBuildDate>Sat, 21 Aug 2010 13:09:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using amazon and amazon-ecs to identify genres</title>
		<link>http://jystewart.net/process/2008/12/using-amazon-and-amazon-ecs-to-identify-genres/</link>
		<comments>http://jystewart.net/process/2008/12/using-amazon-and-amazon-ecs-to-identify-genres/#comments</comments>
		<pubDate>Mon, 22 Dec 2008 17:55:41 +0000</pubDate>
		<dc:creator>James Stewart</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[amazon]]></category>
		<category><![CDATA[amazon-ecs]]></category>
		<category><![CDATA[browsenodes]]></category>
		<category><![CDATA[categorisation]]></category>
		<category><![CDATA[extraction]]></category>
		<category><![CDATA[genre]]></category>
		<category><![CDATA[onlyconnect]]></category>

		<guid isPermaLink="false">http://jystewart.net/process/?p=522</guid>
		<description><![CDATA[I&#8217;m currently building a site where users will enter details of books and music they&#8217;re listening to and we want to provide lists of that on their profile and also find ways of matching users based on those choices. We&#8217;re looking at a number of ways of doing that, including matching based on genre, and <a href="http://jystewart.net/process/2008/12/using-amazon-and-amazon-ecs-to-identify-genres/" class="more-link">More &#62;</a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently building a site where users will enter details of books and music they&#8217;re listening to and we want to provide lists of that on their profile and also find ways of matching users based on those choices. We&#8217;re looking at a number of ways of doing that, including matching based on genre, and in order to achieve that I needed a way to identify the genres for their listed books and music.</p>
<p>I knew that amazon provided that data through their Associate Web Services (formerly E-Commerce services) so I fired up the trusty <a href="http://www.pluitsolutions.com/projects/amazon-ecs" title="Pluit Solutions   &raquo; Ruby Amazon E-Commerce REST Service API  &nbsp; (amazon-ecs)">amazon-ecs</a> gem and pulled down the data for a couple of books and CDs. eg.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">res = <span style="color:#6666ff; font-weight:bold;">Amazon::Ecs</span>.<span style="color:#9900CC;">item_search</span><span style="color:#006600; font-weight:bold;">&#40;</span>asin, <span style="color:#ff3333; font-weight:bold;">:search_index</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Music'</span>, <span style="color:#ff3333; font-weight:bold;">:country</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'uk'</span>, <span style="color:#ff3333; font-weight:bold;">:response_group</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Large'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Looking at the results I see that amazon assign a varying number of &#8216;browsenodes&#8217; to each item. browsenodes are a flexible system that allows amazon to associate arbitrary hierarchical data to items in their catalogue. They describe them in the documentation as:</p>
<blockquote cite="http://docs.amazonwebservices.com/AWSEcommerceService/2006-05-17/PgDatamodelConceptual.html">
<p>Browse nodes are categories into which items for sale are organized. A single node might have many items associated with it. In the above example, the child node, &#8220;Boxed Sets,&#8221; might have the items &#8220;Abott and Costell Collection,&#8221; and &#8220;Laurel and Hardy Collection&#8221; associated with it.</p>
<p>The number of items associated with a browse node can change radically over time as items are added for sale, or as items go out of stock and are no longer sold. For example, for the browse node, TopSellers, items are attached and unattached according to their sales.</p>
<p>Even browse nodes themselves are created and deleted as items demand. When, for example, a new toy starts selling briskly, there may not be a node that appropriately categorizes the toy. In that case, a node would be created and the toy would be associated with the node. Then, if the sale of the toy died out, the node might be deleted. Other nodes are much longer lived. Top level nodes, for example, &#8220;Books&#8221; and &#8220;Apparel,&#8221; have remained unchanged for years.</p>
</blockquote>
<p>That means that an album may have browsenodes giving us values of &#8220;Pop, Jazz, and Hard Bop&#8221;, but the same item may also have browsenodes with names like &#8220;Bestsellers&#8221;, &#8220;40% off sale&#8221;, and all sorts of other useless data. Thankfully it&#8217;s fairly easy to identify that genre data all sits under &#8220;Styles&#8221; (for music) and &#8220;Subjects&#8221; (for books) which have IDs 520920 and 1025612 respectively in the UK.</p>
<p>Once I had those parents identified it was easy enough to extend the Amazon::Element class to add a method to read out the appropriate browsenodes and return them:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#6666ff; font-weight:bold;">Amazon::Element</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> extract_genre_children_of<span style="color:#006600; font-weight:bold;">&#40;</span>browse_node_id<span style="color:#006600; font-weight:bold;">&#41;</span>
    browse_nodes = elem.<span style="color:#9900CC;">search</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'browsenodes&gt;browsenode'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    genres = browse_nodes.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>collection, bnode<span style="color:#006600; font-weight:bold;">|</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> bnode.<span style="color:#9900CC;">search</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;//ancestors//browsenode/browsenodeid[text()='#{browse_node_id}']&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">length</span> <span style="color:#006600; font-weight:bold;">&gt;</span> <span style="color:#006666;">0</span>
        collection <span style="color:#006600; font-weight:bold;">+</span>= bnode.<span style="color:#9900CC;">search</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'name'</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">collect</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:inner_text<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      collection
    <span style="color:#9966CC; font-weight:bold;">end</span>
    genres.<span style="color:#9900CC;">uniq</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>That code&#8217;s far from perfect &#8211; it returns a few higher level items I subsequently filter out &#8211; but it&#8217;s doing the job for me.</p>
<p>Of course, the resulting data is variable in quality. I certainly wouldn&#8217;t call <a href="http://www.amazon.co.uk/gp/product/B000OLHGBQ?ie=UTF8&amp;tag=sarahmasensof-21&amp;linkCode=as2&amp;camp=1634&amp;creative=19450&amp;creativeASIN=B000OLHGBQ" title="Mirrored: Battles: Amazon.co.uk: Music">Battles&#8217; <em>Mirrored</em></a> &#8220;Adult Contemporary&#8221;, but it&#8217;s a start towards finding some genres automatically and hopefully as we build up a large enough store of data it will begin to become more useful. In the meantime, we&#8217;re also experimenting with musicbrainz to pull in some extra data from their tags.</p>
]]></content:encoded>
			<wfw:commentRss>http://jystewart.net/process/2008/12/using-amazon-and-amazon-ecs-to-identify-genres/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
