Revision 117755 of "Utente:Ruthven/PostOCR.js" on napwikisource/* Correzioni post-OCR automatiche alla creazione di una nuova Pagina */
mw.loader.using(['ext.proofreadpage.page', 'oojs-ui-core']).done( function () {
$(document).ready(function() {
// Only for NS Paggena
if ( mw.config.get('wgCanonicalNamespace') === 'Page' &&
(mw.config.get("wgAction")==="edit" ||mw.config.get("wgAction")==="submit") ) {
// bozza funzioni specifiche
if ( typeof $ != 'undefined' && typeof $.fn.wikiEditor != 'undefined' ) {
$( function() {
// CREA PULSANTE
var fixButton = new OO.ui.ButtonWidget( {
label: '’’',
id: 'postOCR-button-widget',
framed: false,
title: 'Appara \'o tiesto'
} );
fixButton.on( 'click', function ( context ) {
postOCR();
} );
$( '#wikiEditor-section-advanced' ).append( fixButton.$element );
});
}}});
});
function newDpl(testo) {
var r6 = /(.*)\<\!--(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)\s(\d+)--\>/;
var r4 = /(.*)\<\!--(\d+)\s(\d+)\s(\d+)\s(\d+)--\>/;
if (r6.test(testo)) {
testo = testo.split("\n");
datiPagina = {};
datiPagina.righe = [];
for (var i = 0; i < testo.length; i += 1) {
if (r6.test(testo[i])) {
var res = r6.exec( testo[i] );
datiPagina.xypagina = [ res[6], res[7] ];
var riga = [ res[2], res[3], res[4], res[5], res[1] ];
datiPagina.righe.push( riga );
testo[i] = res[1];
}
if (r4.test(testo[i])) {
var res = r4.exec( testo[i] );
var riga = [ res[2], res[3], res[4], res[5], res[1] ];
datiPagina.righe.push( riga );
testo[i] = res[1];
}
}
testo = testo.join("\n");
}
return testo;
}
/* Correzioni post-OCR automatiche alla creazione di una nuova Pagina */
function postOCR () {
var editbox = document.getElementsByName('wpTextbox1')[0];
editbox.value = newDpl(editbox.value);
editbox.value = apostrofi();
editbox.value = editbox.value.replace(/’’’/g, '\'\'\'')
.replace(/(\s|\W)’’(\w+)/g, '$1\'\'$2')
.replace(/(\b|\W)’’(\W)/g, '$1\'\'$2').replace(/ \n/g, '\n');
// tento di disabilitare la riunione delle righe spezzate |
//.replace(/-\n/g, '')
//.replace(/- \n/g, '');
editbox.value = editbox.value.replace(/([^|])[-¬] *\n([^ \n]*)[ ]*[\n]?/g,"$1$2\n"); // importo da cleanup()
// dehyphen(editbox.value)
// eseguo temporaneamente dehyphen in attesa di costruire/trovare la regex giusta
editbox.value = editbox.value.replace(/1’/g, 'l’')
.replace(/\bdeir\b\s*/g, 'dell’')
.replace(/\bair\b\s*/g, 'all’')
.replace(/\bneir\b\s*/g, 'nell’')
.replace(/\bcoir\b\s*/g, 'coll’')
.replace(/\bperche\b/g, 'perchè')
.replace(/\bpoiche\b/g, 'poichè')
.replace(/\bpiu\b/g, 'più')
.replace(/\bpiti\b/g, 'più')
.replace(/\s+([,;.:!?])/g, '$1')
.replace(/«\s+/g, '«')
.replace(/\s+»/g, '»')
.replace(/([bcdfghlmnprstvzBCDFGHLMNPRSTV])’\s+/g, '$1’')
.replace(/(eh|cb)’/g, 'ch’')
.replace(/\s+$/, '')
.replace(/\.\.\./g, '…')
.replace(/fi/g, 'fi')
.replace(/U’/g, 'll’');
console.log("Eseguite correzioni post-OCR automatiche");
}
/*Chiamata senza parametri corregge gli apostrofi nel box di default (wpTextbox1[0] in ns0, wpTextbox1[1] in nsPagina.
In rari casi può essere forzata l'azione su un box non di default (box="0","1","2" ) */
function newApostrofi(box) {
var testo;
if (box === undefined) {
testo = leggiBox();
} else {
testo = leggiBox(box);
}
var testoCod = codifica(testo);
testoCod[0] = testoCod[0].replace(/'/g, '’').replace(/’’’’’/g, "'''''").replace(/’’’’/g, "''''").replace(/’’’/g, "'''").replace(/’’/g, "''");
testo = decodifica(testoCod[0], testoCod[1]);
if (box === undefined) {
scriviBox(testo);
} else {
scriviBox(testo, box);
}
}
//Sostituisce tutti gli apostrofi dattilografici in tipografici, ma rispetta gli apostrofi di marckup wiki e gli apostrofi
//contenuti in: math, {{{}}}, {{}}, [[]], [], http:.....
function apostrofi(editbox) {
if (editbox === undefined) {
editbox = document.getElementsByName('wpTextbox1')[0];
}
var testoCod = codifica(editbox.value);
testoCod[0] = testoCod[0].replace(/'/g, '’').replace(/’’’’’/g, "'''''").replace(/’’’’/g, "''''").replace(/’’’/g, "'''").replace(/’’/g, "''");
return decodifica(testoCod[0], testoCod[1]);
}
function codifica(testo) {
var l = [];
// gestione {{poem..}}
var poem="";
var lista=produciLista(testo,"{{poem","}}",1,"{{")
if (lista.length>0) {
for (i=0;i<lista.length;i+=1) {
poem=lista[i].replace("{{poem","<#poem>").replace(/}}$/,"</#poem>")
testo=testo.replace(lista[i],poem)
}
}
// fine gestione {{poem..}}
var res = ss(testo, l, "<math", "</math>", "");
res = ss(res[0], res[1], "<!--", "-->", "<");
res = ss(res[0], res[1], "{", "}", "{");
res = ss(res[0], res[1], "[", "]", "[");
res = ss(res[0], res[1], "<", ">", "<");
res = ss(res[0], res[1], "http://", " ", "");
res = ss(res[0], res[1], "https://", " ", "");
return res;
}
function ss(testo, l, tagi, tagf, x) {
while (find_stringa(testo, tagi, tagf, 1) > "") {
var el = find_stringa(testo, tagi, tagf, 1, tagi);
testo = testo.replace(el, "###el" + l.length + "###");
l.push(el);
}
return [testo, l];
}
// La funzione decodifica() riceve un testo codificato e la lista degli elementi protetti e restituisce un testo
// con gli elementi protetti risistemati al loro posto; è complementare a codifica()
function decodifica(testo, l) {
for (i = l.length - 1; i > -1; i = i - 1) {
testo = testo.replace("###el" + i + "###", l[i]);
}
// gestione {{poem ... }}
testo=testo.replace(/<#poem>/g,"{{poem").replace(/<\/#poem>/g,"}}");
return testo;
}All content in the above text box is licensed under the Creative Commons Attribution-ShareAlike license Version 4 and was originally sourced from https://nap.wikisource.org/w/index.php?oldid=117755.
![]() ![]() This site is not affiliated with or endorsed in any way by the Wikimedia Foundation or any of its affiliates. In fact, we fucking despise them.
|