Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #142 from hcodes/fix_repeated_words
Browse files Browse the repository at this point in the history
Print row:col for repeated words
  • Loading branch information
hcodes committed Mar 27, 2020
2 parents fc084ef + 6091f1d commit 5d8175f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 33 deletions.
4 changes: 2 additions & 2 deletions lib/dictionary.js
@@ -1,6 +1,7 @@
'use strict';

const chalk = require('chalk');
const { ERROR_TOO_MANY_ERRORS } = require('yandex-speller');
const debug = require('./debug');
const exitCodes = require('./exit-codes');
const utils = require('./utils');
Expand Down Expand Up @@ -132,8 +133,7 @@ module.exports = {
const dictionary = this.get();

data.forEach(function(typo) {
// ERROR_TOO_MANY_ERRORS = 4
if (typo.code === 4 || this.isTypo(typo.word, dictionary)) {
if (typo.code === ERROR_TOO_MANY_ERRORS || this.isTypo(typo.word, dictionary)) {
result.push(typo);
}
}, this);
Expand Down
21 changes: 11 additions & 10 deletions lib/report/index.js
Expand Up @@ -11,22 +11,23 @@ const stats = {
total: 0,
};
const reports = [];
const reportNames = {};
const reportNames = new Set();
const buffer = [];

module.exports = {
addReports(names) {
names.forEach(function(name) {
const moduleName = pth.extname(name) === '.js' ? name : './report/' + name;
if (reportNames[moduleName]) {
const moduleName = pth.extname(name) === '.js' ? name : './' + name;

if (reportNames.has(moduleName)) {
return;
}

try {
reportNames[moduleName] = true;
reports.push(require(moduleName));
reportNames.add(moduleName);
} catch (e) {
console.error(chalk.red('Can\'t load report module "' + moduleName + '".'));
console.error(chalk.red(`Can't load report module "${moduleName}".`));
console.error(chalk.red(e));
}
});
Expand All @@ -37,17 +38,17 @@ module.exports = {
});
},
oneach(hasError, data, dictionary) {
stats.total++;

const hasTypos = Boolean(data && data.data && data.data.length);

stats.total++;
if (hasTypos) {
stats.hasTypos = true;
}

if (hasError || hasTypos) {
stats.errors++;

if (hasTypos) {
stats.hasTypos = true;
}

buffer.push([hasError, data]);
} else {
stats.ok++;
Expand Down
3 changes: 2 additions & 1 deletion lib/utils.js
Expand Up @@ -5,6 +5,7 @@ const isutf8 = require('isutf8');
const minimatch = require('minimatch');
const pth = require('path');
const stripJsonComments = require('strip-json-comments');
const { ERROR_TOO_MANY_ERRORS } = require('yandex-speller');
const isWin = process.platform === 'win32';

module.exports = {
Expand Down Expand Up @@ -177,7 +178,7 @@ module.exports = {
* @returns {boolean}
*/
hasManyErrors(data) {
return data.some(el => el.code === 4); // ERROR_TOO_MANY_ERRORS
return data.some(el => el.code === ERROR_TOO_MANY_ERRORS);
},
/**
* Get uptime in sec.
Expand Down
34 changes: 18 additions & 16 deletions lib/yaspeller.js
Expand Up @@ -15,7 +15,6 @@ const yaspellerApi = require('yandex-speller');
const md = new MarkdownIt();
const printDebug = require('../lib/debug').print;
const MAX_LEN_TEXT = 10000; // Max length of text for Yandex.Speller API
const TOO_MANY_ERRORS = 4;

function getMaxRequest(settings) {
return settings.maxRequest || 2;
Expand Down Expand Up @@ -327,14 +326,19 @@ function checkSitemap(url, commonCallback, settings, callback) {
*/
function addPositions(text, data) {
data.forEach(function(item) {
if (item.code === TOO_MANY_ERRORS || item.position) {
if (item.code === yaspellerApi.ERROR_TOO_MANY_ERRORS || item.position) {
return;
}

const result = [];
const letters = '[^a-zA-Zа-яА-ЯЁёҐґЄєІіЇї]';
let word = item.word;

text.replace(new RegExp(item.word + '(?:' + letters + '|$)', 'g'), function($0, index) {
if (item.code === yaspellerApi.ERROR_REPEATED_WORD) {
word = item.word + '\\s+' + item.word;
}

text.replace(new RegExp(word + '(?:' + letters + '|$)', 'mg'), function($0, index) {
const prevSymbol = text[index - 1];
if (prevSymbol && prevSymbol.search(letters) === -1) {
return;
Expand Down Expand Up @@ -473,15 +477,15 @@ function prepareText(text) {
text = fixLineEndings(text);
text = removeSpecialSymbols(text);

return text.trim();
return text;
}

function fixLineEndings(text) {
return text.replace(/\r\n/g, '\n') // Fix Windows
return text
.replace(/\r\n/g, '\n') // Fix Windows
.replace(/\r/g, '\n') // Fix MacOS
.replace(/\s+\n/g, '\n') // Trailling spaces
.replace(/\s+/g, ' ') // Repeat spaces
.replace(/\n+/g, '\n'); // Repeat line endings
.trimRight();
}

function removeSpecialSymbols(text) {
Expand All @@ -495,17 +499,15 @@ function removeSpecialSymbols(text) {
}

function getErrors() {
return yaspellerApi.errorCodes.filter(function(el) {
return el.code !== TOO_MANY_ERRORS;
}).map(function(el) {
return {
return yaspellerApi.errors
.filter(el => el.code !== yaspellerApi.ERROR_TOO_MANY_ERRORS)
.map(el => ({
code: el.code,
title: el.text
};
}).concat({
code: 100, // ERROR_EYO
title: 'Letter Ё (Yo)'
});
})).concat({
code: 100, // ERROR_EYO
title: 'Letter Ё (Yo)'
});
}

module.exports = {
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -46,7 +46,7 @@
"node-fetch": "^2.6.0",
"strip-json-comments": "^3.0.1",
"xml2js": "^0.4.23",
"yandex-speller": "^4.0.0"
"yandex-speller": "^4.1.0"
},
"devDependencies": {
"chai": "^4.2.0",
Expand Down

0 comments on commit 5d8175f

Please sign in to comment.