info-com / econtext-sdk-php
用于与eContext API交互的PHP客户端
Requires
- dev-master
- 0.1.12
- 0.1.11
- 0.1.10
- 0.1.9
- 0.1.8
- 0.1.7
- 0.1.6
- 0.1.5
- 0.1.4
- 0.1.3
- 0.1.2
- 0.1.1
- 0.1.0
- dev-dependabot/pip/doc/wheel-0.38.1
- dev-dependabot/pip/doc/html5lib-0.999999999
- dev-dependabot/pip/doc/babel-2.9.1
- dev-dependabot/pip/doc/pygments-2.7.4
- dev-dependabot/pip/doc/pyyaml-5.4
- dev-dependabot/pip/doc/jinja2-2.11.3
This package is not auto-updated.
Last update: 2024-09-14 19:09:35 UTC
README
一个简单的PHP客户端,用于公开eContext API。
安装
我们推荐使用Composer将eContext库包含到您的项目中。在您的项目composer.json文件中,您可以简单地包含econtext-sdk-php,Composer将为您下载并包含最新版本。
{
"require": {
"info-com/econtext-sdk-php": "0.1.*"
}
}
基本用法
$posts = [
"HAPPY PI DAY 3.141592653589793238462643383279502884197169399375...",
"Happy birthday, MIT! The Institute was founded April 10, 1861 by William Barton Rogers. #tbt",
"Consciousness is a state of matter",
"Flying car by @MITAeroAstro spinoff @Terrafugia moves from science fiction to reality",
"Amazing Time-Lapse Video Shows Evolution of #Universe Like Never Before",
"MIT alum @JeopardyJulia now trails only @kenjennings for all-time #Jeopardy! wins",
"Happy b-day Nikola #Tesla! Startup @WiTricity is bringing his ideas on wireless power to life",
"Seen at the Student Center this afternoon: Tetris hash browns!",
"Researchers at @eapsMIT say a large earthquake may occur 5 miles from Istanbul",
"MIT's robotic cheetah can now run and jump untethered",
"Spacesuit from @MITAeroAstro shrink-wraps to astronauts' bodies",
"This social post should be flagged for fireworks and gambling references"
];
$client = new eContext\Client($username, $password);
$classify = new Social($client);
$classify->setData($posts);
$classify->setParameter('classification_type', 1);
$result = $classify->classify();
foreach($result->yieldResults() as $mapping) {
foreach($mapping['scored_categories'] as $cat_info) {
$cid = $cat_info['category_id'];
echo " * {$result->getCategory($cid)['name']}".PHP_EOL;
}
}
eContext\Client
封装GuzzleHttp对象的基客户端对象。它应该传递给传递给API的每个调用。
参数
- $username API用户名
- $password API密码
- $baseuri API的基本URI。默认情况下,此值设置为http://api.econtext.com
- $statusCallback 一个回调,在每次API交互之后被调用。
statusCallback可以在运行期间交互。回调函数必须接收两个参数。第一个是与该调用序列ID对应的索引号(例如 $data[4])。第二个是GuzzleHttp响应对象(如果API调用成功或失败)。
eContext\Classify\Type\Social
一个与社会分类端点交互的社会分类对象。社会对象的数据对象应该是一组社会内容短语(例如 ["这是我想分类的第一条推文", "这是我想分类的第二条推文", ...]
),可以包含您想要的任意多个元素。这个列表将被拆分并发送到API,这些调用可以通过在$social->classify()调用中设置concurrency
参数来并行化。例如,如果您有10,000个元素要分类,并且将并发设置为5,那么您应该能够同时运行5组调用,每次发送1000个元素,因此是5000个并发。
当调用完成时,您可以使用从classify()调用返回的对象按提交顺序检索它们。
您还可以向分类调用传递额外的参数。例如,为了指定仅使用基于规则的eContext分类,您可以调用 $classify->setParameter('classification_type', 1)
;
eContext\Classify\Result
eContext分类对象的基结果对象。每次在yieldResults()调用中产生结果时,都会创建一个Result对象,并填充多个项目。不同的分类类型扩展了此对象,但基对象仍然被创建并填充了至少 categories
、translate
、overlay
和 inner
参数。可以通过 $result->getCategory($id)
调用检索单个类别,并将返回一个关联的类别数组。《inner》参数包含API调用的全部内部内容,可以用于手动探索结果对象。
eContext\Classify\Results\Social
社交分类的结果对象将在所有输入数据上生成结果。如果您在$classify
对象的数据中输入了10,000条推文,yieldResults应该生成10,000个结果数组。这些数组中的每一个都将包含一个与API结果内部内容关联的数组。例如,当您从第1000篇帖子切换到第1001篇帖子时,加载新页面时,对象的分类数组也会相应改变,以加载该页面的新分类。这意味着第一页上存在的分类可能不会出现在第二页上。
除了存储在通用结果对象中的categories
、inner
、translate
和overlay
参数外,社交结果对象还将包含一个与yieldResults调用中生成的项目相对应的results
对象。
eContext\Classify\Results\Html 和 eContext\Classify\Results\Url
HTML和URL分类对象的结果对象。传递给URL或HTML分类对象的数据对象可能包含多个元素。例如,您可以一次性用来自几个不同页面的HTML内容填充$html_classify
对象,或者一次性用几个URL填充。
例如
$url = new eContext\Classify\Type\Url($client);
$urls = ['http://www.cnn.com', 'http://www.econtext.ai', 'http://www.nytimes.com'];
$url->setData($urls);
$results = $url->classify(3); # classify all pages at once
$i = 0;
foreach($results->yieldResults() as $url_result) {
echo $urls[$i++] . PHP_EOL;
echo $url_result['title'] . PHP_EOL;
echo "Categories:" . PHP_EOL;
foreach($url_result['scored_categories'] as $scored_category) {
echo " " . $results->getCategory($scored_category['category_id'])['name'] . " : " . $scored_category['score'] . PHP_EOL
}
}
上述调用的输出应如下所示
http://www.cnn.com
CNN - Breaking News, Latest News and Videos
Categories:
Newsworthy Topics : 0.75
Health : 0.125
Arts & Entertainment : 0.125
http://www.econtext.ai
eContext | The Web's Deepest Text Classification System
Categories:
eContext : 0.90909090909091
Business & Industrial : 0.090909090909091
http://www.nytimes.com
The New York Times - Breaking News, World News & Multimedia
Categories:
The New York Times : 0.23255813953488
House of Representatives : 0.18604651162791
Republican Party : 0.093023255813953
Palliative & End of Life Care : 0.093023255813953
Paul Ryan : 0.046511627906977
Newsworthy Topics : 0.046511627906977
Sports Movies : 0.046511627906977
Senates : 0.046511627906977
Physicians : 0.046511627906977
International News : 0.046511627906977
Art Museums & Sculpture Gardens : 0.046511627906977
Republicans in Congress : 0.046511627906977
United States Armed Forces : 0.023255813953488
注意随着迭代器的每次前进,$results->getCategories()对象是如何变化的,并返回与当前页面(当前url_result)关联的分类。
每个$url_result对象包含三项。
- 'title' -- 与从HTML/URL页面检索到的标题元素相对应
- 'scored_categories' -- 一系列得分的分类对象列表,每个对象都包含一个
category_id
和一个score
。 - 'scored_keywords' -- 一系列在页面中具有价值的关键词