使用opencalais提取推文的实体

时间:2015-04-29 08:11:53

标签: python 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'#您的Calais API密钥。 SyntaxError:无法分配给运算符。开放的加莱已经改变了它的新API。

1 个答案:

答案 0 :(得分:1)

不要使用&#39; - &#39;在变量赋值中,使用&#39; _&#39;因为否则Python将其解释为减号并抛出&#39; SyntaxError:无法分配给运算符&#39; 。还要确保您使用的是OpenCalais的最新API。试试这个简单的测试:

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