2015-04-29 40 views
0

我必須使用工具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.

回答

1

不要使用「 - 」在變量賦值,使用「_」,因爲否則的Python解釋爲一個減號並拋出'SyntaxError:不能分配給操作符'。還要確保您使用的是OpenCalais的最新API。試試這個簡單的測試:

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