RDF- Crack (RDFa, triples,FOAF) ( part 2 )

July 1, 2008

RDF is a way of presenting knowledge, relationship or a thing in the format of triples. For everything in the web will be represented as a triples format. As we know about grammer we read it in our child hood, like every basic sentence is represented as format of triples. That means subject, predicate & objects.

FOAF:

FOAF is the vocabulary to represent RDF the objects & relationship between them. FOAF is a way of describing a person / company. FOAF is Friend of A friend using RDF to represent its data. It will describe a person in the following way.

nandakumar -> is -> http://www.desiji.com/ndotnanda

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:foaf="http://xmlns.com/foaf/0.1/">
 <foaf:Person>
   <foaf:name>Nandakumar</foaf:name>
   <foaf:mbox rdf:resource="http://www.desiji.com/ndotnanda"/>
 </foaf:Person>
</rdf:RDF>

N3 Notation & triples:
A syntax to represent RDF.
@prefix pref <http://www.example.org/vacobulary #>.
<#nandakumar><pref:working><#ndot>

RDFa:
RDFa is way to integrate RDF into XHTML pages. It reduces the lot of work & difficulties for
normal people those who want to represent their blog / personal data into semantic web. RDFa helps
them to represent their information to semantic web with XTHML. we can take an small example about
how we can represent a data with RDFa,

Here we are going to explain a FOAF integration into XHTML page.

Triples:
Nandakumar knows Preethi

RDFa:

<body xmlns:foaf="http://xmlns.com/foaf/0.1">
<span about="#Nandakumar" property="foaf:name">
Nandakumar Somasundaram
</span>
</body>
Triple : Nandakumar -> Name -> Nandakumar Somasundaram

RDFa : example 2
<body xmlns:foaf="http://xmlns.com/foaf/0.1">
<span about="#Nandakumar" property="foaf:name">
Nandakumar Somasundaram
</span>
<span about="#Preethi" property="foaf:name">
Preethi
</span>
<span about="#Nandakumar" rel="foaf:knows" resource="#Preethi">
Nandakumar is classmate of preethi
</span
</body>

Open Calais API & examples – PHP

June 27, 2008

Open Calais is a rapidly growing toolkit of capabilities that allow you to readily incorporate state-of-the-art semantic functionality within your blog & provide more or less same functionality as what zemanta guys do.

Open calais use NLP to manipulate user’s queries. They are giving API access to multiple languages. Here i can explain the samples with php.

Live URL : http://livebillion.com/calais/

index.php

<form name=”calais” action=”octest.php” method=”post”>

URL : <input type=”text” name=”url” value=”" /> <input type=”submit” name=”submit” value=”Get Tags!” />

</form>

octest.php

$apikey = “XXXXXXXXXX”;

$oc = new OpenCalais($apikey);

if(isset($_REQUEST["url"])){
$content = file_get_contents($_REQUEST["url"]);
}

//echo $content;

$entities = $oc->getEntities($content);

foreach ($entities as $type => $values) {

echo “<b>” . $type . “</b>”;
echo “<ul>”;

foreach ($values as $entity) {
echo “<li>” . $entity . “</li>”;
}

echo “</ul>”;

}

opencalais.php

http://www.dangrossman.info/open-calais-tagsopen-calais-tags
All the best :)

Zemanta Pixie

Zemanta API & examples – over view with PHP

June 27, 2008

Zemanta provides live suggesions when you start writing blogs.  Zemanta also a browser based plugin gives you suggestions based on your text that you typing, image, url’s & more.

What is the purpose of using Zemanta ?

You can get relevant tags what you have typed.

NLP – Natural Language processing, it analyze your input text with lexical analyzer & find out the tags(/objects/names)

Based on that tags, it lists the number of URL’s/ images found related to those tags.

Input  : “text”

Output : Related URLs, Tags & images

Sample Code how it works,

Contact Zemanta & get API from them.

Live Testing URL :  http://livebillion.com/calais/test_zemanta.php

test_zemanta.php

——–

<form action=”dozemanta.php”>
<table width=”100%”>
<tr><th colspan=”2″>Testing Zemanta API</th></tr>
<tr><td>Text:</td><td>
<textarea name=”text” style=”width:500px;”></textarea>
</td></tr>
<tr><td> ——— Or ————–</td></tr>
<tr><td>URL:</td><td>
<input name=”url” type=”text” value=”" style=”width:500px;”/>
</td></tr>
<tr><td> <input type=”submit” name=”btn” value=”Extract” /></td></tr>
</table>
</form>

dozemanta.php

$url = ‘http://api.zemanta.com/services/rest/0.0/’; //Should be in a conffile
$format = ‘xml’; // May depend of your application context
$text = $_REQUEST["text"];
if($_REQUEST["url"]!=”"){
$text = strip_tags(file_get_contents($_REQUEST["url"]));
}

$key = “YOUR_API_KEY”; //Should be in a conf file
$method = “zemanta.suggest”; // May depend of your application context

$args = array(
‘method’=> $method,
‘api_key’=> $key,
‘text’=> $text,
‘format’=> $format
);

$data = “Here we build the data we want to POST to Zementa”;
foreach($args as $key=>$value)
{
$data .= ($data != “”)?”&”:”";
$data .= urlencode($key).”=”.urlencode($value);
}

/* Initialisation of curl */
$ch = curl_init();
/* Setup of the url*/
curl_setopt($ch, CURLOPT_URL, $url);
/* We want a post request */
curl_setopt($ch, CURLOPT_POST, 1);
/* Here we give to curl the data we want to send to Zementa*/
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
/* We setup the response method of curl */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
/* Execute curl and fetch the result */
$response = curl_exec ($ch);
/* Close curl connection */
curl_close ($ch);
//echo $response;

$xmlArray = xml2array($response); // XML2ARRAY is a function to covert xml to array

// Get the top array

$content = $xmlArray["rsp"];

// Related Articles

foreach($content["articles"] as $articles){
$i = 0;
echo ‘<table width=”100%”><tr><th colspan=2>Related Articles:</th></tr>’;
foreach($articles as $article){
//echo $i;
//echo $article["url"]["value"];
echo ‘<tr><td><a href=”‘.$article["url"]["value"].’” target=”_new” >’.$article["title"]["value"].’</a></td></tr>’;
$i++;
}
//print_r($articles[0]["url"]["value"]);
echo ‘</table>’;
}

// Related Keywords

foreach($content["keywords"] as $keywords){
//print_r($markup);
$i = 0;
echo ‘<table width=”100%”><tr><th colspan=2>Keywords:</th></tr><tr><td>’;
foreach($keywords as $keyword){
//echo $i;
//echo $article["url"]["value"];
echo ‘<a href=”http://www.popdup.com/tag/’.$keyword["name"]["value"].’” target=”_new” >’.$keyword["name"]["value"].’</a>   ’;
$i++;
}
//print_r($articles[0]["url"]["value"]);
echo ‘</td></tr></table>’;
}

// Related Images

foreach($content["images"] as $images){
//print_r($markup);
$i = 0;
echo ‘<table width=”100%”><tr><th colspan=2>Images:</th></tr>’;
foreach($images as $image){
//echo $i;
//echo $article["url"]["value"];
echo ‘<tr><td><a href=”‘.$image["url_l"]["value"].’” target=”_new” ><img src=”‘.$image["url_s"]["value"].’” style=” border:0px;” /></a></td><td>’.$image["description"]["value"].’</td></tr>’;
$i++;
}
//print_r($articles[0]["url"]["value"]);
echo ‘</table>’;
}

Zemanta Pixie