Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby和RegExp_Ruby_Regex - Fatal编程技术网

Ruby和RegExp

Ruby和RegExp,ruby,regex,Ruby,Regex,对不起,如果已经有人问过这个问题 我有大约一百万个包含在psql中的文本文档 我想看看它们是否包含某些单词,例如癌症、死亡或心脏病发作等。这个列表也很长 文档只需要包含其中一个单词 如果它们包含一个单词,我会尝试将它们复制到另一个文件夹中 我目前的代码是: directory = "disease" #Creates a directory called heart attacks FileUtils.mkpath(directory) # Makes the direc

对不起,如果已经有人问过这个问题

  • 我有大约一百万个包含在psql中的文本文档
  • 我想看看它们是否包含某些单词,例如癌症、死亡或心脏病发作等。这个列表也很长
  • 文档只需要包含其中一个单词
  • 如果它们包含一个单词,我会尝试将它们复制到另一个文件夹中
我目前的代码是:

  directory = "disease"     #Creates a directory called heart attacks
  FileUtils.mkpath(directory)   # Makes the directory if it doesn't exists

  cancer = Eightk.where("text ilike '%cancer%'")
  died = Eightk.where("text ilike '%died%'")

  cancer.each do |filing|   #filing can be used instead of eightks
  filename = "#{directory}/#{filing.doc_id}.html"
  File.open(filename,"w").puts filing.text
  puts "Storing #{filing.doc_id}..."


  died.each do |filing|     #filing can be used instead of eightks
  filename = "#{directory}/#{filing.doc_id}.html"
  File.open(filename,"w").puts filing.text
  puts "Storing #{filing.doc_id}..."

  end
结束

但这并不适用于以下情况

  • 与确切的单词不匹配

  • 这是非常耗时的,因为它包含许多处理相同代码和只更改一个单词的操作

因此,我尝试使用Regexp.union,如下所示,但有点不知所措

    directory = "disease"       #Creates a directory called heart attacks
    FileUtils.mkpath(directory)     # Makes the directory if it doesn't exists


    keywords = [/dead/,/killed/,/cancer/]

    re = regexp.union(keywords)
所以我试图在文本文件中搜索这些关键字,然后复制文本文档

非常感谢您的帮助

既然你说:

我有大约一百万个包含在psql中的文本文档

并使用“iLike”文本搜索操作符搜索这些文档中的单词

IMHO,这是一个低效的实现,因为您的数据很大,您的查询每次搜索都会处理100万个文本文档,而且速度非常慢

在继续之前,我认为你应该先看看PG。(如果您只是想在PG中使用内置的全文搜索),或者您也可以看看其他一些专门解决文本搜索问题的产品,如elasticsearch、solr等

关于PG全文搜索,在Ruby中,您可以使用gem。不过,如果您使用Rails,我将介绍Rails中PG的简单全文搜索实现

我希望你会觉得这很有用