String comparison

Asked

Viewed 122 times

2

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

inserir a descrição da imagem aqui

But depending on the search performed 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 get the content in the English language, but there were several problems and even trying other methods that I read in Stack En could not, so I would be very grateful for an eplication. ( I’m 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

2 answers

2


I don’t know if I understood your question correctly, since in this JSON there is more than one object with the value of lang: en. So I made the function below that looks recursively for all the objects that have the key lang 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#
# ]
  • Yay, it worked ( I only made a few changes ), thank you very much.

  • 1

    @You’re welcome! Good luck there!

2

The following is not an answer to your question, but I think it may be a valid contribution... I will use a json processor (command line): http://trentm.com/json/.

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

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 it all together:

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

gives the expected: "Life is a characteristic distinguishing Physical entities having Biological processes (such as Signaling and self-sustaining processes) from ....."

By the way guess what it would give (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
  • If you need to install: install node and then sudo npm install -g json

  • Super valid, thank you very much.

  • @Luissouza, thank you I.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.