eXorithm – Execute Algorithm: View / Run Algorithm part_of_speech

Logo Beta

function part_of_speech ($word
{
  $data = file_get_contents"http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q="urlencode$word)."&sl=en&tl=en&restrict=pr%2Cde&client=te");
  
  if ( (substr$data, 0, 25)=="dict_api.callbacks.id100(") &&
       (substr$data, -10)==",200,null)") )
  {
    $data = substr$data, 25, -10);
    $data = str_replace'x22', '"', $data);
    $data = decode_phpstring$data);
    
    $parts = array();
    
    $obj = json_decode$data);
    
    if (isset$obj->primaries)) {
      foreach ($obj->primaries as $prim) {
        $headword = ''
        $part = ''
        foreach ($prim->terms as $term) {
          if ($term->type=="text") {
            $headword = str_replace'·', '', $term->text);
            foreach ($term->labels as $label) {
              if ($label->title=="Part-of-speech") {
                
                $st = false;
                $part = strtolower$label->text);
                
                foreach ($prim->entries as $entry) {
                  if ($entry->type=="related") {
                    foreach ($entry->terms as $term2) {
                      if ($term2->text==$word) {
                        $st = true;
                        $i = count$parts);
                        $parts$i]['type'] = $part
                        $parts$i]['base'] = $headword
                        $parts$i]['subtype'] = $term2->labels[0]->text;
                      }
                    }
                  }
                }
                
                if (!$st) {
                  $i = count$parts);
                  $parts$i]['type'] = $part
                  $parts$i]['base'] = $headword
                  $parts$i]['subtype'] = ''
                }
              }
            }
          }
        }
      }
    }
    return $parts
    
  } else {
    throw new Exception"Google dictionary API did not return a result.");
  }
}Â