使用 opencalais 提取推文实体

extracting entities of tweets using opencalais

我必须使用工具 opencalais 从推文中提取实体。 我的代码是:

# 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

我收到以下错误:

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

不要在变量赋值中使用“-”,使用“_”,否则 Python 会将其解释为减号并抛出“SyntaxError: can't assign to operator”。还要确保您使用的是 OpenCalais 的最新 API。试试这个简单的测试:

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