最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Find similar words in array of words - Stack Overflow

programmeradmin2浏览0评论

I googled these days but nothing helps. I m not sure now if its possible, so I thought I just aks at stackoverflow.

The situation: The user can input a word or in a inputbox. When he finishes a function check if the word is in the array of words - easy. Now I wanna write a help, if one letter is missing or the letters are written the wrong way, a message should popout.

What are the keys to search for? I tried:

  • javascript find string in array
  • javascript find similar words in array
  • javascript regex similar words
  • ... and more

I hope you undestand what i mean, and can give me some hints.

I googled these days but nothing helps. I m not sure now if its possible, so I thought I just aks at stackoverflow.

The situation: The user can input a word or in a inputbox. When he finishes a function check if the word is in the array of words - easy. Now I wanna write a help, if one letter is missing or the letters are written the wrong way, a message should popout.

What are the keys to search for? I tried:

  • javascript find string in array
  • javascript find similar words in array
  • javascript regex similar words
  • ... and more

I hope you undestand what i mean, and can give me some hints.

Share Improve this question asked Jul 7, 2012 at 17:18 user1509034user1509034 811 silver badge8 bronze badges 3
  • 7 take a look at the Levenshtein distance here: en.wikipedia/wiki/Levenshtein_distance and the implementations here: en.wikibooks/wiki/Algorithm_Implementation/Strings/… It provides you with a metric for the similarity of words. – peshkira Commented Jul 7, 2012 at 17:21
  • 1 @peshkira that would make a good answer – Stefan Commented Jul 7, 2012 at 21:19
  • Git: js-levenshtein – Roko C. Buljan Commented Jan 2, 2022 at 13:51
Add a ment  | 

2 Answers 2

Reset to default 13

The Levenshtein distance is a metric for puting the distance between similar words. For each changed, shuffled or missing letter the distance is increased. You can read more here: http://en.wikipedia/wiki/Levenshtein_distance

and take a reference for the implementation in different languages here: http://en.wikibooks/wiki/Algorithm_Implementation/Strings/Levenshtein_distance

I hope that helps and thanks for the ment up votes ;)

See here for an algorithm to check for similarity between words.

Using the code from there, you can use array.any?{|e| e.similar?(user_input)}

You may adjust the threshold as required. Of course, this is Ruby, so you'd have to translate to javascript...

I copied the code from there:

class String

  def levenstein(other, ins=2, del=1, sub=1)

    return nil if self.nil? || other.nil?

    dm = []
    dm[0] = (0..self.length).collect { |i| i * ins}
    fill = [0] * (self.length - 1)

    for i in 1..other.length
      dm[i] = [i * del, fill.flatten] 
    end

    for i in 1..other.length
      for j in 1..self.length
        dm[i][j] = [
          dm[i-1][j-1] + (self[i-1] == other[i-1] ? 0 : sub),
          dm[i][j-1] + ins,
          dm[i-1][j] + del
          ].min
      end
    end

    dm[other.length][self.length]
  end

  def similar?(other, thresh = 2)
    self.levenstein(other) < thresh
  end

end

# Tryout
"Foobar".similar?("Fuubar", 3) # => true
发布评论

评论列表(0)

  1. 暂无评论