Help - Search - Members - Calendar
Full Version: Turning your MT blog into a Semantic Web
Movable Type Community Forum > Additional Resources > Tips and Tricks
ishbadiddle
Update: A more complete tutorial for this method is now on LMT, thanks Elise!


Some time ago on this forum, I posted my "recipe" for using the Keyword field to create an open-ended, bottom-up classification scheme. I've made some tweaks to the code since then, so I thought I'd share with you.

First, you might want to read my blog post on why you might want to do this. Namely, why coding your blog using keywords can turn your blog into a semantic web that is easier for your readers to browse. This is especially useful, I think, for group blogs, but the methodology can be used on any sort of blog.

Basically, what this method does is link your blog posts together by keyword. Unlike categories, which are limited, top-down, and hierarchical, keywords are open-ended and limitless. You can see how it works on my blog, Ishbadiddle. And here's how to do it:

1. Get the Plugins

You'll need the following plugins: IfEmpty, MTLoop, Compare, and MTCollate.

Install the plugins.

2. Code your posts.

This system assumes that your keywords are formatted thusly:

CODE
Penguins, George W. Bush, New York City


Each keyword is separated by a comma and a space. Capitalization matters. Anything can go into a keyword, although &s make it go kind of wonky.

3. Make a new keyword-only search template


Next, you'll need to modify your search function so that you can limit the search to the keyword field. Follow the instructions for the hack on Stepan Riha's site.

Next you'll create a new template, just to use for keyword extraction. Get your regular search template (default.tmpl), rename it subject.tmpl. My basic code for the template is:

CODE
<MTSearchResults>

    <MTBlogResultHeader>
    <h2 class="date">Subject Index for <$MTSearchString$></h2>
    </MTBlogResultHeader>


<div class="posts">
    <h3 class="title"><a href="<$MTEntryPermalink$>"><$MTEntryTitle remove_html="1"$></a></h3>
    <$MTEntryExcerpt$><br />
    <span class="author"><$MTEntryAuthor$></span><span class="byline"> wrote this on <$MTEntryDate format="%B %e, %Y"$></span><br /><br />
    </div>

</MTSearchResults>


Of course you can change the styling for your own blog, and list the contents however you choose (full posts?)

The other addition to your keyword search template is the list of related keywords. I put this in my sidebar.

CODE
<b>Other Subjects Related to "<$MTSearchString$>"</b><br/>

<MTCollateCollect>
<MTSearchResults>
<MTLoop values="[MTEntryKeywords]" delimiter=", "><MTCollateRecord>
<MTCollateSetField name="subject_keyword">[MTLoopValue]</MTCollateSetField></MTCollateRecord></MTLoop>
</MTSearchResults>
</MTCollateCollect>

<MTCollateList sort="subject_keyword:+:i:d">
<MTIfNotEqual a="[MTSearchString]" b="[MTCollateField name='subject_keyword']">
<a href="http://WWW.YOURURL.NET/mt/mt-search.pl?IncludeBlogs=1&SearchField=keywords&Template=subject&CaseSearch=1&ResultDisplay=Ascending&search=<MTCollateField name="subject_keyword">" title="Index of posts relating to <MTCollateField name="subject_keyword">"><MTCollateField name="subject_keyword"></a><br />
</MTIfNotEqual>
</MTCollateList>


This creates a list of keywords that is related to the one on the page they're looking at. It's generated by looking at all the keywords that occur in posts along with the current keyword. Fun to browse!

Upload subject.tmpl into the proper folder (see here for instructions.)

4. Create a full list of keywords

Make a new template, one that will auto-update. The following code will make a list of all the keywords used on your blog:

CODE
<h3 class="title">Subjects</h3>

<div class="posts">

<MTCollateCollect>
<MTEntries lastn="9999">
<MTLoop values="[MTEntryKeywords]" delimiter=", "><MTCollateRecord>
<MTCollateSetField name="subject_keyword">[MTLoopValue]</MTCollateSetField></MTCollateRecord></MTLoop>
</MTEntries>
</MTCollateCollect>


<MTCollateList sort="subject_keyword:+:i:d">
<a href="http://WWW.YOURURL.NET/mt/mt-search.pl?IncludeBlogs=1&SearchField=keywords&Template=subject&CaseSearch=1&ResultDisplay=Ascending&search=<MTCollateField name="subject_keyword">" title="Index of posts relating to <MTCollateField name="subject_keyword">"><MTCollateField name="subject_keyword"></a><br />
</MTCollateList>


I use similar code to create category-specific sets of keywords, which are much more browsable than the full list. This I put in a new category archive.

CODE
<h3 class="title"><$MTArchiveCategory$> Subject Index</h3>

<div class="posts">
<i>This is the list of subjects covered in <$MTArchiveCategory$> posts. You can also read <a href="<$MTBlogURL$>archives/<$MTArchiveCategory dirify="1"$>">all the posts in the <$MTArchiveCategory$> category here.</a></i>
<br/><br/>
</div>


<div class="posts">

<MTCollateCollect>
<MTEntriesWithSubCategories>
<MTLoop values="[MTEntryKeywords]" delimiter=", "><MTCollateRecord>
<MTCollateSetField name="subject_keyword">[MTLoopValue]</MTCollateSetField></MTCollateRecord></MTLoop>
</MTEntriesWithSubCategories>
</MTCollateCollect>



<MTCollateList sort="subject_keyword:+:i:d">
<a href="http://WWW.YOURURL.NET/mt/mt-search.pl?IncludeBlogs=1&SearchField=keywords&Template=subject&CaseSearch=1&ResultDisplay=Ascending&search=<MTCollateField name="subject_keyword">" title="Index of posts relating to <MTCollateField name="subject_keyword">"><MTCollateField name="subject_keyword"></a><br />
</MTCollateList>
</div>
<br/><br/>
</div>
(Note that I'm using MTEntriesWithSubCategories here, but you don't have to.)


5. Coding your main blog

Finally, you'll want to put this code into your main blog index, as part of your code for each post within MTEntries:

CODE
<MTIfNotEmpty var="EntryKeywords"><br /><br />See also: <MTLoop values="[MTEntryKeywords]" delimiter=", "> <a href="http://WWW.YOURURL.NET/mt/mt-search.pl?IncludeBlogs=1&SearchField=keywords&Template=subject&CaseSearch=1&ResultDisplay=Ascending&search=[MTLoopValue]" title="Index of posts relating to [MTLoopValue]">[MTLoopValue]</a> |</MTLoop></MTIfNotEmpty>


That will create a list of keywords, separated by "|"s, each of which will link to a list of posts in chronological order on that subject.

Pretty neat, huh?

You'll want to include that code on your Individual Archive pages as well.

If you want to get fancy, you can create a list of recent topics for your sidebar:

CODE
<!--Recent Topics-->
<a name="recenttopics"></a>
<b>What We're Talking About Lately</b><br>
    

<MTCollateCollect>
<MTEntries lastn="20">
<MTLoop values="[MTEntryKeywords]" delimiter=", "><MTCollateRecord>
<MTCollateSetField name="subject_keyword">[MTLoopValue]</MTCollateSetField></MTCollateRecord></MTLoop>
</MTEntries>
</MTCollateCollect>

<MTCollateList sort="subject_keyword:+:i:d">
<a href="http://WWW.YOURURL.NET/mt/mt-search.pl?IncludeBlogs=1&SearchField=keywords&Template=subject&CaseSearch=1&ResultDisplay=Ascending&search=<MTCollateField name="subject_keyword">" title="Index of posts relating to <MTCollateField name="subject_keyword">"><MTCollateField name="subject_keyword"></a><br />
</MTCollateList>


And that's it. I think there are lots of uses for this, so I wanted to share the code. Let me know if you have suggestions, questions, kudos, etc.
ishbadiddle
I almost forgot the meta-tagging! You'll need Brad Choate's Regex plugin for this. Put the following in the header information for your Individual Entry Archive:

CODE
<MTRegexDefine name="delimiter">s|, |" /><meta name="DC.subject" content="|g</MTRegexDefine>

<MTIfNotEmpty var="EntryKeywords"><meta name="DC.subject" content="<$MTEntryKeywords regex="delimiter"$>" /></MTIfNotEmpty>


That will put the metadata onto each entry page, using the Dublin Core standard.
ishbadiddle
Important! For reasons having to do with my server, my own MT files have the .pl extension instead of .cgi. Most of you will have .cgi files, so do a search-and-replace on .pl for the code above.
ishbadiddle
New! Support for Technorati tags. Here's the code I use.

CODE
<MTIfNotEmpty var="EntryKeywords"><br /><br />See also: <MTLoop values="[MTEntryKeywords]" delimiter=", "> <a href="http://www.YOURSITE/mt/mt-search.cgi?IncludeBlogs=1&SearchField=keywords&Template=subject&CaseSearch=1&ResultDisplay=Ascending&search=[MTLoopValue]" title="Index of posts relating to [MTLoopValue]">[MTLoopValue]</a><a href="http://technorati.com/tag/[MTLoopValue]" rel="tag" title="Technorati tag page for [MTLoopValue]"><img src="http://www.YOURSITE/images/technotag.gif" border=0 alt="Technorati icon"></a> |</MTLoop></MTIfNotEmpty>


Here is the image I use for the link:

[IMG]http://www.triptronix.net/ishbadiddle/images/technotag.gif[/IMG]

(Please use but host on your site, thanks!)
dbabbage
ishbadiddle, you rock!

You have saved me so much time. More than that, you've enabled me to do something I'd concluded was impossible without turning hundreds of keywords into categories...

You can see the result in a slightly non-traditional use of MT at HearingGodsVoice.org.uk, a site I've been setting up for some older friends...

Thanks for this excellent solution. Well done!
superblue
ishbadiddle, this is indeed very interesting! Thanks for sharing, may be just what I'm looking for. Do you happen to know if double-byte characters (e.g. Japanese) work in this setup too? If so, I'm a very happy man!
ishbadiddle
superblue: I'm not sure if it works in Japanese, but it should, as long as your search function supports it. Let me know if that works out for you.

Oh, and one more thing I forgot about integrating Technorati tags into this solution: you can't just put that tag code into your main index. Even if you're pinging technorati, it won't "see" the tag on your index. You must put it into your RSS feed, which is what technorati is reading. I was very confused about that.
superblue
ishbadiddle, I've finally got round to trying out your tutorial and have to say I'm very impressed! I usually find hacking the system ends in endless errors, but for the first time, I've got there without a glitch - THANKYOU!

The system seems to work fine using Japanese (have yet to thoroughly test it, but no problems so far).

However, I'm using your idea across multiple blogs (MT 2.6). And on the top "portal" page for all the blogs, I've used your suggested code to give a list of the "latest" keywords used in the site (covering multiple blogs) by using the OtherBlog plugin.

CODE
<MTOtherBlog exclude_blog_id="1,2,8">
<MTCollateCollect>
<MTEntries lastn="10">
<MTLoop values="[MTEntryKeywords]" delimiter=", "><MTCollateRecord>
<MTCollateSetField name="subject_keyword">[MTLoopValue]</MTCollateSetField></MTCollateRecord></MTLoop>
</MTEntries>
</MTCollateCollect>
<MTCollateList sort="subject_keyword:+:i:d">
<a href="/cgi-bin/movabletype/mt-search.cgi?SearchField=keywords&Template=subject&CaseSearch=1&ResultDisplay=Ascending&search=<MTCollateField name="subject_keyword">"> <MTCollateField name="subject_keyword"></a>,
</MTCollateList>
</MTOtherBlog>


This is giving me 2 problems :

1) Repeated keywords (i.e. if the same keyword is used in 2 blogs, it appears twice)

2) The "portal page" is a separate blog, and needs rebuilding to reflect keywords added since the previous rebuild.

I'm thinking one solution to the 2nd problem is to use php includes on the portal page, bringing in data from "latest keyword" templates in each of the other blogs. A downside would be that the keywords would be listed by the order of the includes, as oppsed to something more natural like alphabetically or newest first etc...

Just wondering if you have any takes on the problem - perhaps you know of a way to solve both problems by somehow exporting the list of keywords from the mutliple blogs, stripping duplicates and dropping that list onto a portal page? (wishful thinking! biggrin.gif ) Am not much of a programmer, so am limited to the basics and what I can scrounge form these forums!

Anyway, if you have any ideas I'd love to hear them. Thanks again for sharing your code!
superblue
Well the repeated keywords problem seems to have disappeared somehow... not sure what that was! I've overcome the rebuild problem by using mt-rebuild and an hourly cron job. So all's well there.

What I'm wodering next is if it's possible to develop the keyword listing into a weighted tag cloud (showing densely populated keywords in a larger font) like on Technorati and Flickr... Has anybody had any luck doing this in MT yet?
almuhajabah
This is a great idea and I've been working to implement the different ideas one at a time to my main blog.

I recently figured out how to do a tag cloud. It requires PHP but is fairly easy to set up. See here.
elisebauer
Hi Al-Muhajabah,

This is quite cool, thanks for sharing.

(and welcome back!)
almuhajabah
Thanks, Elise smile.gif

My current job keeps me too busy to come by here very often, but I do lurk in some of the interesting topics like this one smile.gif
csross
I'm looking to have users add tags to my site for references on their site. Will this (or something like it) allow me to do that? Do you have an idea of how to accomplish this?

Thanks so much
elisebauer
csross - I think you are referring to "TrackBacks". Check the MT Manual for an explanation of that feature.
hatchback
Thanks, Ishbadiddle! Your instructions were super easy! I was able to set up a rudimentary version of this on my site in about 30 minutes. I used a comma to separate the keywords instead of the vertical bar.

One question, though: How do I get rid of the last comma or vertical bar among the list of keywords? It looks strange to have either punctuation mark just hanging out there.

Thanks again!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.