E
E
eternal_blue2020-08-02 13:27:42
Web development
eternal_blue, 2020-08-02 13:27:42

How to create a substitution with error tolerance?

Hello, I'm sawing my forum, on the page for creating new messages there is an input where a person enters tags. The task is to make auto-completion below, depending on what the person entered, as here, on habr qna. I implemented like this:

//tag - инпут, куда человек вводит теги. result - div, куда выводятся варианты тегов, на странице находится под инпутом
//result-elem - предлагаемый тег внутри result
tag.addEventListener('input', ()=>{ //при любом изменении инпута тегов благодаря этому событию происходит триггер
    
    if(tag.value.length > 2) { //при вводе больше 2 букв начинается подбор
        result.style.opacity = '1'; //показываем div результатов
        while (result.firstChild) { //очищаем из него предыдущие значения
            result.removeChild(result.firstChild);
        }
        tags.forEach((elem)=>{  //tags - массив тегов вида ['apple','orange','pear'], перебираем методом forEach
            let string = elem; //string принимает значения тега
            let flag = string.includes(tag.value); //если строка string содержит содержимое инпута, то во flag кладется true
            if (flag) { 
                insertElem(elem); //функция, ничего особенного, создает и вставляет элемент p в result с содержимым elem
            }
        });
    } else {
        result.style.opacity = '0'; // если введено меньше 2 символов, убираем result
    }

});

Checked, everything works. The question is: can human error be built into this algorithm? If, for example, "fishing" is entered, how to teach the system to understand that this is a "fishing" tag?

Upd: Something like calculating the percentage of matches and something like that immediately comes to mind. Am I digging correctly?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question