在文本文件(Ruby)中搜索特定单词时显示周围的单词

时间:2022-06-01 20:28:17

I'm very new to ruby. I'm trying to search for any instance of a word in a text file (not the problem). Then when the word is discovered, it would show the surrounding text (maybe 3-4 words before and after the target word, instead of the whole line), output to a list of instances and continue searching.

我对ruby很陌生。我正在搜索文本文件中任何一个单词的实例(不是问题)。然后当单词被发现时,它会显示周围的文本(可能是目标单词前后的3-4个单词,而不是整行),输出到实例列表并继续搜索。

Example:

例子:

The quick brown fox jumped over the lazy dog.

敏捷的棕色狐狸跳过了懒狗。

Search word: jumped

搜索词:跳

Output: ...brown fox jumped over the...

输出:…棕色狐狸跳过了……

Any help is appreciated.

任何帮助都是感激。

def word_exists_in_file
   f = File.open("test.txt")
   f.each do line
      print line
      if line.match /someword/
         return true
      end
   end
   false
end

2 个解决方案

#1


4  

def find_word(string, word)
  r1 = /\w+\W/
  r2 = /\W\w+/
  "..." + string.scan(/(#{r1}{0,2}#{word}#{r2}{0,2})/i).join("...") + "..."
end

string = "The quick brown fox jumped over the lazy dog."

find_word(string, "the")
#=> "...The quick brown...jumped over the lazy dog..."

find_word(string, "over")
#=> "...fox jumped over the lazy..."

It's not perfect solution, just the path, so work around it.

它不是完美的解决方案,只是路径,所以围绕它工作。

#2


3  

Rails has a text helper called excerpt that does exactly this, so if you're trying to do this inside a Rails view:

Rails有一个叫做摘录的文本助手,它就是这样做的,所以如果你想在Rails视图中这么做的话:

excerpt('The quick brown fox jumped over the lazy dog',
        'jumped', :radius => 10)
=> "...brown fox jumped over the..."

If you want to use this outside Rails (but you have the Rails gems installed) you can load ActionView:

如果你想使用这个外部Rails(但是你已经安装了Rails gems),你可以加载ActionView:

require "action_view"
ActionView::Base.new.excerpt('The quick brown fox jumped over the lazy dog',
                                 'jumped', :radius => 10)

=> "...brown fox jumped over the..."

#1


4  

def find_word(string, word)
  r1 = /\w+\W/
  r2 = /\W\w+/
  "..." + string.scan(/(#{r1}{0,2}#{word}#{r2}{0,2})/i).join("...") + "..."
end

string = "The quick brown fox jumped over the lazy dog."

find_word(string, "the")
#=> "...The quick brown...jumped over the lazy dog..."

find_word(string, "over")
#=> "...fox jumped over the lazy..."

It's not perfect solution, just the path, so work around it.

它不是完美的解决方案,只是路径,所以围绕它工作。

#2


3  

Rails has a text helper called excerpt that does exactly this, so if you're trying to do this inside a Rails view:

Rails有一个叫做摘录的文本助手,它就是这样做的,所以如果你想在Rails视图中这么做的话:

excerpt('The quick brown fox jumped over the lazy dog',
        'jumped', :radius => 10)
=> "...brown fox jumped over the..."

If you want to use this outside Rails (but you have the Rails gems installed) you can load ActionView:

如果你想使用这个外部Rails(但是你已经安装了Rails gems),你可以加载ActionView:

require "action_view"
ActionView::Base.new.excerpt('The quick brown fox jumped over the lazy dog',
                                 'jumped', :radius => 10)

=> "...brown fox jumped over the..."