Last active
July 16, 2017 09:48
-
-
Save vsemozhetbyt/0efd0e3d6abc511e95968f9f38120156 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const fs = require('fs'); | |
const readline = require('readline'); | |
const files = []; | |
const [, , target = '.'] = process.argv; | |
if (fs.statSync(target).isDirectory()) { | |
files.push(...fs.readdirSync(target) | |
.map(file => `${target}/${file}`) | |
.filter(file => !fs.statSync(file).isDirectory())); | |
} else { | |
files.push(target); | |
} | |
const whitelisted = 'θ'; // used in some transcriptions | |
const latinScript = `[\\p{Script_Extensions=Latin}\\p{Script_Extensions=Common}${whitelisted}]`; | |
const anyLetter = `\\p{General_Category=Letter}`; | |
const latinLetter = `(?=${latinScript})${anyLetter}`; | |
const otherLetters = RegExp(`(?:(?!${latinScript})${anyLetter})+`, 'gu'); | |
const mix = `(?:${latinLetter}${otherLetters.source}|${otherLetters.source}${latinLetter})`; | |
const mixedWord = RegExp(`${anyLetter}*${mix}${anyLetter}*`, 'gu'); | |
const wrapAt = /(.{1,75}) /gu; | |
let mixedWords = 0; | |
check(files.shift()); | |
function check(file) { | |
let lineNum = 0; | |
let withErrors = false; | |
readline.createInterface({ | |
input: fs.createReadStream(file, { encoding: 'utf8' }), | |
}).on('line', (line) => { | |
lineNum++; | |
const matches = line.match(mixedWord); | |
if (matches) { | |
mixedWords += matches.length; | |
if (!withErrors) { | |
withErrors = true; | |
console.log(`\n${file}`); | |
} | |
console.log(` Line ${lineNum}: ${ | |
matches.map(str => `${str}: ${str.replace(otherLetters, '($&)')}`) | |
.join(', ')}`.replace(wrapAt, wrap)); | |
} | |
}).on('close', () => { | |
if (files.length) check(files.shift()); | |
else console.log(`\nMixed words: ${mixedWords}.`); | |
}); | |
} | |
function wrap(match, chunk, offset, string) { | |
const rest = string.length - (offset + chunk.length); | |
return `${chunk}${rest + chunk.length > 75 ? '\n ' : ' '}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
node --harmony_regexp_property check-mixed-latin-script.js [path-to-file-or-directory]
If the path argument is not provided, the current directory is checked. Directories are checked without recursion or filters (all files are checked within only defined directory).
Example:
cyrillic-in-latin.txt
:node --harmony_regexp_property check-mixed-latin-script.js cyrillic-in-latin.txt
: