本文整理汇总了Python中alchemyapi.AlchemyAPI.relations方法的典型用法代码示例。如果您正苦于以下问题:Python AlchemyAPI.relations方法的具体用法?Python AlchemyAPI.relations怎么用?Python AlchemyAPI.relations使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类alchemyapi.AlchemyAPI
的用法示例。
在下文中一共展示了AlchemyAPI.relations方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: relExtract
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import relations [as 别名]
def relExtract(company, summary):
alchemyapi = AlchemyAPI()
response = alchemyapi.relations('text', summary)
for relation in response['relations']:
if 'subject' in relation:
if company.lower().strip() in relation['subject']['text'].encode('utf-8').lower():
return classify(relation['object']['text'].encode('utf-8').lower())
break
示例2: trigger
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import relations [as 别名]
def trigger(self, text):
print('Request: {0}'.format(text))
try:
alchemyapi = AlchemyAPI()
except Exception as e:
print('Could not connect to AlchemyAPI. Details: {}'.format(e))
relations = alchemyapi.relations('text', text)
action = None # Action to take. Only 'Play' implemented so far.
format = None # In pratice 'album' or 'track'.
toPlay = None # Album or track name.
if relations['status'] == 'OK':
print('## Object ##')
print(json.dumps(relations, indent=4))
for relation in relations['relations']:
if 'action' in relation:
print('Action: ', relation['action']['text'].encode('utf-8'))
if relation['action']['verb']['text'] in ('play', 'listen'):
action = 'PLAY'
else:
print('Error in relation extaction call: ', relations['statusInfo'])
# If no action found, we abort.
if not action:
print('Could not find any action to take.')
return
# Detect the artist to play
artists = []
artist = None
keywords = alchemyapi.keywords('text', text, {'sentiment': 1})
if keywords['status'] == 'OK':
print('## Response Object ##')
print(json.dumps(keywords, indent=4))
for keyword in keywords['keywords']:
artists.append((keyword['relevance'], extractArtists(keyword['text'].encode('utf-8'))))
else:
print('Error in keyword extaction call: ', keywords['statusInfo'])
print('Action: {0}'.format(action))
print('Artists extracted from the request: {0}'.format(' '.join([str(n)+'('+c+')' for c,n in artists])))
for e in artists:
if not e[1]:
continue
else:
artist = e[1][-1]
break
if not artist:
print('Could not find any artist.')
return
print('Selected artist: {0}'.format(artist))
print('To simplify I assume you are looking for an album and not a track.')
albums = AudioDB_getAlbums(artist)
if not albums:
print('Could not find any album for {0}'.format(artist))
return
album = None
for name, item in albums.iteritems():
if name in text:
album = item
break
if not album:
print('Could not find any specific album, so will try to extract a track from the full discography')
for _, album in albums.iteritems():
tracks = AudioDB_getTracks(album['id'])
for track in tracks:
if track in text:
format = 'song'
toPlay = track
break
if not toPlay:
format = 'album'
toPlay = albums.itervalues().next()['name'] # We assume the artist has at least one album
print('Could not find any album or track so I will play the album {0}'.format(toPlay))
else:
print('Selected album is {0}. Now, check the tracks.'.format(album))
tracks = AudioDB_getTracks(album['id'])
for track in tracks:
if track in text:
format = 'song'
toPlay = track
break
if not toPlay:
print("Could not find a track, so I will play the whole album.")
toPlay = album['name']
format = 'album'
else:
print('The song to play will be {0}'.format(toPlay))
print('Selected album/track: {0}'.format(toPlay))
hint = 'album' if format == 'album' else ''
args = {'q': ' '.join([toPlay, artist, hint]),
#.........这里部分代码省略.........
示例3: print
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import relations [as 别名]
print('')
print('')
print('')
print('############################################')
print('# Relation Extraction Example #')
print('############################################')
print('')
print('')
print('Processing text: ', demo_text)
print('')
response = alchemyapi.relations('text',demo_text)
if response['status'] == 'OK':
print('## Object ##')
print(json.dumps(response, indent=4))
print('')
print('## Relations ##')
for relation in response['relations']:
if 'subject' in relation:
print('Subject: ', relation['subject']['text'].encode('utf-8'))
if 'action' in relation:
print('Action: ', relation['action']['text'].encode('utf-8'))
示例4: print
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import relations [as 别名]
#Title
print('Checking title . . . ')
response = alchemyapi.title('text', test_text);
assert(response['status'] == 'ERROR') #only works for html and url content
response = alchemyapi.title('html', test_html);
assert(response['status'] == 'OK')
response = alchemyapi.title('url', test_url);
assert(response['status'] == 'OK')
print('Title tests complete!')
print('')
#Relations
print('Checking relations . . . ')
response = alchemyapi.relations('text', test_text);
assert(response['status'] == 'OK')
response = alchemyapi.relations('html', test_html);
assert(response['status'] == 'OK')
response = alchemyapi.relations('url', test_url);
assert(response['status'] == 'OK')
response = alchemyapi.relations('random', test_url);
assert(response['status'] == 'ERROR') #invalid flavor
print('Relation tests complete!')
print('')
#Category
print('Checking category . . . ')
response = alchemyapi.category('text', test_text);
示例5: AlchemyAPI
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import relations [as 别名]
from alchemyapi import AlchemyAPI
alchemyapi = AlchemyAPI()
entities = {}
mytext = open('mytext.txt','r')
mytext = mytext.read()
response1 = alchemyapi.entities("text",mytext)
response = alchemyapi.keywords("text",mytext)
response3 = alchemyapi.relations("text",mytext)
en1 = response1['entities'][0]['text']
en2 = response1['entities'][1]['text']
key1 = response['keywords'][0]['text']
key2 = response['keywords'][1]['text']
key3 = response['keywords'][2]['text']
key4 = response['keywords'][3]['text']
key5 = response['keywords'][4]['text']
key6 = response['keywords'][5]['text']
示例6: print
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import relations [as 别名]
print("Error in title extraction call: ", response["statusInfo"])
print("")
print("")
print("")
print("############################################")
print("# Relation Extraction Example #")
print("############################################")
print("")
print("")
print("Processing text: ", demo_text)
print("")
response = alchemyapi.relations("text", demo_text)
if response["status"] == "OK":
print("## Object ##")
print(json.dumps(response, indent=4))
print("")
print("## Relations ##")
for relation in response["relations"]:
if "subject" in relation:
print("Subject: ", relation["subject"]["text"].encode("utf-8"))
if "action" in relation:
print("Action: ", relation["action"]["text"].encode("utf-8"))
if "object" in relation:
示例7: createGraph
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import relations [as 别名]
def createGraph(ac_results):
alchemyapi = AlchemyAPI()
g=nx.Graph()
total = 0.0
i = 0.0
for key in sorted(ac_results.iterkeys()):
total += len(ac_results[key])
for key in sorted(ac_results.iterkeys()):
print "\nAlchemyAPI is now intepreting all of the "+key+" queries...\n"
for item in ac_results[key]:
i +=1.0
percent_complete = round((i/total)*100.0, 0)
# print str(i) +" / "+str(total)+" - "+item
print str(int(i)) +" / "+str(int(total))+" "+str(percent_complete) +"% " + item
response_relations = alchemyapi.relations('text',item, {'entities':1, 'sentiment':1})
response_entities = alchemyapi.entities('text',item, { 'sentiment':0 })
if response_relations['status'] == 'OK':
for relation in response_relations['relations']:
# red.publish('chat', "found relation!")
if 'subject' in relation:
subject = relation['subject']['text']
g.add_node(subject, query=key)
if 'entities' in relation['subject']:
g.node[subject]['type'] = relation['subject']['entities'][0]['type']
if 'sentimentFromObject' in relation['subject']:
# print relation['subject']['sentimentFromObject']['score']
g.node[subject]['sentiment'] = float(relation['subject']['sentimentFromObject']['score'])
if 'sentiment' in relation['subject']:
# print relation['subject']['sentiment']['score']
g.node[subject]['sentiment'] = float(relation['subject']['sentiment']['score'])
if 'object' in relation:
object_ = relation['object']['text']
g.add_node(object_, query=key)
if 'entities' in relation['object']:
g.node[object_]['type'] = relation['object']['entities'][0]['type']
if 'sentimentFromSubject' in relation['object']:
# print relation['object']['sentimentFromSubject']['score']
g.node[object_]['sentiment'] = float(relation['object']['sentimentFromSubject']['score'] )
if 'sentiment' in relation['object']:
# print relation['object']['sentiment']['score']
g.node[object_]['sentiment'] = float(relation['object']['sentiment']['score'])
try:
if all(x in ['subject', 'action', 'object'] for x in relation):
n1 = relation['subject']['text']
a = relation['action']['text']
n2 = relation['object']['text']
if g.has_edge(n1,n2):
g[n1][n2]['weight'] += 1
else:
g.add_edge(n1,n2, weight=1, relation=a)
except:
pass
try:
for entity in response_entities['entities']:
g.add_node(entity['text'], type=entity['type'], query=key)
except:
continue
nx.write_gexf(g, topic+".gexf")
else:
print "AlchemyAPI is not responding."
return g
示例8: __init__
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import relations [as 别名]
class AlchemyPost:
def __init__(self, post_tumblr, post_id, consumer_key, consumer_secret, oauth_token, oauth_secret):
self.post_tumblr = post_tumblr
self.post_id = post_id
self._init_tumblr(consumer_key, consumer_secret, oauth_token, oauth_secret)
self._init_alchemy()
def _init_tumblr(self, consumer_key, consumer_secret, oauth_token, oauth_secret):
self._client = pytumblr.TumblrRestClient(consumer_key, consumer_secret, oauth_token, oauth_secret)
def _init_alchemy(self):
self.alchemyapi = AlchemyAPI()
self.content = {}
def analyze_post(self):
self.post = self._get_content_post()
self._alchemy_entities()
self._alchemy_keywords()
self._alchemy_concepts()
self._alchemy_sentiment()
self._alchemy_relations()
self._alchemy_category()
self._alchemy_feeds()
self._alchemy_taxonomy()
def print_content(self):
print(json.dumps(self.content, indent=4))
def _get_content_post(self):
print "*",
infos = self._get_infos_post()
self.title = ''
self.tags = []
if 'tags' in infos:
self.tags = infos['tags']
if infos['type'] == 'text':
return self._get_content_text(infos)
if infos['type'] == 'quote':
return self._get_content_quote(infos)
return ''
def _get_infos_post(self):
infos = self._client.posts(self.post_tumblr, id=self.post_id)
if 'posts' in infos and len(infos['posts'])>0:
return infos['posts'][0]
return {}
def _get_content_text(self, infos):
content = "<h1>" + str(infos['title']) + "</h1>"
content += " <br>" + str(infos['body'])
content += " <br>" + " ".join(infos['tags'])
return content
def _get_content_quote(self, infos):
content = str(infos['text'])
content += " <br>" + str(infos['source'])
content += " <br>" + " ".join(infos['tags'])
return content
def _alchemy_entities(self):
print ".",
response = self.alchemyapi.entities('html', self.post)
if response['status'] != 'OK':
return False
self.content['entities'] = response['entities']
return True
def _alchemy_keywords(self):
print ".",
response = self.alchemyapi.keywords('html', self.post)
if response['status'] != 'OK':
return False
self.content['keywords'] = response['keywords']
return True
def _alchemy_concepts(self):
print ".",
response = self.alchemyapi.concepts('html', self.post)
if response['status'] != 'OK':
return False
self.content['concepts'] = response['concepts']
return True
def _alchemy_sentiment(self):
print ".",
response = self.alchemyapi.sentiment('html', self.post)
if response['status'] != 'OK':
return False
self.content['sentiment'] = response['docSentiment']
return True
def _alchemy_relations(self):
print ".",
response = self.alchemyapi.relations('html', self.post)
if response['status'] != 'OK':
return False
self.content['relations'] = response['relations']
return True
#.........这里部分代码省略.........
示例9: user_analysis_sentiments
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import relations [as 别名]
def user_analysis_sentiments(request):
if request.method == 'GET':
print request.GET.get('user', '')
user = request.GET.get('user', '')
messages = []
message = Message.objects.filter(user_send=user.decode("utf8"))
for m in message:
messages.append(m.message_text)
text = ",".join(messages)
alchemyapi = AlchemyAPI()
#keywords
response = alchemyapi.keywords('text', text, {'sentiment': 1})
if response['status'] == 'OK':
keywords = []
for keyword in response['keywords']:
keyword_text = keyword['text'].encode('utf-8')
keyword_relevance = keyword['relevance']
keyword_sentiment = keyword['sentiment']['type']
key_word = {'keyword_text': keyword_text, 'keyword_relevance': keyword_relevance,
'keyword_sentiment': keyword_sentiment}
keywords.append(key_word)
else:
print('Error in keyword extaction call: ', response['statusInfo'])
response = alchemyapi.concepts('text', text)
if response['status'] == 'OK':
concepts = []
for concept in response['concepts']:
concept_text = concept['text']
concept_relevance = concept['relevance']
concept_entity = {'concept_text': concept_text, 'concept_relevance': concept_relevance}
concepts.append(concept_entity)
else:
print('Error in concept tagging call: ', response['statusInfo'])
response = alchemyapi.language('text', text)
if response['status'] == 'OK':
print(response['wikipedia'])
language = response['language']
iso_639_1 = response['iso-639-1']
native_speakers = response['native-speakers']
wikipedia = response['wikipedia']
language_id = {'language': language, 'iso_639_1': iso_639_1, 'native_speakers': native_speakers, 'wikipedia': wikipedia}
else:
print('Error in language detection call: ', response['statusInfo'])
response = alchemyapi.relations('text', text)
if response['status'] == 'OK':
relations = []
for relation in response['relations']:
if 'subject' in relation:
relation_subject_text = relation['subject']['text'].encode('utf-8')
if 'action' in relation:
relation_action_text = relation['action']['text'].encode('utf-8')
if 'object' in relation:
relation_object_text = relation['object']['text'].encode('utf-8')
relation_entity = {'relation_subject_text': relation_subject_text,
'relation_action_text': relation_action_text,
'relation_object_text': relation_object_text}
relations.append(relation_entity)
else:
print('Error in relation extaction call: ', response['statusInfo'])
response = alchemyapi.category('text', text)
if response['status'] == 'OK':
print('text: ', response['category'])
category = response['category']
print('score: ', response['score'])
score = response['score']
categories = {'category': category, 'score': score}
else:
print('Error in text categorization call: ', response['statusInfo'])
response = alchemyapi.taxonomy('text', text)
if response['status'] == 'OK':
taxonomies = []
for category in response['taxonomy']:
taxonomy_label = category['label']
taxonomy_score = category['score']
taxonomy = {'taxonomy_label': taxonomy_label, 'taxonomy_score': taxonomy_score}
taxonomies.append(taxonomy)
else:
print('Error in taxonomy call: ', response['statusInfo'])
response = alchemyapi.combined('text', text)
if response['status'] == 'OK':
print('## Response Object ##')
print(json.dumps(response, indent=4))
print('')
user = {'user_name': 'LOL', 'keywords': keywords, 'concepts': concepts, 'language_id': language_id,
'relations': relations, 'categories': categories, 'taxonomies': taxonomies}
return HttpResponse(json.dumps(user), content_type="application/json")