String Comparison

3

Problem: I have to get the object that displays the content in the English language (lang == en)

But depending on the search the order of the languages comes completely different and I tried to create a loop obj.take['http://dbpedia.org/resource/HTML']['http://www.w3.org/2000/01/rdf-schema#comment'].each where in the scope of this iteration I did a string comparison to obtain the content in the English language, but several problems arose and even trying other methods that I read on Stack En I could not, so I would be very grateful for an explanation. (I get too lost to handle json)

    obj.take['http://dbpedia.org/resource/HTML']['http://www.w3.org/2000/01/rdf-schema#comment'].each do |i|

        # Obj contém todo o arquivo json que está representado na imagem

        # if ... aqui

        # obj.take['http://dbpedia.org/resource/HTML']['http://www.w3.org/2000/01/rdf-schema#comment'][]['value']

    end
    
asked by anonymous 01.02.2017 / 02:20

2 answers

2

I do not know if I understood your question well, since in this JSON there is more than one object with the value of lang : en . Then I made the function below that recursively looks for all objects that have the lang key specified.

Returns an array with these objects.

def deep_find_lang_entry(obj,lang, results = [])

    obj.keys.each do |key|
        if obj[key].is_a? Hash
            deep_find_lang_entry(obj[key], lang, results)
        elsif obj[key].is_a? Array
            obj[key].each do |item|
                deep_find_lang_entry(item, lang, results) if item.is_a? Hash
            end
        else
            results.push(obj) if key.eql?("lang") && obj[key].eql?(lang)
        end
    end

    results
end

#procura por todos que possuem a chave 'lang' com valor 'en'
objs_with_lang_eng = deep_find_lang_entry(obj, "en")

puts objs_with_lang_eng
#[
#   {
#       "type":"literal",
#       "value":"Life",
#       "lang":"en"
#   },
#   {
#       "type":"literal",
#       "value":"Life is a characteristic... of life, although many other sciences are involved.",
#       "lang":"en"
#   }
#   #, ...more#
# ]
    
01.02.2017 / 05:09
2

The following is not an answer to your question, but I think it can be a valid contribution ... I'll use a json processor (command line): link .

1) Where is the dbpédia-Life page?

curl http://dbpedia.org/data/Life.json > a.json

2) What is the description of "Life"?

cat a.json | 
json '["http://dbpedia.org/resource/Life"]["http://www.w3.org/2000/01/rdf-schema#comment"]' > b.json

3) filter the English part and extract the value

cat b.json |  json  -c 'this.lang=="en"'  -a  value

Finally putting everything together:

curl http://dbpedia.org/data/Life.json |
  json '["http://dbpedia.org/resource/Life"]["http://www.....#comment"]' |
  json  -c 'this.lang=="en"'  -a value 

Now guess what it would be (bash):

for a in Rio_de_Janeiro Braga London Paris Prague
  do 
    curl -q http://dbpedia.org/data/$a.json |   
     json "['http://dbpedia.org/resource/$a']['http://www.w3.org/2000/01/rdf-schema#comment']" | 
     json  -c 'this.lang=="pt"' -a value
  done > cidades.txt
    
01.02.2017 / 12:36