-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7248a2
commit dde8eb1
Showing
2 changed files
with
327 additions
and
0 deletions.
There are no files selected for viewing
116 changes: 116 additions & 0 deletions
116
Net_Cicindela/tags/release-0.2.0-20090820143402/Net/Cicindela.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
/** | ||
* レコメンドエンジンCicindelaのWebAPIラッパ | ||
* | ||
* PHP version 5 | ||
* | ||
* LICENSE: This source file is subject to version 3.01 of the PHP license | ||
* that is available through the world-wide-web at the following URI: | ||
* http://www.php.net/license/3_01.txt. If you did not receive a copy | ||
* the PHP License and are unable to obtain it through the web, | ||
* send a note to license@php.net so we can mail you a copy immediately. | ||
* | ||
* @category Net | ||
* @package Net_Cicindela | ||
* @author TANAKA Koichi <tanaka@ensites.com> | ||
* @copyright authors | ||
* @license http://www.php.net/license/3_01.txt The PHP License, version 3.01 | ||
* @version $Id$ | ||
* @link http://d.hatena.ne.jp/Mugeso/ | ||
* @see http://code.google.com/p/cicindela2/ | ||
*/ | ||
require_once 'HTTP/Request2.php'; | ||
require_once 'Net/URL2.php'; | ||
require_once 'Cicindela/Dataset.php'; | ||
|
||
class Net_Cicindela | ||
{ | ||
const VERSION = '0.2.1'; | ||
private $baseUrl; | ||
|
||
/** | ||
* | ||
* @var HTTP_Request2 | ||
*/ | ||
private $request; | ||
|
||
public function __construct($baseurl = 'http://localhost/cicindela/', $request = null) | ||
{ | ||
$this->baseUrl = $baseurl; | ||
$this->request = $request instanceof HTTP_Request2 ? $request : new HTTP_Request2(); | ||
$this->request->setHeader('User-Agent', 'Net_Cicindela/' . self::VERSION . | ||
' (http://openpear.org/package/Net_Cicindela) ' . | ||
'PHP/' . phpversion()); | ||
} | ||
|
||
/** | ||
* データセットを取得する | ||
* | ||
* @param string $name データセット名 | ||
* @return Net_Cicindela_Dataset | ||
*/ | ||
public function getDataset($name) | ||
{ | ||
return new Net_Cicindela_Dataset($name, $this); | ||
} | ||
|
||
/** | ||
* ベースURLを取得する | ||
* | ||
* @return string | ||
*/ | ||
public function getBaseUrl() | ||
{ | ||
return $this->baseUrl; | ||
} | ||
|
||
/** | ||
* 情報を記録する | ||
* | ||
* 通常はNet_Cicindela_Datasetから呼び出されるので | ||
* 普通の人は気にしなくてよい。 | ||
* | ||
* @param array $param | ||
*/ | ||
public function record(array $param) | ||
{ | ||
$url = new Net_URL2($this->getBaseUrl() . 'record'); | ||
$url->setOption(Net_URL2::OPTION_SEPARATOR_OUTPUT, '&'); // arg_separator.outputに左右されては困る。 | ||
$url->setQueryVariables($param); | ||
$request = $this->request; | ||
$request->setURL($url); | ||
$response = $request->send(); | ||
|
||
$responseCode = $response->getStatus(); | ||
if($responseCode!==204) { | ||
throw new RuntimeException('Bad response returned.', $responseCode); | ||
} | ||
} | ||
|
||
/** | ||
* レコメンドを取得する | ||
* | ||
* 通常はNet_cicindela_Datasetから呼び出されるので、 | ||
* 普通の人は気にしなくてよい。 | ||
* | ||
* @param array $param | ||
* @return array | ||
*/ | ||
public function getRecommend(array $param) | ||
{ | ||
$url = new Net_URL2($this->getBaseUrl() . 'recommend'); | ||
$url->setOption(Net_URL2::OPTION_SEPARATOR_OUTPUT, '&'); // arg_separator.outputに左右されては困る。 | ||
$url->setQueryVariables($param); | ||
$request = $this->request; | ||
$request->setURL($url); | ||
$response = $request->send(); | ||
|
||
$responseCode = $response->getStatus(); | ||
if($responseCode !== 200) { | ||
throw new RuntimeException('Bad response returned.', $responseCode); | ||
} | ||
|
||
return array_filter(explode("\n", $response->getBody())); | ||
} | ||
} | ||
?> |
211 changes: 211 additions & 0 deletions
211
Net_Cicindela/tags/release-0.2.0-20090820143402/Net/Cicindela/Dataset.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
<?php | ||
/** | ||
* レコメンドエンジンCicindelaのデータセット | ||
* | ||
* PHP version 5 | ||
* | ||
* LICENSE: This source file is subject to version 3.01 of the PHP license | ||
* that is available through the world-wide-web at the following URI: | ||
* http://www.php.net/license/3_01.txt. If you did not receive a copy | ||
* the PHP License and are unable to obtain it through the web, | ||
* send a note to license@php.net so we can mail you a copy immediately. | ||
* | ||
* @category Net | ||
* @package Net_Cicindela | ||
* @author TANAKA Koichi <tanaka@ensites.com> | ||
* @copyright authors | ||
* @license http://www.php.net/license/3_01.txt The PHP License, version 3.01 | ||
* @version $Id$ | ||
* @link http://d.hatena.ne.jp/Mugeso/ | ||
* @see http://code.google.com/p/cicindela2/ | ||
*/ | ||
require_once 'Net/Cicindela.php'; | ||
class Net_Cicindela_Dataset | ||
{ | ||
/** | ||
* | ||
* @var string データセット名 | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* | ||
* @var Net_Cicindela | ||
*/ | ||
protected $cicindela; | ||
|
||
/** | ||
* コンストラクタ | ||
* | ||
* @param string $name データセット名 | ||
* @param mixed $cicindela Net_CicindelaインスタンスかベースURL | ||
*/ | ||
public function __construct($name, $cicindela = 'http://localhost/cicindela/') { | ||
$this->name = $name; | ||
|
||
if($cicindela instanceof Net_Cicindela) { | ||
$this->cicindela = $cicindela; | ||
} else { | ||
$this->cicindela = new Net_Cicindela($cicindela); | ||
} | ||
} | ||
|
||
/** | ||
* アイテムを購入/閲覧 | ||
* | ||
* @param mixed $user_id 主体ユーザID | ||
* @param mixed $item_id 対象アイテムID | ||
*/ | ||
public function pickItem($user_id, $item_id) | ||
{ | ||
return $this->record('insert_pick', compact('user_id', 'item_id')); | ||
} | ||
|
||
/** | ||
* アイテムへ投票 | ||
* | ||
* @param mixed $user_id 主体ユーザID | ||
* @param mixed $item_id 対象アイテムID | ||
* @param int $raging 評価 | ||
*/ | ||
public function vote($user_id, $item_id, $rating) | ||
{ | ||
return $this->record('insert_rating', compact('user_id', 'item_id', 'rating')); | ||
} | ||
|
||
/** | ||
* アイテムにタグ付け | ||
* | ||
* @param mixed $user_id 主体ユーザID | ||
* @param mixed $item_id 対象アイテムID | ||
* @param mixed $tag_id タグのID | ||
*/ | ||
public function tag($user_id, $item_id, $tag_id) | ||
{ | ||
return $this->record('insert_tag', compact('user_id', 'item_id', 'tag_id')); | ||
} | ||
|
||
/** | ||
* アイテムにカテゴリを設定 | ||
* | ||
* @param mixed $item_id 対象アイテムID | ||
* @param mixed $category_id カテゴリID | ||
*/ | ||
public function setCategory($item_id, $category_id) | ||
{ | ||
return $this->record('set_category', compact('item_id', 'category_id')); | ||
} | ||
|
||
/** | ||
* アイテムを購入/閲覧を取り消し | ||
* | ||
* @param mixed $user_id | ||
* @param mixed $item_id | ||
*/ | ||
public function unpickItem($user_id, $item_id) { | ||
return $this->record('delete_pick', compact('user_id', 'item_id')); | ||
} | ||
|
||
/** | ||
* アイテムへの評価を取り消し | ||
* | ||
* @param mixed $user_id | ||
* @param mixed $item_id | ||
*/ | ||
public function unvote($user_id, $item_id) | ||
{ | ||
return $this->record('delete_rating', compact('user_id', 'item_id', 'rating')); | ||
} | ||
|
||
/** | ||
* アイテムのタグを取り消し | ||
* | ||
* @param mixed $user_id | ||
* @param mixed $item_id | ||
* @param mixed $tag_id | ||
*/ | ||
public function untag($user_id, $item_id, $tag_id) | ||
{ | ||
return $this->record('delete_tag', compact('user_id', 'item_id', 'tag_id')); | ||
} | ||
|
||
/** | ||
* カテゴリ付を取り消し | ||
* | ||
* @param mixed $item_id | ||
* @param mixed $category_id | ||
*/ | ||
public function removeCategory($item_id, $category_id) | ||
{ | ||
return $this->record('remove_category', compact('item_id', 'category_id')); | ||
} | ||
|
||
/** | ||
* データ入力を記録する | ||
* | ||
* @param string $op 処理内容 | ||
* @param array $param パラメータ | ||
*/ | ||
protected function record($op, $param) | ||
{ | ||
$param['set'] = $this->name; | ||
$param['op'] = $op; | ||
|
||
return $this->cicindela->record($param); | ||
} | ||
|
||
/** | ||
* アイテムに対するレコメンドを取得する | ||
* | ||
* @param mixed $item_id 対象アイテムID | ||
* @param mixed $limit 取得するレコメンド数 | ||
* @param mixed $categotry カテゴリを絞り込む場合に指定する | ||
* @return array | ||
*/ | ||
public function getRecommendForItem($item_id, $limit = 10, $categotry = null) | ||
{ | ||
return $this->getRecommend('for_item', compact('item_id', 'limit', 'category')); | ||
} | ||
|
||
/** | ||
* ユーザに対するレコメンドを取得する | ||
* | ||
* @param mixed $user_id 対象ユーザID | ||
* @param mixed $limit 取得するレコメンド数 | ||
* @param mixed $categotry カテゴリを絞り込む場合に指定する | ||
* @return array | ||
*/ | ||
public function getRecommendForUser($user_id, $limit = 20, $category = null) | ||
{ | ||
return $this->getRecommend('for_user', compact('user_id', 'limit', 'category')); | ||
} | ||
|
||
/** | ||
* 似ているユーザを取得する | ||
* | ||
* @param mixed $user_id 対象ユーザID | ||
* @param mixed $limit 取得するユーザ数 | ||
* @param mixed $categotry カテゴリを絞り込む場合に指定する | ||
* @return array | ||
*/ | ||
public function getSimilarUsers($user_id, $limit = 20, $category = null) { | ||
return $this->getRecommend('similar_users', compact('user_id', 'limit', 'category')); | ||
} | ||
|
||
/** | ||
* レコメンドを取得する | ||
* | ||
* @param array $op レコメンドの種類 | ||
* @param array $param パラメータ | ||
* | ||
* @return array | ||
*/ | ||
protected function getRecommend($op, $param) | ||
{ | ||
$param['set'] = $this->name; | ||
$param['op'] = $op; | ||
|
||
return $this->cicindela->getRecommend(array_filter($param)); | ||
} | ||
} | ||
?> |