Here the solution in PHP:
#!/usr/bin/php -q /* -*- c -*- */
<?php
/** usage from command line:
* php index.php input.html words.txt
* where input.html is the book file
* and words.txt is a file with excluded words (one on each line)
*
* result will be in file out_input.html
*/
$transforming = false;
// input and excluded words must be submitted
if (isset($argv[1]) && isset($argv[2])) {
$transforming = true;
$inputFilename = $argv[1];
$inputFile = fopen($inputFilename, "r") or die('Input file not found');
$excludedWordFilename = $argv[2];
$excludedWordsFile = fopen($excludedWordFilename, 'r') or die('Excluded words file not found');
// load excluded words
$excludedWords = [];
while (! feof($excludedWordsFile)) {
$excludedWords[] = fgets($excludedWordsFile);
}
$outputLines = [];
// read input file line by line
while (! feof($inputFile)) {
$line = fgets($inputFile);
$outputLines[] = process($line, $excludedWords);
}
// write result to file
$outputFile = implode(PHP_EOL, $outputLines);
$outputFilename = 'out_'.$inputFilename;
file_put_contents($outputFilename, $outputFile);
} else {
echo 'no file, please use this format: php index.php "inputfile.html" "excludedwords.txt"';
}
function process($line, $excludedWords)
{
// splits the line into comments and non-comment parts
$lineParts = preg_split('/(<!--.+?-->)/msi', $line, 0, PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE);
// extract all comments from the line
$lineComments= preg_grep('/<!--.+?-->/', $lineParts);
// And keep the non comment part of the line for word comparison
$lineText = implode(' ', preg_grep('/<!--.+?-->/', $lineParts, PREG_GREP_INVERT));
// get the original comment tags and trimmed comment words within it
preg_match_all('/<!--[\s](.+?)[\s]-->/msi', implode(' ', $lineComments), $comments);
list($commentTags, $commentTexts) = $comments;
$comments = array_combine($commentTags, $commentTexts);
// explode each words in the comment and clean from excluded words
foreach ($comments as $tag => $words) {
$moreWordsToCheck = preg_split('`[\s,#]+`', $words);
foreach ($moreWordsToCheck as $wordToCheck) {
// check if word in exclude list
if (! in_array($wordToCheck, $excludedWords)) {
if (stripos($lineText, $wordToCheck)) {
$line = str_replace($tag, '', $line);
}
}
}
}
return $line;
}
also here a example of the document words.txt for spanish speakers with the most of pronouns and so for:
a
a cuál
a cuáles
a lo mejor
a qué
a quién
a quiénes
acaso
además
ahí
ahora
algo
algún
alguna
algunas
alguno
algunos
allí
alrededor
ante
anteayer
antes
aparte
aquel
aquella
aquellas
aquello
aquellos
aquí
así
asimismo
aún
ayer
bajo
bastante
bastantes
bien
cabe
cada
casi
cerca
como
con
contra
cuál
cuáles
cuanta
cuánta
cuantas
cuántas
cuanto
cuánto
cuantos
cuántos
cuya
cuyas
cuyo
cuyos
de
debajo
delante
demasiado
dentro
deprisa
desde
despacio
después
detrás
durante
el
él
el cual
el mío
el nuestro
el que
el suyo
el tuyo
el vuestro
ella
ellas
ellos
en
encima
entre
esa
esas
ese
eso
esos
esta
estas
este
esto
estos
fuera
hacia
hasta
hoy
incluso
jamás
la
la cual
la mía
la nuestra
la que
la suya
la tuya
la vuestra
las
las cuales
las mías
las nuestras
las que
las suyas
las tuyas
las vuestras
le
lejos
les
lo
los
los cuales
los míos
los nuestros
los que
los suyos
los tuyos
los vuestros
luego
mal
más
me
mediante
medio
menos
mi
mía
mías
mío
míos
mis
mucho
muy
nada
ningún
ninguna
ningunas
ninguno
ningunos
no
nos
nosotras
nosotros
nuestra
nuestras
nuestro
nuestros
nunca
os
otra
otras
otro
otros
para
poco
por
pronto
que
qué
quien
quién
quienes
quiénes
quizá
quizás
se
según
sendas
sendos
sí
sin
so
sobre
su
sus
suya
suyas
suyo
suyos
tal vez
también
tampoco
tanta
tantas
tanto
tantos
tarde
te
temprano
toda
todas
todavía
todo
todos
tras
tu
tú
tus
tuya
tuyas
tuyo
tuyos
un
una
unas
unos
usted
ustedes
varias
varios
versus
vía
vos
vosotras
vosotros
vuestra
vuestras
vuestro
vuestros
ya
yo