Skip to content

Commit

Permalink
Added differences highlighting to suggested corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
RagePeanut committed Oct 11, 2018
1 parent cb3213c commit b396d2e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# checky (0.1.7)
# checky (0.1.8)
A Steem bot that checks if the users mentioned in a post exist on the blockchain. If they don't, it indicates to authors that they may have made a typo or it suggests them some existing usernames close to the wrong ones.

See this bot in action here: https://steemit.com/@checky/comments
Expand Down
6 changes: 5 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"request_nodes": [
"https://api.steemit.com"
"https://api.steemit.com",
"https://api.steem.house",
"https://gtg.steem.house:8090",
"https://api.steem.house",
"https://rpc.curiesteem.com"
],
"stream_nodes": [
"https://api.steemit.com",
Expand Down
2 changes: 1 addition & 1 deletion 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "checky",
"version": "0.1.7",
"version": "0.1.8",
"description": "A bot that checks if mentioned Steem users exist.",
"main": "app.js",
"scripts": {
Expand Down
41 changes: 34 additions & 7 deletions utils/username-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function correct(username, author, otherMentions, tags) {
const mentionNoPunct = mention.replace(/[\d.-]/g, '');
return usernameNoPunct === mentionNoPunct || 'the' + usernameNoPunct === mentionNoPunct;
});
if(suggestion) return resolve('@<em></em>' + suggestion);
if(suggestion) return resolve('@<em></em>' + highlightDifferences(username, suggestion));
// Testing for usernames that are one edit away from the wrong username
let edits = new Set();
edits1(username, edits, false);
Expand All @@ -81,15 +81,15 @@ function correct(username, author, otherMentions, tags) {
suggestions = await getExisting(edits);
}
if(suggestions.length > 0) {
if(suggestions.length === 1) return resolve('@<em></em>' + suggestions[0]);
if(suggestions.length === 1) return resolve('@<em></em>' + highlightDifferences(username, suggestions[0]));
// Trying to find the better suggestion based on the mentions made by the author in the post and, if needed, in his previous posts
suggestion = suggestions.find(mention => otherMentions.includes(mention) || users[author].mentioned.includes(mention));
if(suggestion) return resolve('@<em></em>' + suggestion);
if(suggestion) return resolve('@<em></em>' + highlightDifferences(username, suggestion));
// Suggesting the most mentioned username overall
return resolve('@<em></em>' + suggestions.sort((a, b) => users[b].occ - users[a].occ)[0]);
} else if(await exists('the' + username)) return resolve('@<em></em>the' + username);
return resolve('@<em></em>' + highlightDifferences(username, suggestions.sort((a, b) => users[b].occ - users[a].occ)[0]));
} else if(await exists('the' + username)) return resolve('@<em></em><strong>the</strong>' + username);
// Testing for tags written as mentions
else if(tags.includes(username)) return resolve('#' + username)
else if(tags.includes(username)) return resolve('#' + username);
// No suggestion
else return resolve(null);
});
Expand Down Expand Up @@ -133,6 +133,29 @@ function deletes(username, edits, mustBeValid) {
}
}

/**
* Highlights the differences between a wrong username and its suggested correction
* @param {string} username A wrong username
* @param {string} correction The wrong username's suggested correction
* @returns {string} The correction with its differences highlighted (between strong tags)
*/
function highlightDifferences(username, correction) {
// Two deletes
if(username.length === correction.length + 2) return correction;
let highlighted = correction.split('');
for(let i = 0; i < correction.length; i++) {
if(correction[i] !== username[i]) {
if(correction.length > username.length) {
username = username.substring(0, i + 1) + username.substring(i, username.length);
}
if(correction.length >= username.length) {
highlighted[i] = '<strong>' + correction[i] + '</strong>';
}
}
}
return highlighted.join('')
}

/**
* Generates all the variations of `username` with one character inserted
* @param {string} username The username
Expand Down Expand Up @@ -245,7 +268,11 @@ function getExisting(usernames) {
function getDiscovered(usernames) {
return new Promise(resolve => {
steem.api.lookupAccountNames(usernames, async (err, res) => {
if(err) return resolve(await getDiscovered(usernames));
if(err) {
request_nodes.push(request_nodes.shift());
steem.api.setOptions({ url: request_nodes[0] });
return resolve(await getDiscovered(usernames));
}
const discovered = res.filter(user => user).map(user => user.name);
addUsers(null, discovered);
return resolve(discovered);
Expand Down

0 comments on commit b396d2e

Please sign in to comment.