SlideShare a Scribd company logo
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">
&nbsp
<h2>CAS Demo</h2>
<p>
<TEXTAREA rows="20" cols="50" name="content">
肉豬凍冷良優是我哈哈
</TEXTAREA>
&nbsp
<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>";
$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 "&nbsp<p><h2>Segmentation Result</h2>";
     echo "<table border=0 width=90%><tr><td>";
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."&nbsp;&nbsp;</font>";
                         if($pos == 'NOUN'){
                            echo "</u>";
                         }
                         echo "&nbsp;";
                     }
                 }
             }
         }
         echo "</td></tr></table>";
     }
     else{
        echo "&nbsp<p><h2>Keyword Extraction Result</h2>&nbsp<p>";
         foreach($index as $key=>$val){
             if($key == "TOKEN"){
                 foreach($val as $i => $idx){
                    $score = $XML[$idx+1]['value'];
                     echo $XML[$idx]['value']."&nbsp;Score:$score<br>";
                 }
             }
         }
         //var_dump($result);
     }
?>
</body>
</html>

More Related Content

PDF
Pemrograman Web 9 - Input Form DB dan Session
Nur Fadli Utomo
 
PDF
wget.pl
Yasuhiro Onishi
 
PDF
Pemrograman Web 8 - MySQL
Nur Fadli Utomo
 
PPTX
London XQuery Meetup: Querying the World (Web Scraping)
Dennis Knochenwefel
 
PDF
R57shell
ady36
 
PDF
PHP an intro -1
Kanchilug
 
DOCX
Table through php
syeda zoya mehdi
 
PPSX
web design and jQuery introduction in persian
Ahmad Badpey
 
Pemrograman Web 9 - Input Form DB dan Session
Nur Fadli Utomo
 
Pemrograman Web 8 - MySQL
Nur Fadli Utomo
 
London XQuery Meetup: Querying the World (Web Scraping)
Dennis Knochenwefel
 
R57shell
ady36
 
PHP an intro -1
Kanchilug
 
Table through php
syeda zoya mehdi
 
web design and jQuery introduction in persian
Ahmad Badpey
 

What's hot (19)

PPTX
5. hello popescu
Razvan Raducanu, PhD
 
PDF
Advanced modulinos trial
brian d foy
 
PDF
Advanced modulinos
brian d foy
 
PDF
Perl Fitxers i Directoris
frankiejol
 
PDF
DEV Čtvrtkon #76 - Fluent Interface
Ctvrtkoncz
 
PDF
News of the Symfony2 World
Fabien Potencier
 
PDF
Hidden in plain site – joomla! hidden secrets for code monkeys
Nicholas Dionysopoulos
 
PDF
Bag of tricks
brian d foy
 
PDF
PHP and Rich Internet Applications
elliando dias
 
PPTX
6. Php MongoDB adaugarea unui document
Razvan Raducanu, PhD
 
PDF
Perl Bag of Tricks - Baltimore Perl mongers
brian d foy
 
RTF
Document
viwviw
 
PDF
Not Really PHP by the book
Ryan Kilfedder
 
DOCX
TICT #13
azman_awan9
 
PDF
Introduzione JQuery
orestJump
 
PDF
TDC2015 Porto Alegre - Automate everything with Phing !
Matheus Marabesi
 
DOCX
TICT #11
azman_awan9
 
5. hello popescu
Razvan Raducanu, PhD
 
Advanced modulinos trial
brian d foy
 
Advanced modulinos
brian d foy
 
Perl Fitxers i Directoris
frankiejol
 
DEV Čtvrtkon #76 - Fluent Interface
Ctvrtkoncz
 
News of the Symfony2 World
Fabien Potencier
 
Hidden in plain site – joomla! hidden secrets for code monkeys
Nicholas Dionysopoulos
 
Bag of tricks
brian d foy
 
PHP and Rich Internet Applications
elliando dias
 
6. Php MongoDB adaugarea unui document
Razvan Raducanu, PhD
 
Perl Bag of Tricks - Baltimore Perl mongers
brian d foy
 
Document
viwviw
 
Not Really PHP by the book
Ryan Kilfedder
 
TICT #13
azman_awan9
 
Introduzione JQuery
orestJump
 
TDC2015 Porto Alegre - Automate everything with Phing !
Matheus Marabesi
 
TICT #11
azman_awan9
 
Ad

Viewers also liked (6)

PPT
2009 Hackday Taiwan Yui
JH Lee
 
PPT
Tw Mail Open Hack Day 1
JH Lee
 
PPT
K+ Open Api For 2009 Yahoo! Open Hack Day By Sc@20091017
JH Lee
 
PPT
Tw Mail Open Hack Day 2
JH Lee
 
PDF
Y Boss External 20091017
JH Lee
 
PDF
Search Monkey
JH Lee
 
2009 Hackday Taiwan Yui
JH Lee
 
Tw Mail Open Hack Day 1
JH Lee
 
K+ Open Api For 2009 Yahoo! Open Hack Day By Sc@20091017
JH Lee
 
Tw Mail Open Hack Day 2
JH Lee
 
Y Boss External 20091017
JH Lee
 
Search Monkey
JH Lee
 
Ad

More from JH Lee (8)

PPT
Cas Open Api 2009 Hack Day
JH Lee
 
PPT
2009 Tw Oh Wretch Api
JH Lee
 
PPT
Yql V8
JH Lee
 
PPT
Erikeldridge Yos V9
JH Lee
 
PPT
TWOHD_Search Monkey
JH Lee
 
PPT
TWOHD_Wretch API
JH Lee
 
PPT
TWOHD_K+ API
JH Lee
 
PPT
TWOHD_Blueprint
JH Lee
 
Cas Open Api 2009 Hack Day
JH Lee
 
2009 Tw Oh Wretch Api
JH Lee
 
Yql V8
JH Lee
 
Erikeldridge Yos V9
JH Lee
 
TWOHD_Search Monkey
JH Lee
 
TWOHD_Wretch API
JH Lee
 
TWOHD_K+ API
JH Lee
 
TWOHD_Blueprint
JH Lee
 

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"> &nbsp <h2>CAS Demo</h2> <p> <TEXTAREA rows="20" cols="50" name="content"> 肉豬凍冷良優是我哈哈 </TEXTAREA>
  • 2. &nbsp <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 "&nbsp<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."&nbsp;&nbsp;</font>"; if($pos == 'NOUN'){ echo "</u>"; } echo "&nbsp;"; } } } } echo "</td></tr></table>"; } else{ echo "&nbsp<p><h2>Keyword Extraction Result</h2>&nbsp<p>"; foreach($index as $key=>$val){ if($key == "TOKEN"){ foreach($val as $i => $idx){ $score = $XML[$idx+1]['value']; echo $XML[$idx]['value']."&nbsp;Score:$score<br>"; } } } //var_dump($result); } ?> </body> </html>