TRTFILE


Récupère tous les mots d'une page HTML afin de les écrire dans un fichier BdeD indexé pour une recherche ultérieure.




|
     H DFTACTGRP(*NO) ACTGRP(*NEW) BNDDIR('QC2LE') BNDDIR('IFSTEXT')
     FAF4MOTP1  O    E             DISK    EXTFILE('AF400/AF4MOTP1')

     D/copy IFSEBOOK/QRPGLESRC,IFSIO_H      D/copy IFSEBOOK/QRPGLESRC,ERRNO_H      D/copy IFSEBOOK/QRPGLESRC,IFSTEXT_H
     D STDIN           C                   CONST(0)      D STDOUT          C                   CONST(1)      D STDERR          C                   CONST(2)
     D trt_file        PR                  EXTPGM('TRTFILE')      D   curfile                   1024A    const      D trt_file        PI      D   curfile                   1024A    const
     D fd              S             10I 0      D line            S           1024A      D msg             S           1024A      D len             S             10I 0      D nbl             S             10I 0      D nbm             S             10I 0
    D i               S             10I 0      D d               S             10I 0      D f               S             10I 0      Dsavmot           s             26      Dhtmlencours      s               N      Daecrire          s               N
     d car             s              1     dim(80) ctdata perrcd(80)      d carspc          s              8     dim(80) ctdata perrcd(1)      D caridx          S             10I 0

      /FREE        exsr trtFile;        *inlr = *on;        //*************************************************************        //  Read file, line by line        //*************************************************************       /END-FREE

|
     CSR   trtFile       begsr
     C
      //------------------------
      /FREE
         fd = open(%trim(curfile):  O_RDONLY+O_TEXTDATA);
         if fd < 0;
           die('open(): ' + %str(strerror(errno)));
         endif;

         dow readline(fd: %addr(line): %size(line))>=0;              exsr trt_line;              nbl += 1;          enddo;
          msg = 'Fichier ' + %trim(curfile) + ' ('                 +%char(nbl) + 'lignes/ '                 +%char(nbm) + 'mots )' ;           len = %len(%trimr(msg));           writeline(STDOUT: %addr(msg): len);          callp close(fd);          //------------------------       /END-FREE      CSR                 endsr
    CSR   trt_line      begsr      C       //------------------------       /FREE          mot = *blanks;          for i=1 to %len(%trimr(line));          // blanc et &nbsp; c'est pareil            if (i < %len(%trimr(line))-4 AND %subst(line : i : 6) = '&nbsp;');             %subst(line : i : 6) = '      ';            ENDIF;
// on trouve un blanc, "mot" contient le mot précédent
           if %subst(line : i : 1) = ' '             or %subst(line : i : 1) = ','             or %subst(line : i : 1) = '.';               if mot <> *blank;                 exsr trt_mot;               ENDIF;            else;               // caractère significatif               if htmlencours OR %subst(line :i :1) = '<';                 // c'est du HTML recherche de >
                dou i >= %len(%trimr(line)) or %subst(line : i :1) = '>';                    i +=1;                  ENDDO;                  if i > %len(%trimr(line)) OR                     i = %len(%trimr(line)) and %subst(line : i :1) <> '>' ;                    // si on arrive ici, la fin de HTML n'a pas été trouvée                    htmlencours = *on;                  else;                    // caractère > trouvé                    // on recommence après le >                    htmlencours = *off;                  ENDIF;               else;               // caractère normal, constitution du mot               mot = %trimr(mot) + %subst(line : i : 1);               ENDIF;            ENDIF;          ENDFOR;          // dernier mot de la ligne          if mot <> *blank;            exsr trt_mot;          ENDIF;         //------------------------
      /END-FREE      CSR                 endsr

|
     CSR   trt_mot       begsr
     C
      //------------------------
      /FREE
         aecrire = *on;
         savmot = mot; // sauvegarde mot d'origine pour debug;

// traitement d'un mot
         // enlever le S pluriel ?          IF   %subst(mot : %len(%trimr(mot)) : 1) = 's' or               %subst(mot : %len(%trimr(mot)) : 1) = 'S';           IF   %len(%trimr(mot)) > 1 AND                (%subst(mot : %len(%trimr(mot))-1 : 1) = 'e' or                 %subst(mot : %len(%trimr(mot))-1 : 1) = 'E' ) ;                  %subst(mot : %len(%trimr(mot)) : 1) = ' ';           ENDIF;          ENDIF; // enlever certains premiers caractères
IF %subst(mot : %len(%trimr(mot)) : 1) = ':'
or %subst(mot : %len(%trimr(mot)) : 1) = '/'
or %subst(mot : %len(%trimr(mot)) : 1) = '('
or %subst(mot : %len(%trimr(mot)) : 1) = '''' ;
%subst(mot : %len(%trimr(mot)) : 1) = ' ';
if mot = *blanks;
aecrire = *off;
leavesr;
else;
mot = %triml(mot);
ENDIF; // enlever ")" final, si pas de "("
IF %subst(mot : %len(%trimr(mot)) : 1) = ')'
and %scan('(' : mot) = 0;
%subst(mot : %len(%trimr(mot)) : 1) = ' ';
ENDIF;
        // "petits" mots à ignorer aussi          if %len(%trimr(mot)) < 3;            aecrire = *off;            mot = *blanks;            leavesr;          ENDIF;
         // autres mots à ignorer (mais ou est donc ornicar ?)           if mot = 'mais' or mot = 'donc' or mot = 'par' or mot = 'pour'           or mot ='car' or mot = 'que' or mot = 'parceque' or mot ='jamais'           or mot ='aux' or mot = 'celle' or mot = 'ceux' or mot = 'que'           or mot ='plus' or mot = 'son' or mot = 'dans' or mot ='une'           or mot = 'quelle' or mot = 'sur' or mot = 'tous' or mot = 'tout'           or mot = 'les' or mot = 'des' or mot = 'elle' or mot = 'pendant'           or mot = 'ils' or mot = 'avant' or mot = 'après' ;            aecrire = *off;            mot = *blanks;            leavesr;          ENDIF;        

|
         if aecrire;          
         // enlever l', s' , d' , j' et  n'

           for d = 1 to %len(%trimr(mot))-1;
              if %subst(mot : d : 2) = 'l'''                or %subst(mot : d : 2) = 's'''                or %subst(mot : d : 2) = 'd'''
or %subst(mot : d : 2) = 'j'''
               or %subst(mot : d : 2) = 'n''' ;                   %subst(mot : d : 2) = '  ';               ENDIF;               // HTML : recherche et remplacement de &eacute; par é, etc...               if %subst(mot : d : 1) = '&';                  f = %scan(';':mot:d);                  if f>0;                     caridx = %lookup(%subst(mot:d:(f-d)+1):carspc);                     if caridx >0;                       mot = %replace(car(caridx):mot:d:(f-d)+1);                     ENDIF;                  ENDIF;               ENDIF;             ENDFOR;             // enlever certains caractères spéciaux, remplacés par espace               mot = %xlate('["-;,.]+>!?&' : '            ' : mot) ;         // derniers tests (lg > 2 et peu d'autres caractères spéciaux)
if %len(%trimr(mot)) > 2;
// dans la base en minuscules sans accent ; mot = %xlate('àâéèêë{}ÈÉÊîÎïöôÔ' : 'aaeeeeeeeeeiiiooo' : mot);
mot = %xlate('ABCDEFGHIJKLMNOPQRSTUVWXYZ' :
'abcdefghijklmnopqrstuvwxyz' : mot);
// écriture dans la base;
if isAlphaNum(mot);
// on note la position du mot dans le fichier afin de proposer des recherches // de type "SMTP" près de "POP3"
Nbm += 1;
mot = %triml(mot);
chemin = curfile;
position = nbm;
write af4motf1;
ENDIF;
endif;
         endif;          mot = *blanks;          //------------------------       /END-FREE      CSR                 endsr

      /DEFINE ERRNO_LOAD_PROCEDURE       /COPY IFSEBOOK/QRPGLESRC,ERRNO_H

|
      P isAlphaNum      B
*=====================================================================
D PI N
D mot 26A Value
D i s 3I 0
D cpt s 3I 0
      /free
        cpt = 0;
        for i = 1 to %len(%trimr(mot));
          if Chralphanum(%subst(mot : i : 1));
           cpt += 1;
          ENDIF;
        ENDFOR;
        if cpt >= %len(%trimr(mot)) * 0,8; // plus de 80 % = alphanumérique
          Return *On;
        Else;
          Return *Off;
        Endif;
   /end-free
   P                    E
P ChrAlphaNum B Export *===================================================================== D PI N D Chr 1A Value
    /FREE
       If Chr >= 's' And Chr <= 'z' Or
        Chr >= 'j' And Chr <= 'r' Or
        Chr >= 'a' And Chr <= 'i' Or
        Chr >= 'S' And Chr <= 'Z' Or
        Chr >= 'J' And Chr <= 'R' Or
        Chr >= 'A' And Chr <= 'I' Or
        Chr >= '0' And Chr <= '9';
          Return *On;
       Else;
          Return *Off;
       Endif;
   /END-FREE
   P                   E
**
"&<>£|àâäçèéêëîïñôöùûüÀÂÄÉÈÊËÎÏÔÖÙÛÜ-°
**
&quot; &amp; &lt; &gt; &pound; &#124; &agrave; &acirc; &auml; &ccedil; &egrave; &eacute; &ecirc; &euml; &icirc; &iuml; &ntilde &ocirc; &ouml; &ugrave; &ucirc; &uuml;
&Agrave; &Acirc; &Auml; &Eacute; &Egrave; &Ecirc; &Euml; &Icirc; &Iuml; &Ocirc; &Ouml; &Ugrave; &Ucirc; &Uuml; &bull; &deg;




©AF400