在单词数组中找到相似的单词

时间:2022-09-13 09:35:36

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

这些天我在谷歌上搜索,但没有任何帮助。我现在不确定是否可能,所以我想我就在*上。

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在数组中查找字符串
  • javascript find similar words in array
  • javascript可以在数组中找到类似的单词
  • javascript regex similar words
  • javascript regex相似的单词
  • ... and more
  • …和更多的

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

我希望你能理解我的意思,并给我一些提示。

2 个解决方案

#1


5  

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

Levenshtein距离是计算相似单词之间的距离的度量。对于每一个变化的、混乱的或缺失的字母,距离都增加了。你可以在这里读到更多:http://en.wikipedia.org/wiki/Levenshtein_distance。

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

并在这里引用不同语言的实现:http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance

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

我希望这能对你们有所帮助,也谢谢你们的评论。

#2


1  

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)}

使用那里的代码,您可以使用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...

您可以根据需要调整阈值。当然,这是Ruby,所以你必须翻译成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

#1


5  

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

Levenshtein距离是计算相似单词之间的距离的度量。对于每一个变化的、混乱的或缺失的字母,距离都增加了。你可以在这里读到更多:http://en.wikipedia.org/wiki/Levenshtein_distance。

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

并在这里引用不同语言的实现:http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance

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

我希望这能对你们有所帮助,也谢谢你们的评论。

#2


1  

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)}

使用那里的代码,您可以使用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...

您可以根据需要调整阈值。当然,这是Ruby,所以你必须翻译成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