このコード出力の重複をどのように停止できますか?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
OUTPUTS
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
彼らは私がループでnext
を置くことができ、この
show B-date
me O
the O
current O
conditions O
for O
detroit B-city
同様の条件
に一致する場合、私は言葉だけの単一のインスタンスをしたいですか?
編集
このコードは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