Step 4: Create Custom Reports with SPARQL
Fine-grained stream filtering is already helpful in terms of noise reduction and interest tracking. But in order to
let Twitter assist you even more, you will also need quick access to custom query results. After all, it's not the
post, but the information
in the post that enables automation. One option is to simply bookmark query URLs
from the SPARQL API. For example, a query to generate a list of your online contacts (not the people in your
followers list, but those you really interact with) is not too complicated:
SELECT DISTINCT ?name WHERE {
# people mentioned by me
?post sioc:has_creator ?account ;
smr:mentionedUser ?name .
?account rdfs:label "your_twitter_username" .
# people who mentioned me
?post2 sioc:has_creator ?account2 ;
smr:mentionedUser "your_twitter_username" .
?account2 rdfs:label ?name .
}
A single SPARQL operation alone might not be enough, though, especially if you want to inject additional data or
reformat the results. In this case, you can extend the main page with your own tabs. Open
code/smr/SMR_ViewBox.php and add your own entries to the
getTabs
method. This works similar to extending the filters, but this time you have to create a view class in
code/smr/custom/ for each new tab:
function getTabs() {
return array(
'all' => array('label' => 'All posts'),
'contacts' => array('label' => 'My Contacts'),
'bookmarks' => array('label' => 'Bookmarks'),
);
}
function getAllTabHTML() {
$tab = Trice::getObject('SMR_Stream_Tab', $this->a, $this->caller);
return $tab->getHTML();
}
function getContactsTabHTML() {
$tab = Trice::getObject('SMR_Custom_ContactsTab', $this->a, $this->caller);
return $tab->getHTML();
}
function getBookmarksTabHTML() {
$tab = Trice::getObject('SMR_Custom_BookmarksTab', $this->a, $this->caller);
return $tab->getHTML();
}
 |
|
|
Figure 5. Custom Tabs with Bookmarks and Proven Contacts: The bookmark query was aligned with the filtering mechanism, so that you can use facets to narrow down the
results. |
You can also fine-tune the stylesheet information in
code/smr/custom/custom.css if you like.
Listing 1 contains the complete class for a basic bookmarks tab
that orders entries by popularity (see
Figure 5).
Other possible use cases include project reports or a birthday reminder. To a certain extent, you can implement the former based simple tags, but the latter requires the ability to detect and extract the month and calendar day of a person's birthday from a micropost.
Step 5: Type Your Tags and Run More Powerful Queries
You can filter your microposts nicely and also create custom reports now. However, as just mentioned, for a personal
assistant you will need even more granularity. Technically, this is not too much of a problem as the data model of
your RDF-driven application can be freely extended at run-time. The question is rather how to add the functionality to
existing Twitter clients. You want to add more structure than provided through tags, but are restricted to a simple
input form and the maximum length of 140 characters. The solution proposed in this article is a slightly extended tag
syntax, similar to machine tags on
Flickr, but for now
without the namespace-qualified keys.
You can tweak the regular expressions in the RDFExtractor (code/smr/SMR_RDFExtractor.php) to
add support for such typed tags that follow a #key=value pattern. (You actually don't have to
write this method yourself, it's already part of the code bundle.) Now you can add something like
#birthday #month=08, #todo #priority=A or
#done #task="DevX article" #hours=16 to your tweets and let the application extract the classified tags
(which use a local my: namespace in the resulting RDF). Create conventions you feel comfortable with and let them evolve.
 |
|
|
Figure 6. Basic Summary of Working Hours: The report uses SPARQL+ to aggregate working hours and then groups the results by related tags. |
Typed tags open the door to very sophisticated and automated reports on top of Twitter, for example, a personal
software bug tracker (see
Figure 6):
SELECT DISTINCT ?post ?prio ?text WHERE {
?post smr:tag "todo" , "bug" ;
my:priority ?prio ;
content:encoded ?text .
}
ORDER BY ASC(?prio)
or a calculator for working hours:
SELECT ?project SUM(?hours) as ?sum WHERE {
?post my:hours ?hours ;
my:project ?project .
}
GROUP BY ?project
or upcoming birthdays:
SELECT ?text WHERE {
?post content:encoded ?text ;
smr:tag "birthday" ;
my:month "2" ;
my:day ?day .
FILTER(?day > 10)
}
ORDER BY ?day
Further Steps
This article demonstrated the basic building blocks and possibilities for accessing and processing semantic Twitter posts. The obvious next step is to create a dedicated microblogging client that keeps selected tweets or tags private. A long-running background script that sends alerts for pre-defined events would also be handy. Another compelling idea is a script that uses SPARQL to combine the simple working hours example with external information to generate a more complete project report; or perhaps a monthly invoice.