349 lines
11 KiB
PHP
349 lines
11 KiB
PHP
<?php
|
|
$crmn = microtime(true);
|
|
|
|
if (!empty($_SERVER["argv"][1])){
|
|
$argument = $_SERVER["argv"][1];
|
|
$consulta = $_SERVER["argv"][2];
|
|
} else {
|
|
// Presenta l'ajuda
|
|
echo "NO HE REBUT la variable\n";
|
|
echo "Cal el directori on s'executa el bot_plus.\n";
|
|
exit;
|
|
}
|
|
$lloc = $argument;
|
|
|
|
// ara pot carregar la configuració i el logger
|
|
include("".$lloc."/configuracio/bot_plus_cfg.php");
|
|
include("".$lloc."/utils/bot_plus_funcions.php");
|
|
require_once("".$lloc."/lib/logger.php");
|
|
require_once($lloc."/utils/vadersentiment/vadersentiment_cat.php");
|
|
|
|
// inicia el log
|
|
$log = new Logging();
|
|
$log->lfile("".$log_fitxer."");
|
|
|
|
$fitxer = $lloc."/fitxers/mencions.json";
|
|
//echo $fitxer;
|
|
|
|
//matriu a partir del fitxer de les mencions
|
|
$json_dec = json_decode(file_get_contents($fitxer), true);
|
|
|
|
//echo "matriu fitxer\n";
|
|
//print_r($json_dec);
|
|
//exit;
|
|
//echo is_array($json_dec)."\n";
|
|
//sleep(2);
|
|
$reg_arx = count($json_dec);
|
|
|
|
if($reg_arx<>null) {
|
|
echo "L'arxiu json de les mencions valorades amb les emocions té ".$reg_arx." ".(($reg_arx<>1)?"registres":"registre")."\n";
|
|
} else {
|
|
print "L'arxiu json de les mencions és buit.\n";
|
|
exit;
|
|
}
|
|
|
|
$dades_url = ["mention"];
|
|
//http_build_query_for_curl($dades_url, $limita_enq);
|
|
|
|
if($consulta!=0){
|
|
echo "Faig consulta\n";
|
|
goto consulta;
|
|
}
|
|
|
|
//echo "cal programar-lo correcetament per registrar les puntuacions de vadersentiment";
|
|
//exit;
|
|
// consulta els existents per afegir-los a la matriu existent si no hi son
|
|
//$menc_mat_nova = ConsultaNotificacions($token,$base_url,$limit_m,$headers);
|
|
$limit_m = 80;
|
|
$imp_mat_nova = ConsultaNotificacions($token,$base_url,$limit_m,$headers);
|
|
//print_r($imp_mat_nova);
|
|
//exit;
|
|
|
|
$nom_mat = count($imp_mat_nova);
|
|
echo "A la instància he repassat les darreres ".$nom_mat." ".(($nom_mat<>1)?"notificacions":"notificació")."\n";
|
|
// fer matriu de mencions noves
|
|
|
|
$menc_mat = array();
|
|
if($nom_mat>=1) {
|
|
|
|
/**
|
|
// llegir fitxer de temps
|
|
$temps_imp = strtotime("now");
|
|
$espera_ob = fopen($lloc . "/fitxers/impulsos_temps.txt", "r") or die("No puc obrir la darrera data!");
|
|
$espera_ll = fread($espera_ob,filesize($lloc . "/fitxers/impulsos_temps.txt"));
|
|
$espera_imp = $espera_ll;
|
|
fclose($espera_ob);
|
|
|
|
$temps_ini = $espera_imp - 604800;
|
|
**/
|
|
|
|
$mc = 0;
|
|
for($f=0;$f<$nom_mat;$f++) {
|
|
$men_id = $imp_mat_nova[$f]['id'];
|
|
$men_tip = $imp_mat_nova[$f]['type'];
|
|
$men_qui = $imp_mat_nova[$f]['account']['acct'];
|
|
$men_qua = $imp_mat_nova[$f]['created_at'];
|
|
// $men_mid = $imp_mat_nova[$f]['status']['id'];
|
|
if( ($men_tip == "mention") ){
|
|
$men_mid = $imp_mat_nova[$f]['status']['id'];
|
|
$mstg = html_entity_decode(strip_tags($imp_mat_nova[$f]['status']['content']));
|
|
} else { $men_mid = 0; $mstg = null; }
|
|
if ( $men_tip == "mention" ) {
|
|
$men_mid = $imp_mat_nova[$f]['status']['id'];
|
|
$menc_mat[] = array ( 'id' => (int)$men_id, 'mis_id' => (int)$men_mid, 'qui' => $men_qui, 'mencio' => $men_tip, 'quan' => $men_qua, 'que' => $mstg );
|
|
$mc++;
|
|
// }
|
|
// sleep(2);
|
|
}
|
|
/** else {
|
|
// $menc_mat[] = array ( );
|
|
$menc_mat[] = array ( 'id' => (int)0, 'mis_id' => (int)0, 'qui' => null, 'mencio' => null, 'quan' => null );
|
|
}
|
|
**/
|
|
}
|
|
|
|
//print_r($menc_mat);
|
|
//exit;
|
|
|
|
$comprova_mat = is_array($menc_mat);
|
|
echo "comprova: ".$comprova_mat."\n";
|
|
//echo "menc_mat_f\n";
|
|
//print_r($menc_mat);
|
|
//sleep(2);
|
|
//exit;
|
|
|
|
// evita repetir
|
|
$fins = count($menc_mat);
|
|
//if( ($mc<0) ){
|
|
echo "De les ".$nom_mat." notificacions, ".$mc." ".(($mc<>1)?"són":"és")." un mencio.\n";
|
|
$cont_fit = 0;
|
|
//echo $fins."\n";
|
|
|
|
for($g=0;$g<$fins;$g++) {
|
|
$id_busc = $menc_mat[$g]['id'];
|
|
$qu_busc = $menc_mat[$g]['qui'];
|
|
$tp_busc = $menc_mat[$g]['mencio'];
|
|
// echo "id_busc: ".$id_busc."\n";
|
|
$troba = array_search($id_busc, array_column($json_dec, 'id'));
|
|
// echo "troba: ".$troba."\n";
|
|
|
|
if( ($troba !== false ) ) {
|
|
// echo "El ".$tp_busc." ".$id_busc." de ".$qu_busc." ja està al registre\n";
|
|
} else {
|
|
if($id_busc<>0) {
|
|
echo "NOVA MENCIO: ".$tp_busc." ".$id_busc." de ".$qu_busc."\n";
|
|
array_push($json_dec,
|
|
['id' => (int)$menc_mat[$g]['id'], 'mis_id' => (int)$menc_mat[$g]['mis_id'],
|
|
'mencio' => $menc_mat[$g]['mencio'], 'qui' => $menc_mat[$g]['qui'],
|
|
'quan' => $menc_mat[$g]['quan'], 'que'=> $menc_mat[$g]['que']
|
|
]);
|
|
}
|
|
$cont_fit++;
|
|
}
|
|
|
|
}
|
|
|
|
//echo "matriu després de push\n";
|
|
//print_r($json_dec);
|
|
|
|
if($cont_fit >= 1) {
|
|
// actualitza el fitxer json de les mencions
|
|
// $fes_json = json_encode($json_dec, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_OBJECT_AS_ARRAY|JSON_PRETTY_PRINT);
|
|
$fes_json = json_encode($json_dec, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
|
|
$escriu_f_json = fopen($lloc."/fitxers/mencions.json", "w") or die("No puc escriure al registre!");
|
|
fwrite($escriu_f_json, $fes_json);
|
|
fclose($escriu_f_json);
|
|
} else {
|
|
echo "No hi ha mencions noves a la instància.\n";
|
|
}
|
|
|
|
//fin if matriu de notificacions té registres
|
|
}
|
|
|
|
echo "Revise els del fitxer txt\n";
|
|
// fer matriu de les del fitxer txt i afegir-les si no hi son.
|
|
$mat_em_txt = exec("php ".$lloc."/fitxers/mencions.txt ".$lloc, $eixida_met, $resposta_met);
|
|
|
|
//print_r($eixida_met);
|
|
|
|
$fins_met = count($eixida_met);
|
|
|
|
for($f=0;$f<$fins_met;$f++) {
|
|
|
|
$regs[] = explode(":::",$eixida_met[$f]);
|
|
// echo "".$f."\n";
|
|
// print_r($regs[$f]);
|
|
$regs_mat[] = $regs[$f];
|
|
|
|
}
|
|
//print_r($regs_mat);
|
|
|
|
$fins_nous = count($regs_mat);
|
|
//exit;
|
|
$cont_nous = 0;
|
|
for($g=0;$g<$fins_nous;$g++) {
|
|
|
|
$id_busc = $regs_mat[$g][0];
|
|
$qu_busc = $regs_mat[$g][2];
|
|
$tp_busc = $regs_mat[$g][3];
|
|
// echo "id_busc: ".$id_busc."\n";
|
|
$troba = array_search($id_busc, array_column($json_dec, 'id'));
|
|
// echo "troba: ".$troba."\n";
|
|
|
|
if( ($troba !== false ) ) {
|
|
// echo "El ".$tp_busc." ".$id_busc." de ".$qu_busc." ja està al registre\n";
|
|
} else {
|
|
if($id_busc<>0) {
|
|
echo "NOVA MENCIO: ".$tp_busc." ".$id_busc." de ".$qu_busc."\n";
|
|
array_push($json_dec,
|
|
['id' => (int)$regs_mat[$g]['id'], 'mis_id' => (int)$regs_mat[$g]['mis_id'],
|
|
'mencio' => $regs_mat[$g]['mencio'], 'qui' => $regs_mat[$g]['qui'],
|
|
'quan' => $regs_mat[$g]['quan'], 'que'=> $regs_mat[$g]['que']
|
|
]);
|
|
}
|
|
$cont_nous++;
|
|
}
|
|
|
|
}
|
|
echo "Hi han ".$cont_nous." registres nous des del fitxer txt.\n";
|
|
// actualitzar el fitxer de mencions json
|
|
if($cont_nous >= 1) {
|
|
// actualitza el fitxer json de les mencions
|
|
// $fes_json = json_encode($json_dec, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_OBJECT_AS_ARRAY|JSON_PRETTY_PRINT);
|
|
$fes_json = json_encode($json_dec, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
|
|
$escriu_f_json = fopen($lloc."/fitxers/mencions.json", "w") or die("No puc escriure al registre!");
|
|
fwrite($escriu_f_json, $fes_json);
|
|
fclose($escriu_f_json);
|
|
} else {
|
|
echo "No hi ha mencions noves al fitxer txt.\n";
|
|
}
|
|
|
|
// enviar els tuts a vadersentiment
|
|
echo "envie els textos del fitxer json actualitzat un a un a vedersentiment per a que els analitze.\n";
|
|
|
|
$fins_json = count($json_dec);
|
|
$cont_regs = 0;
|
|
for($h=0;$h<$fins_json;$h++) {
|
|
|
|
$entrada = $json_dec[$h]['que'];
|
|
|
|
$textToTest = $entrada;
|
|
$sentimenter = new SentimentIntensityAnalyzer();
|
|
$result = $sentimenter->getSentiment($textToTest);
|
|
//print_r($result);
|
|
|
|
//print_r($sentimenter);
|
|
$posi = $result['pos'];
|
|
$neut = $result['neu'];
|
|
$nega = $result['neg'];
|
|
$anim = $result["compound"];
|
|
//echo "text: ".$entrada."\nanalitzat: ".$anim." - pos: ".$posi." - neu: ".$neut." - neg: ".$nega."\n";
|
|
//usleep(200000);
|
|
$mat_em_json[] = array( 'id' => $json_dec[$h]['id'],
|
|
'mis_id' => $json_dec[$h]['mis_id'],
|
|
'qui' => $json_dec[$h]['qui'],
|
|
'quan' => $json_dec[$h]['quan'],
|
|
'mencio' => $json_dec[$h]['mencio'],
|
|
'que' => $json_dec[$h]['que'],
|
|
'pos' => $posi,
|
|
'neu' => $neut,
|
|
'neg' => $nega,
|
|
'tot' => $anim );
|
|
$cont_regs++;
|
|
}
|
|
|
|
//print_r($mat_em_json);
|
|
|
|
// fer fitxer json de les valoracions
|
|
if($cont_regs >= 1) {
|
|
// actualitza el fitxer json de les mencions
|
|
// $fes_json = json_encode($mat_em_json, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_OBJECT_AS_ARRAY|JSON_PRETTY_PRINT);
|
|
$fes_json = json_encode($mat_em_json, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
|
|
$escriu_f_json = fopen($lloc."/fitxers/mencions_em.json", "w") or die("No puc escriure al registre!");
|
|
fwrite($escriu_f_json, $fes_json);
|
|
fclose($escriu_f_json);
|
|
|
|
echo "He escrit ".$cont_regs." registres al fitxer json de les emocions.\n";
|
|
} else {
|
|
echo "No hi ha mencions noves a la instància.\n";
|
|
}
|
|
|
|
consulta:
|
|
|
|
// per a fer els repas linia a linia del fitxer de les emocions
|
|
$fitxer_em = $lloc."/fitxers/mencions_em.json";
|
|
$json_dec_em = json_decode(file_get_contents($fitxer_em), true);
|
|
|
|
if($json_dec_em <> null) {
|
|
$maximj = count($json_dec_em);
|
|
$e=0;
|
|
}else{
|
|
print "L'arxiu json del registre de les emocions està buit.\n";
|
|
exit;
|
|
}
|
|
|
|
//echo "ARRAY EMOCIONS INCOMPLET";
|
|
//print_r($json_dec_em);
|
|
|
|
//$ara = strtotime("today");
|
|
//$ara = strtotime(filemtime($fitxer));
|
|
$ara = (filemtime($fitxer_em));
|
|
//echo $ara."\n";
|
|
|
|
echo "REPASE ".(($maximj<>1)?"ELS ".$maximj." REGISTRES":"EL REGISTRE").".\n";
|
|
|
|
for($e=0;$e<$maximj;$e++) {
|
|
$men_id = $json_dec_em[$e]['id'];
|
|
$men_mi = $json_dec_em[$e]['mis_id'];
|
|
$men_qi = $json_dec_em[$e]['qui'];
|
|
$men_qa = $json_dec_em[$e]['quan'];
|
|
$men_tp = $json_dec_em[$e]['mencio'];
|
|
$men_ms = $json_dec_em[$e]['que'];
|
|
$men_ps = $json_dec_em[$e]['pos'];
|
|
$men_ne = $json_dec_em[$e]['neu'];
|
|
$men_ng = $json_dec_em[$e]['neg'];
|
|
$men_vs = $json_dec_em[$e]['tot'];
|
|
if( ($men_vs >= -1.0000)&&($men_vs <= -0.8000) ) { $valoracio = "molt contrari o crític"; }
|
|
elseif( ($men_vs >= -0.7999)&&($men_vs <= -0.6000) ) { $valoracio = "prou negativa"; }
|
|
elseif( ($men_vs >= -0.5999)&&($men_vs <= -0.5000) ) { $valoracio = "negativa"; }
|
|
elseif( ($men_vs >= -0.4999)&&($men_vs <= -0.4000) ) { $valoracio = "lleugera critica"; }
|
|
elseif( ($men_vs >= -0.3999)&&($men_vs <= -0.2000) ) { $valoracio = "neutra amb lleugera critica"; }
|
|
elseif( ($men_vs >= -0.1999)&&($men_vs <= 0.1999) ) { $valoracio = "neutra"; }
|
|
elseif( ($men_vs >= 0.2000)&&($men_vs <= 0.3999) ) { $valoracio = "neutra amb lleuger suport"; }
|
|
elseif( ($men_vs >= 0.4000)&&($men_vs <= 0.4999) ) { $valoracio = "un poc a favor"; }
|
|
elseif( ($men_vs >= 0.5000)&&($men_vs <= 0.5999) ) { $valoracio = "a favor"; }
|
|
elseif( ($men_vs >= 0.6000)&&($men_vs <= 0.7999) ) { $valoracio = "prou partidari"; }
|
|
elseif( ($men_vs >= 0.8000)&&($men_vs <= 1.000) ) { $valoracio = "molt partidari"; }
|
|
else { $valoracio = "incomprensible"; }
|
|
|
|
|
|
echo "nº".$e." -> ".$men_id." | ".$men_mi. " | ".$men_tp." \t- ".utf8_decode($men_qi)." \t-> ".$men_qa."\n".
|
|
$men_ms."\npos: \e[96m".$men_ps."\e[0m neu: \e[96m".$men_ne."\e[0m neg: \e[96m".$men_ng."\e[0m total = \e[96m".$men_vs."\e[0m \e[1m| ".$valoracio." |\e[0m\n";
|
|
|
|
// pausa d'un segon
|
|
//sleep(1);
|
|
//pausa de mig segon (amb microsegons => unitat més sis ceros == segons );
|
|
//usleep(200000);
|
|
}
|
|
|
|
retalla:
|
|
|
|
$maximj = count($json_dec_em);
|
|
$e=0;
|
|
|
|
if($maximj > $max_linies) {
|
|
$json_dec_e = array_splice($json_dec_em,($maximj-$max_linies),$maximj);
|
|
//print_r($json_dec);
|
|
|
|
// actualitza el fitxer json de les emocions
|
|
//$fes_json = json_encode($json_dec, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_OBJECT_AS_ARRAY|JSON_PRETTY_PRINT);
|
|
$fes_json = json_encode($json_dec_e, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
|
|
$escriu_f_json = fopen($lloc."/fitxers/mencions_em.json", "w") or die("No puc escriure al registre!");
|
|
fwrite($escriu_f_json, $fes_json);
|
|
fclose($escriu_f_json);
|
|
}
|
|
$crmnf = microtime(true);
|
|
$tmf = $crmnf - $crmn;
|
|
echo "Temps emocions de les emocions de les mencions: ".round($tmf,4)."";
|
|
//$log->lwrite("Temps mencions: ".round($tmf,4)."");
|
|
?>
|