如何停止此代码输出中的重复项。Ruby循环输出重复
RE = /<("[^"]*"|'[^']*'|[^'">])*>/
TAG_RE = /<(.+?)>(.*?)<.+?>/
text = "<date>show</date> me the current conditions for <city> detroit <END>"
a = []
text.scan(TAG_RE).map { |w| a<< w; }
text.gsub(RE, '').split.each do |q|
a.each_with_index do |v, i|
if q == a[i].last.strip
puts "#{q}\tB-#{a[i].first}"
else
puts "#{q}\tO"
end
end
end
产出
show B-date
show O
me O
me O
the O
the O
current O
current O
conditions O
conditions O
for O
for O
detroit O
detroit B-city
我只想字的单个实例,如果他们符合条件
喜欢这个
show B-date
me O
the O
current O
conditions O
for O
detroit B-city
我在哪里可以把next
的循环?
编辑
这是密码Rubyiotic?
text.gsub(RE, '').split.each do |q|
a.each_with_index do |v, i|
@a = a[i].last.strip # save in a variable
if @a == q
puts "#{q}\tB-#{a[i].first}"
break # break inner loop if match found
end
end
next if @a == q #skip current outer loop if match found
puts "#{q}\tO"
end
底特律应该结束''标签吗? –
这并不重要。它只是检查标签内的单词,然后从开始部分获取标签名称。 – arjun