cabadsanchez
11/20/2015 - 12:01 PM

This gist shows how to obtain the morphological analysis of all the elements in a text using MeaningCloud's Lemmatization, PoS and Parsing 2

This gist shows how to obtain the morphological analysis of all the elements in a text using MeaningCloud's Lemmatization, PoS and Parsing 2.0

<?php

/**
 * PoS and Parsing 2.0 example for PHP.
 *
 * In order to run this example, the license key must be included in the key variable.
 * If you don't know your key, check your account at MeaningCloud (https://www.meaningcloud.com/developer/account/licenses)
 *
 * You can find more information at http://www.meaningcloud.com/developer/lemmatization-pos-parsing/doc/2.0
 *
 * @author     MeaningCloud (support@meaningcloud.com)
 * @copyright  Copyright (c) 2015, MeaningCloud LLC. All rights reserved.
 */

// We define the variables needed to call the API
$api = 'http://api.meaningcloud.com/parser-2.0';
$key = '<<<your license key>>>';
$txt = '<<< text to analyze >>>';
$lang = 'en';  // es/en/fr/it/pt/ca


// We make the request and parse the response to an array
$response = sendPost($api, $key, $lang, $txt);
$json = json_decode($response, true);

$morpho = array();

//we traverse the syntactical tree saving the leaves we find
if(isset($json['token_list'])){
  foreach($json['token_list'] as $sentence){
    traverseTree($sentence, $morpho);
  }
}

// Show the morphological analyses
echo "\nTokens:\n";
echo "==============\n";
foreach($morpho as $m){
  if(isset($m['analysis_list'])){
      echo $m['analysis_list'][0]['original_form']."\n";
    foreach($m['analysis_list'] as $k=>$a)
     echo "\t".$a['tag'].': '.$a['tag_info']."\n";
  }
}
echo "\n";


// Auxiliary function to make a post request
function sendPost($api, $key, $lang, $txt) {
  $data = http_build_query(array('key'=>$key,
                                 'lang'=>$lang,
                                 'txt'=>$txt,
                                 'verbose' => 'y',
                                 'src'=>'sdk-php-ma-2.0')); // management internal parameter
  $context = stream_context_create(array('http'=>array(
        'method'=>'POST',
        'header'=>
          'Content-type: application/x-www-form-urlencoded'."\r\n".
          'Content-Length: '.strlen($data)."\r\n",
        'content'=>$data)));

  $fd = fopen($api, 'r', false, $context);
  $response = stream_get_contents($fd);
  fclose($fd);
  return $response;
} // sendPost


 // This function traverses the tree with the analysis of the input (token). Every time it
 // reaches a leaf, it adds it to the morphological array.
function traverseTree(&$token, &$morpho){

if(isset($token['token_list'])){
    foreach($token['token_list'] as $t){
      if(isset($t['token_list'])){ //if it has token children, it's not a leaf
        traverseTree($t, $morpho);
      }else{ // it's a leaf!
        $morpho[]=$t;
      }
    }
  }
}//traverseTree
?>