More Related Content
Similar to C A S Sample Php (20)
C A S Sample Php
- 1. index.php:
<?php
/* Sample Code for Y! CAS Open API
*
* Author: Chloris Kuo 菲芳郭
* 2009/10/17 Y! Open Hack TW 2009
*/
if(!isset($content)) $content = $_GET['c'];
echo <<<END
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<meta http-equiv='Content-Language' content='zh-TW'>
<TITLE>CAS demo</TITLE>
<FRAMESET COLS='50%,50%'>
<FRAME NAME='left' SRC="left.php?content=$content">
<FRAME NAME='right' SRC="test.php?content=$content">
</FRAMESET>
END;
?>
left.php:
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
</head>
<body>
<center>
<form action="test.php" method="POST" target="right">
 
<h2>CAS Demo</h2>
<p>
<TEXTAREA rows="20" cols="50" name="content">
肉豬凍冷良優是我哈哈
</TEXTAREA>
- 2.  
<p>
method:<select name="method" onchange="this.form.submit();">
<option value="ws" selected>Word Segmentation</option>
<option value="ke">Keyword Extraction</option>
</select>
<p>
<p>
<input type="submit" value="SUBMIT">
<input type="reset" value="RESET">
</form>
</body>
</html>
test.php:
<html>
<head></head>
<body>
<center>
<?php
/* Sample Code for Y! CAS Open API
*
* Author: Chloris Kuo 菲芳郭
* 2009/10/17 Y! Open Hack TW 2009
*/
$appid = ''; // Your appid here
$casapi= 'http://asia.search.yahooapis.com/cas/v1/';
$content = $_POST['content'];
$method = $_POST['method'];
$url = $casapi.$method;
//echo "method: $method, content: $content, url: $url <p>";
- 3. $data = array('appid' => $appid, 'content' => $content);
$color = array(
'ADVERB'=>'00CC00',
'PRONOUN'=>'00CCCC',
'VERB'=>'CC0033',
'NOUN_VERBAL'=>'CC33FF',
'NOUN'=>'FF6600',
'PROPER_NOUN'=>'9933CC',
'PREPOSITION'=>'3399CC',
'NUMERAL'=>'FF99FF',
'NOUN_TIME'=>'00CC66',
'QUANTIFIER'=>'6600FF',
'POSTPOSITION'=>'6666CC',
'ADJECTIVE'=> '009999',
'CONJUNCTION'=>'CC9933',
'PARTICLE'=>'FF00FF',
'DETERMINER'=>'333300',
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_TIMEOUT, _DCP_TIMEOUT_);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
curl_close($curl);
$parser = xml_parser_create("UTF-8");
xml_parse_into_struct($parser, $result, $XML, $index);
xml_parser_free($parser);
//var_dump($result);
if($method == 'ws'){
echo " <p><h2>Segmentation Result</h2>";
echo "<table border=0 width=90%><tr><td>";
- 4. foreach($index as $key=>$val){
if($key == "TOKEN"){
foreach($val as $i => $idx){
$pos = $XML[$idx+4]['value'];
// echo "'$pos':";
if($pos != '' && $pos != null){
if($pos == 'NOUN'){
echo "<u>";
}
echo "<font color=#".$color[$pos].">";
echo $XML[$idx]['value']."</font>";
//echo "<font size='8pt'>".$pos." </font>";
if($pos == 'NOUN'){
echo "</u>";
}
echo " ";
}
}
}
}
echo "</td></tr></table>";
}
else{
echo " <p><h2>Keyword Extraction Result</h2> <p>";
foreach($index as $key=>$val){
if($key == "TOKEN"){
foreach($val as $i => $idx){
$score = $XML[$idx+1]['value'];
echo $XML[$idx]['value']." Score:$score<br>";
}
}
}
//var_dump($result);
}
?>
</body>
</html>