Wednesday, December 10, 2014

Getting user modeling data from Watson


Getting user modeling data from Watson


[
Example of getting content and getting it processed by Watson user modeling interface.
It returns json result.
]

public function getwatsonreport1($sentcontent) {
        $randomString1 = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 8);

        $randomString2 = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 8);

        $data = array(

             "id" => $randomString1,

             "userid" => $randomString2,

             "sourceid" => "Ramesh-test",

             "contenttype" => "text/plain",

             "language" => "en",

             "content" => $sentcontent,

        );

        $contentItems1 = array("contentItems" => array($data));

        $data_string = json_encode($contentItems1);

        header('Content-type: application/json');

        $services_json = json_decode(getenv('VCAP_SERVICES'), true);

        $watson = $services_json["user_modeling"][0]["credentials"];

        $username = $watson["username"];

        $password = $watson["password"];

        $url = $watson["url"] . '/api/v2/profile';

        $auth = base64_encode($username . ":" . $password);

        try {

            $curl = curl_init();
            curl_setopt($curl, CURLOPT_HTTPHEADER, array(

                 'Content-Type: application/json',

                 'X-synctimeout: 30',

                 'Authorization: Basic ' . $auth,

                 'Content-Length: ' . strlen($data_string))

            );

 

            curl_setopt($curl, CURLOPT_POST, true);

            curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);

 

            curl_setopt($curl, CURLOPT_URL, $url);

            curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

 

            $result = curl_exec($curl);

 

            curl_close($curl);

 

            return $result;

        } catch (Exception $e) {

            echo '<p>There Was an Error Accessing Watson !!!</p>';

            echo $e->getMessage();

        }

    }

 

 

Analysing usermodelling data returned from Watson


Analysing usermodelling data returned from Watson


[

Watson returns usermodelling data in json format.

This is stored in a file testoutput.json.

Example shows how to process that.

]

public function actionprofileanalyse() {

        $fileName = 'testoutput.json';

        $data = file_get_contents($fileName);

        $decodeddata = json_decode($data, true);

        $adventrous = $decodeddata["tree"]["children"][0]["children"][0]["children"][0]["children"][0];

        echo $adventrous["id"] . "\n";

        $adval = $adventrous["percentage"] * 100;

        $advcount = sizeof($decodeddata["tree"]["children"][0]["children"][0]["children"][0]["children"]);

        $adv = $decodeddata["tree"]["children"][0]["children"][0]["children"][0]["children"];

        for ($i = 0; $i < $advcount; $i++) {

            print $adv[$i]["id"];

            print ($adv[$i]["percentage"] * 100) . "</br>";

        }

        $advcount1 = sizeof($decodeddata["tree"]["children"][0]["children"][0]["children"]);

        $adv1 = $decodeddata["tree"]["children"][0]["children"][0]["children"];

        for ($j = 0; $j < $advcount1; $j++) {

            $advcount2 = sizeof($adv1[$j]["children"]);

            $adv2 = $adv1[$j]["children"];

            for ($i = 0; $i < $advcount2; $i++) {

                print $adv2[$i]["id"] . "</br>";

                print ($adv2[$i]["percentage"] * 100) . "</br>";

            }

        }

    }