ubergrape / pyspotlight

A thin wrapper around the DBPedia Spotlight REST API
BSD 2-Clause "Simplified" License
58 stars 25 forks source link

Candidates response fails on _dict_cleanup #3

Closed aolieman closed 11 years ago

aolieman commented 11 years ago

Hey,

Thanks for writing this nice wrapper. It makes Spotlight calls a lot more convenient!

I'm running into a problem with the candidates response, seemingly only when exactly two candidate URIs are found for a surface form.

>>> text = "Industrial Design at the Technische Universiteit Delft"
>>> compare.throughSpotlight(text, candidate_param)

Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    compare.throughSpotlight(text, candidate_param)
  File "...\git-code\compare.py", line 44, in throughSpotlight
    spotlight_response = api(text)
  File "build\bdist.win32\egg\spotlight\__init__.py", line 206, in candidates
    for form in pydict['annotation']['surfaceForm']]
  File "build\bdist.win32\egg\spotlight\__init__.py", line 60, in _dict_cleanup
    for key, value in dic.iteritems():
AttributeError: 'unicode' object has no attribute 'iteritems'

With curl:

$ curl -H "accept: application/json" http://spotlight.sztaki.hu:2222/rest/candidates --data "text=Industrial Design at the Technische Universiteit Delft&confidence=0&support=0"
{
   "annotation":{
      "@text":"Industrial Design at the Technische Universiteit Delft",
      "surfaceForm":{
         "@name":"Technische Universiteit Delft",
         "@offset":"25",
         "resource":[
            {
               "@label":"Technische Universiteit Delft",
               "@uri":"Technische_Universiteit_Delft",
               "@contextualScore":"0.9991813164782087",
               "@percentageOfSecondRank":"0.1422872887244497",
               "@support":"3",
               "@priorScore":"2.8799662606192636E-8",
               "@finalScore":"0.8754365122251001",
               "@types":""
            },
            {
               "@label":"Delft University of Technology",
               "@uri":"Delft_University_of_Technology",
               "@contextualScore":"8.186418452925803E-4",
               "@percentageOfSecondRank":"0.0",
               "@support":"521",
               "@priorScore":"5.001541405942121E-6",
               "@finalScore":"0.12456348777489806",
               "@types":"DBpedia:Agent, Schema:Organization, DBpedia:Organisation, Schema:EducationalOrganization, DBpedia:EducationalInstitution, Schema:CollegeOrUniversity, DBpedia:University"
            }
         ]
      }
   }
}

I'm not entirely sure what the conditions are under which _dict_cleanup() fails, but I have only found examples with exactly two candidate URIs. If needed, I can provide you with some more examples.

Any idea what is causing this hiccup?

Cheers, Alex

originell commented 11 years ago

May I ask which version if spotlight you are running?

aolieman commented 11 years ago

Of course you may :-)!

Locally I'm fairly certain the version is 0.6.5 and at http://spotlight.sztaki.hu:2222 it should also be 0.6.5. The responses from my local server and the sztaki.hu server are the same.

Doesn't the issue reproduce with your server?

originell commented 11 years ago

Haha no, or at least it was not above our notification threshold oops.

I have a fix which I'm pushing in the next 15 minutes. That happens because in candidates we are assuming that it's a list, which it does not have to be, as your case demonstrates :)

aolieman commented 11 years ago

That's great, I'll try it right away! Especially great since your fix is faster than me trying to bypass _dict_cleanup() alltogether ;-)

originell commented 11 years ago

So that should be fixed now! Would not have been that fast if you would not have supplied the raw curl response :) Thanks for that. Hope it works? Waiting for your confirmation!

aolieman commented 11 years ago

It works like a charm =)! Many thanks for your fast (and late-night) bugfix; I wasn't expecting that!

originell commented 11 years ago

Glad to hear that! Well, I'm recovering a RAID5, so if you encounter any other error tonight, chances are high that I'll be awake and able to fix it ;-)

Have fun with spotlight!