我有一个相当巨大的json文件,其中包含剧本中的短行。我试图在json文件中将关键字与关键字进行匹配,这样我就可以从json中取出一行。Ruby在大json中搜索匹配
JSON文件的结构是这样的:
[
"Yeah, well I wasn't looking for a long term relationship. I was on TV. ",
"Ok, yeah, you guys got to put a negative spin on everything. ",
"No no I'm not ready, things are starting to happen. ",
"Ok, it's forgotten. ",
"Yeah, ok. ",
"Hey hey, whoa come on give me a hug... "
]
(加上其它更多... 2444行总)
到目前为止,我有这个,但它没有做任何的比赛。
# screenplay is read in from a json file
@screenplay_lines = JSON.parse(@jsonfile.read)
@text_to_find = ["relationship","negative","hug"]
@matching_results = []
@screenplay_lines.each do |line|
if line.match(Regexp.union(@text_to_find))
@matching_results << line
end
end
puts "found #{@matching_results.length} matches..."
puts @matching_results
我没有得到任何命中所以不知道什么是不工作的。另外我相信这是一个非常昂贵的过程,这样做有大量的数据。有任何想法吗?谢谢。
谢谢你。伟大的见解。 find1()方法工作正常 – matski