Search code examples
pythonopencalais

extracting entities of tweets using opencalais


I have to extract entities from tweets using tool opencalais. My code is:

# this code is based on: http://www.flagonwiththedragon.com/2011/06/08/dead-simple-python-calls-to-open-calais-api/

import urllib, urllib2

##### set API key and REST URL values.

x-ag-access-token1 = 'O7tTcXv6TFHA4Z5EKjjxPcrcdWndxl' # your Calais API key.
calaisREST_URL = 'https://api.thomsonreuters.com/permid/Calais' # this is the older REST interface.
# info on the newer one: http://www.opencalais.com/documentation/calais-web-service-api/api-invocation/rest

# alert user and shut down if the API key variable is still null.
if x-ag-access-token1 == '':
  print "You need to set your Calais API key in the 'x-ag-access-token' variable."
 import sys
 sys.exit()


 ##### set the text to ask Calais to analyze.

# text from: http://www.usatoday.com/sports/football/nfl/story/2012-03-22/Tim-Tebow-Jets-hoping-to-avoid-controversy/53717542/1
sampleText = '''
Like millions of football fans, Tim Tebow caught a few training camp glimpses of the New York Jets during the summer of 2010 on HBO's Hard Knocks.
'''

##### set XML parameters for Calais.

# see "Input Parameters" at: http://www.opencalais.com/documentation/calais-web-service-api/forming-api-calls/input-parameters
calaisParams = '''
<c:params xmlns:c="http://s.opencalais.com/1/pred/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
 <c:processingDirectives c:contentType="text/txt"
  c:enableMetadataType="GenericRelations,SocialTags"
  c:outputFormat="Text/Simple"/>
 <c:userDirectives/>
 <c:externalMetadata/>
</c:params>
'''

#########################
##### send data to Calais API.

# see: http://www.opencalais.com/APICalls
dataToSend = urllib.urlencode({
   'x-ag-access-token': x-ag-access-token1,
   'content': sampleText,
   'paramsXML': calaisParams
})

##### get API results and print them.

results = urllib2.urlopen(calaisREST_URL, dataToSend).read()
print results

I am getting the following error:

x-ag-access-token1 = 'O7tTcXv6TFHA4Z5EKjjxPcrcdWndxl' # your Calais API key. SyntaxError: can't assign to operator. The open calais has changed its new API.


Solution

  • Don't use '-' in variable assignment, use '_' because otherwise Python interprets it as a minus sign and throws the 'SyntaxError: can't assign to operator' . Also make sure you are using the latest API from OpenCalais. Try this simple test:

    >>> my-var = 'hello world'
    File "<stdin>", line 1
    SyntaxError: can't assign to operator