首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >利用开放源码提取tweet实体

利用开放源码提取tweet实体
EN

Stack Overflow用户
提问于 2015-04-29 08:11:52
回答 1查看 789关注 0票数 0

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

代码语言:javascript
复制
# 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访问令牌1= 'O7tTcXv6TFHA4Z5EKjjxPcrcdWndxl‘#您的加来API密钥。SyntaxError:不能分配给操作符。开放的加莱公司已经改变了新的API。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-04-29 09:36:52

不要在变量赋值中使用'-‘,使用'_’,因为否则Python会将它解释为减号,并抛出'SyntaxError:不能赋值给操作符‘。还要确保您正在使用来自OpenCalais的最新API。试试这个简单的测试:

代码语言:javascript
复制
>>> my-var = 'hello world'
File "<stdin>", line 1
SyntaxError: can't assign to operator
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29937930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档