194 lines
No EOL
5.2 KiB
PHP
194 lines
No EOL
5.2 KiB
PHP
<?php
|
|
/**
|
|
* Fonctions utiles au plugin Traitement corpus web
|
|
*
|
|
* @plugin Traitement corpus web
|
|
* @copyright 2021
|
|
* @author gamuza
|
|
* @licence GNU/GPL
|
|
* @package SPIP\Corpus\Fonctions
|
|
*/
|
|
|
|
if (!defined('_ECRIRE_INC_VERSION')) {
|
|
return;
|
|
}
|
|
|
|
function cree_titre($id_video) {
|
|
if ($id_video != intval($id_video)) {
|
|
return false;
|
|
}
|
|
$res = sql_allfetsel('title,description', 'spip_corpus_videos', 'id_corpus_video ='.$id_video);
|
|
$titre = $res[0]['title'];
|
|
$description = $res[0]['description'];
|
|
|
|
if ($titre == '' AND $description != '') {
|
|
$set = ['title' => couper($description, 100)];
|
|
sql_updateq('spip_corpus_videos', $set, 'id_corpus_video='.$id_video);
|
|
}
|
|
|
|
return couper($description, 100);
|
|
}
|
|
|
|
function integre_xml($fichier, $id_video) {
|
|
$stop_words = explode(',', lire_config('corpus_web/stop_words'));
|
|
|
|
$chemin = _DIR_TMP.lire_config('corpus_web/repertoire_transcriptions').'/'.$fichier;
|
|
if ($id_video != intval($id_video)) {
|
|
return 'erreur : absence d\'id_video ou format incorrect ('.$id_video.')';
|
|
}
|
|
|
|
if (!file_exists($chemin) OR $id_video != intval($id_video)) {
|
|
$content = 'fichier XML absent';
|
|
}
|
|
else {
|
|
$content = file_get_contents($chemin);
|
|
preg_match_all('/<Word [^>]*> ([^<]*) /i', $content, $matches, PREG_PATTERN_ORDER);
|
|
$content = array_unique($matches[1]);
|
|
$content = array_diff($content, $stop_words);
|
|
$content = join(',', $content);
|
|
}
|
|
if ($content == '') {
|
|
$content = 'fichier XML vide';
|
|
}
|
|
|
|
$set = ['transcription_detailed_content'=> $content];
|
|
sql_updateq('spip_corpus_videos', $set, 'id_corpus_video='.$id_video);
|
|
if (sql_error()) {
|
|
return sql_error();
|
|
}
|
|
$retour = sql_getfetsel('transcription_detailed_content','spip_corpus_videos' , 'id_corpus_video='.$id_video);
|
|
return $retour;
|
|
}
|
|
|
|
function integre_txt($fichier, $id_video) {
|
|
$chemin = _DIR_TMP.lire_config('corpus_web/repertoire_transcriptions').'/'.$fichier;
|
|
if ($id_video != intval($id_video)) {
|
|
return 'erreur : absence d\'id_video ou format incorrect ('.$id_video.')';
|
|
}
|
|
|
|
if (!file_exists($chemin) OR $id_video != intval($id_video)) {
|
|
$content = 'fichier de transcription absent';
|
|
}
|
|
else {
|
|
$content = nettoyer_timecodes(file_get_contents($chemin));
|
|
}
|
|
if ($content == '') {
|
|
$content = 'fichier de transcription vide';
|
|
}
|
|
|
|
$set = ['transcription_readable_content'=> $content];
|
|
sql_updateq('spip_corpus_videos', $set, 'id_corpus_video='.$id_video);
|
|
if (sql_error()) {
|
|
return sql_error();
|
|
}
|
|
$retour = sql_getfetsel('transcription_readable_content','spip_corpus_videos' , 'id_corpus_video='.$id_video);
|
|
return $retour;
|
|
}
|
|
|
|
function nettoyer_timecodes($content) {
|
|
$content = preg_replace('/\[\d{2}:\d{2}:\d{2} - \d{2}:\d{2}:\d{2}\] /i',' ',$content);
|
|
$content = preg_replace('/\n\n/', '§', $content);
|
|
$content = preg_replace('/\n/', '', $content);
|
|
$content = preg_replace('/§/', "\n\n", $content);
|
|
return $content;
|
|
}
|
|
|
|
function nettoyer_balises($content) {
|
|
$content = preg_replace('/<[a-zA-Z0-9\/=\'" ?:.]*>/i', " ", $content);
|
|
return $content;
|
|
}
|
|
|
|
function nombre_ligne($fichier) {
|
|
if (!file_exists($fichier)) {
|
|
return false;
|
|
}
|
|
$num_ligne = 0;
|
|
$handle = @fopen($fichier, "rb");
|
|
if ($handle) {
|
|
while (($buffer = fgets($handle)) !== false) {
|
|
$num_ligne = $num_ligne + 1;
|
|
}
|
|
}
|
|
return $num_ligne;
|
|
}
|
|
|
|
function affiche_ligne($fichier, $num) {
|
|
if (!file_exists($fichier)) {
|
|
return false;
|
|
}
|
|
|
|
$handle = @fopen($fichier, "rb");
|
|
if ($handle) {
|
|
$num_ligne = 0;
|
|
while (($buffer = fgets($handle)) !== false) {
|
|
$num_ligne = $num_ligne + 1;
|
|
if ($num_ligne == $num) {
|
|
return $buffer;
|
|
}
|
|
}
|
|
if (!feof($handle)) {
|
|
echo "Erreur: fgets() a échoué\n";
|
|
}
|
|
fclose($handle);
|
|
}
|
|
return 'Erreur : nombre de lignes du fichier '.$fichier.' inférieur à '.$num;
|
|
}
|
|
|
|
// TO DO : corriger la récup du contenu des titres (cf site brezhoweb.com)
|
|
function affiche_titraille($html) {
|
|
$niv_h = [1,2,3,4,5,6];
|
|
$titraille = [];
|
|
foreach($niv_h as $h) {
|
|
preg_match_all('/<h'.$h.'(.*)>(.*)?<\/h'.$h.'>/si', $html, $match, PREG_SET_ORDER);
|
|
if ($match) {
|
|
foreach($match as $m) {
|
|
$titraille[] = '<h'.$h.'>'.$m[2].'</h'.$h.'>';
|
|
}
|
|
}
|
|
}
|
|
|
|
return join("\r\n", $titraille);
|
|
}
|
|
|
|
function affiche_title($html) {
|
|
preg_match_all('/<title>(.*)?<\/title>/si', $html, $match, PREG_SET_ORDER);
|
|
if ($match) {
|
|
foreach($match as $m) {
|
|
$titles[] = $m[1];
|
|
}
|
|
}
|
|
return join("\r\n", $titles);
|
|
}
|
|
|
|
function human_filesize($bytes, $decimals = 2) {
|
|
$sz = 'BKMGTP';
|
|
$factor = floor((strlen($bytes) - 1) / 3);
|
|
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
|
|
}
|
|
|
|
function recup_titres($html) {
|
|
// regexp : class="(titre|field-content)
|
|
// class="(titre|field-content)">(<[^>]*>)?([^<>]*)
|
|
$titres = [];
|
|
$html = str_replace(['<em>','</em>'], '', $html);
|
|
preg_match_all('/class="(titre|field-content|nodeTitle nobreak)">(<[^>]*>)?([^<>]*)/si', $html, $match, PREG_SET_ORDER);
|
|
if ($match) {
|
|
foreach($match as $m) {
|
|
if ($m[3] !== '') {
|
|
$titres[] = $m[3];
|
|
}
|
|
}
|
|
}
|
|
|
|
return count($titres) ? "<ul><li>".join("</li><li>", $titres)."</li></ul>" : '';
|
|
}
|
|
|
|
function recup_site($nom_fichier_jsonl) {
|
|
return explode('_', $nom_fichier_jsonl)[0];
|
|
}
|
|
|
|
function recup_aspi($nom_fichier_jsonl) {
|
|
$T = explode('_', $nom_fichier_jsonl)[1];
|
|
$T = explode('-', $T);
|
|
return $T[0].'-'.$T[1].'-'.$T[2];
|
|
} |