curl 来自CSV文件的描述的PHP情绪评分

vfhzx4xs  于 2022-11-13  发布在  PHP
关注(0)|答案(2)|浏览(145)

我试图从CSV文件中获取随机产品描述的情绪得分,我面临着一个问题,我认为是API响应时间,我不确定我是否使用API不正确/低效地遍历CSV,但要获得所有300多个CSV的结果需要很长时间CSV中的条目,每当我想将新的更改推送到我的代码库时,我都需要等待API每次重新评估条目,下面是我为加载CSV文件和获取情绪评分而编写的代码

<?php

set_time_limit(500); // extended timeout due to slow / overwhelmed API response

function extract_file($csv) { // CSV to array function

    $file = fopen($csv, 'r');

    while (!feof($file)) {
        $lines[] = fgetcsv($file, 1000, ',');
    }

    fclose($file);
    return $lines;

}

$the_file = 'dataset.csv';
$csv_data = extract_file($the_file);


$response_array = []; // array container to hold returned sentiment values from among prduct descriptions

for($x = 1; $x < count($csv_data) - 1; $x++) { // loop through all descriptions
    echo $x; // show iteration
    $api_text = $csv_data[$x][1];
    $api_text = str_replace('&', ' and ', $api_text); // removing escape sequence characters, '&' breaks the api :)
    $api_text = str_replace(" ", "%20", $api_text); // serializing string
    $text = 'text=';
    $text .=$api_text; // serializing string further for the API
    //echo 'current text1: ', $api_text;
    $curl = curl_init(); // API request init

    curl_setopt_array($curl, [
        CURLOPT_URL => "https://text-sentiment.p.rapidapi.com/analyze",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $text,
        CURLOPT_HTTPHEADER => [
            "X-RapidAPI-Host: text-sentiment.p.rapidapi.com",
            "X-RapidAPI-Key: <snip>",
            "content-type: application/x-www-form-urlencoded"
        ],
    ]);

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }

    $json = json_decode($response, true); // convert response to JSON format
    
    if(isset($json["pos"]) == false) { // catching response error 100, makes array faulty otherwise
        continue;
    }
    else {
        array_push($response_array, array($x, "+" => $json["pos"], "-" => $json["neg"])); // appends array with sentiment values at current index
    }
    
}

echo "<br>";
echo "<br> results: ";

echo "<p>";
for ($y = 0; $y < count($response_array); $y++){ // prints out all the sentiment values
    echo "<br>";
    echo print_r($response_array[$y]);
    echo "<br>";
}
echo "</p>";

echo "<br>the most negative description: ";
$max_neg = array_keys($response_array, max(array_column($response_array, '-')));
//$max_neg = max(array_column($response_array, '-'));
echo print_r($csv_data[$max_neg[0]]);

echo "<br>the most positive description: ";
$max_pos = array_keys($response_array, max(array_column($response_array, '+')));
echo print_r($csv_data[$max_pos[0]]);

?>

这段代码片段的目的是在csv的描述列中找到最消极和最积极的情绪,并根据它们的索引将它们打印出来,我只对找到积极和消极情绪字数最多的描述感兴趣,而不是总体情绪的百分比
该文件可以在此git repo中找到
谢谢你的建议

8fq7wneg

8fq7wneg1#

这可以通过创建缓存文件来实现。
此解决方案使用产品名称作为每个条目的键,创建一个包含API结果的文件cache.json
在后续调用中,它将使用该高速缓存值(如果存在)。

set_time_limit(500);

function file_put_json($file, $data)
{
    $json = json_encode($data, JSON_PRETTY_PRINT);
    file_put_contents($file, $json);
}

function file_get_json($file, $as_array=false)
{
    return json_decode(file_get_contents($file), $as_array);
}

function file_get_csv($file, $header_row=true)
{
    $handle = fopen($file, 'r');
    
    if ($header_row === true)
        $header = fgetcsv($handle);

    $array = [];
    while ($row = fgetcsv($handle)) {
        if ($header_row === true) {
            $array[] = array_combine($header, array_map('trim', $row));
        } else {
            $array[] = array_map('trim', $row);
        }
    }
    fclose($handle);
    return $array;
}

function call_sentiment_api($input)
{
    $text = 'text=' . $input;
    $curl = curl_init();

    curl_setopt_array($curl, [
        CURLOPT_URL => "https://text-sentiment.p.rapidapi.com/analyze",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $text,
        CURLOPT_HTTPHEADER => [
            "X-RapidAPI-Host: text-sentiment.p.rapidapi.com",
            "X-RapidAPI-Key: <snip>",
            "content-type: application/x-www-form-urlencoded"
        ],
    ]);

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        throw new Exception("cURL Error #:" . $err);
    }

    return $response;
}

$csv_data = file_get_csv('dataset.csv');

if (file_exists('cache.json')) {
    $cache_data = file_get_json('cache.json', true);
} else {
    $cache_data = [];
}

$cache_names = array_keys($cache_data);

$output = [];

foreach ($csv_data as $csv) {
    $product_name = $csv['name'];
    echo $product_name . '...';

    if (in_array($product_name, $cache_names)) {
        echo 'CACHED...' . PHP_EOL;

        continue;
    }

    $description = urlencode(str_replace('&', ' and ', $csv['description']));

    $response = call_sentiment_api($description);
    
    echo 'API...' . PHP_EOL;

    $json = json_decode($response, true);

    $cache_data[$product_name] = $json;
}

file_put_json('cache.json', $cache_data);

echo 'SAVE CACHE!' . PHP_EOL . PHP_EOL;

$highest_pos = 0;
$highest_neg = 0;

$pos = [];
$neg = [];

foreach ($cache_data as $name => $cache) {
    if (!isset($cache['pos']) || !isset($cache['neg'])) {
        continue;
    }
    if ($cache['pos'] > $highest_pos) {
        $pos = [$name => $cache];
        $highest_pos = $cache['pos'];
    }
    if ($cache['pos'] === $highest_pos) {
        $pos[$name] = $cache;
    }
    if ($cache['neg'] > $highest_neg) {
        $neg = [$name => $cache];
        $highest_neg = $cache['neg'];
    }
    if ($cache['neg'] === $highest_neg) {
        $neg[$name] = $cache;
    }
}

echo "Most Positive Sentiment: " . $highest_pos . PHP_EOL;
foreach ($pos as $name => $pos_) {
    echo "\t" . $name . PHP_EOL;
}
echo PHP_EOL;

echo "Most Negative Sentiment: " . $highest_neg . PHP_EOL;
foreach ($neg as $name => $neg_) {
    echo "\t" . $name . PHP_EOL;
}

结果:

Most Positive Sentiment: 4
        X-Grip Lifting Straps - GymBeam
        Beta Carotene - GymBeam
        Chelated Magnesium - GymBeam
        Creatine Crea7in - GymBeam
        L-carnitine 1000 mg - GymBeam - 20 tabs
        Resistance Band Set - GymBeam

Most Negative Sentiment: 2
        Calorie free Ketchup sauce 320 ml - GymBeam
        ReHydrate Hypotonic Drink 1000 ml - GymBeam
        Vitamin E 60 caps - GymBeam
        Vitamin B-Complex 120 tab - GymBeam
        Zero Syrup Hazelnut Choco 350 ml - GymBeam
        Bio Psyllium - GymBeam
        Zero calorie Vanilla Syrup - GymBeam
wxclj1h5

wxclj1h52#

"你需要知道时间的流逝"
首先确定curl请求中的时间位置。
我猜是API响应时间。
如果是这样的话,我有一个解决方案。同时,我将得到“多任务”代码代码,我用它来做同时curl请求。
curl具有您需要的定时。它看起来像这样:

'total_time' => 0.029867,
  'namelookup_time' => 0.000864,
  'connect_time' => 0.001659,
  'pretransfer_time' => 0.00988,
  'size_upload' => 0.0,
  'size_download' => 8300.0,
  'speed_download' => 277898.0,
  'speed_upload' => 0.0,

只需添加几行代码

$response = curl_exec($curl);
$info = var_export(curl_getinfo($curl),true);
file_put_contents('timing.txt',$info,FILE_APPEND);

同时运行curl套接字。

把你的卷发放到curl.php里

$text = $_GET['text'];
  curl_setopt_array($curl, [
        CURLOPT_URL => "https://text-sentiment.p.rapidapi.com/analyze",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $text,
        CURLOPT_HTTPHEADER => [
            "X-RapidAPI-Host: text-sentiment.p.rapidapi.com",
            "X-RapidAPI-Key: <snip>",
            "content-type: application/x-www-form-urlencoded"
        ],
    ]);

这段代码进入CSV循环,创建所有URL查询字段并传递给curl.php(例如http://127.0.0.1/curl.php?text=$text

$query = urlencode($text);
$urls[] = array('host' => "127.0.0.1",'path' => "/curl.php?text=$query

然后处理所有URL。

foreach($urls as $path){
    $host = $path['host'];
    $path = $path['path'];
    $http = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";
    $stream = stream_socket_client("$host:80", $errno,$errstr, 120,STREAM_CLIENT_ASYNC_CONNECT|STREAM_CLIENT_CONNECT); 
    if ($stream) {
      $sockets[] = $stream;  // supports multiple sockets
      fwrite($stream, $http);
    }
    else { 
      $err .=  "$id Failed<br>\n";
    }
  }

然后监视套接字并检索每个套接字的响应。
然后关闭插座,直到您拥有所有这些。

while (count($sockets)) {
  $read = $sockets; 
  stream_select($read, $write = NULL, $except = NULL, $timeout);
  if (count($read)) {
    foreach ($read as $r) { 
      $id = array_search($r, $sockets); 
      $data = fread($r, $buffer_size); 
      if (strlen($data) == 0) { 
     //   echo "$id Closed: " . date('h:i:s') . "\n\n\n";
        $closed[$id] = microtime(true);
        fclose($r); 
        unset($sockets[$id]);
      } 
      else {
        $results[$id] .= $data; 
      }
    }
  }
  else { 
 //   echo 'Timeout: ' . date('h:i:s') . "\n\n\n";
    break;
  }
}

所有结果都以$results[]为单位。

相关问题