本文整理汇总了Python中alchemyapi.AlchemyAPI.concepts方法的典型用法代码示例。如果您正苦于以下问题:Python AlchemyAPI.concepts方法的具体用法?Python AlchemyAPI.concepts怎么用?Python AlchemyAPI.concepts使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类alchemyapi.AlchemyAPI
的用法示例。
在下文中一共展示了AlchemyAPI.concepts方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: extractConceptFromUrl
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import concepts [as 别名]
def extractConceptFromUrl(self, url):
"""method for extracting concepts from given url"""
# creating AlchemyAPI object
alchemyapi = AlchemyAPI()
# requesting json response from AlchemyAPI server
response = alchemyapi.concepts("url", url)
if response["status"] == "OK":
for concept in response["concepts"]:
# concept object for storing the extracted concept
conceptObj = AlchemyStructure.Concept()
# extracting the concept name
conceptObj.setText(concept["text"])
# extracting the relevance of the concept
conceptObj.setRelevance(concept["relevance"])
# append the concept into the list of retrieved concepts
self.conceptsFromUrl.append(conceptObj)
else:
print("Error in concept tagging call: ", response["statusInfo"])
示例2: main
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import concepts [as 别名]
def main():
alchemyapi = AlchemyAPI()
try:
filename = sys.argv[1]
except IndexError:
print "Give a filename as the second argument!"
sys.exit(1)
text = pdf_to_str(filename)
if len(text) >= LENGTH_LIMIT:
print "PDF content is longer ({} characters) than the maximum \
of {}, skipping remainder".format(len(text), LENGTH_LIMIT)
text = text[:LENGTH_LIMIT]
print "KEYWORDS"
response = alchemyapi.keywords('text', text)
for keyword in response['keywords']:
print ' - {}'.format(keyword['text'])
print
print "CONCEPTS"
response = alchemyapi.concepts('text', text)
for concept in response['concepts']:
print ' - {}'.format(concept['text'])
示例3: extractConceptFromUrl
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import concepts [as 别名]
def extractConceptFromUrl(self,url):
"""method for extracting concepts from given url"""
#creating AlchemyAPI object
alchemyapi = AlchemyAPI()
#requesting json response from AlchemyAPI server
response = alchemyapi.concepts('url',url)
if response['status'] == 'OK':
for concept in response['concepts']:
#concept object for storing the extracted concept
concept = AlchemyStructure.Concept()
#extracting the concept name
concept.setText(concept['text'])
#extracting the relevance of the concept
concept.setRelevance(concept['relevance'])
#append the concept into the list of retrieved concepts
self.conceptsFromText.append(concept)
else:
print('Error in concept tagging call: ', response['statusInfo'])
示例4: store_concepts
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import concepts [as 别名]
def store_concepts(tweets):
# Convert string array to string
all_tweets_as_string = ' '.join(tweets)
alchemyapi = AlchemyAPI()
alchemyapi.apikey = get_random_alchemy_credentials()
response = alchemyapi.concepts('text', all_tweets_as_string)
if response['status'] == 'OK':
for concept in response['concepts']:
concepts.append(concept['text'])
示例5: performCT
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import concepts [as 别名]
def performCT(url):
conceptText=[]
alchemyapi = AlchemyAPI()
response = alchemyapi.concepts('url', url)
if response['status'] == 'OK':
concepts = response['concepts']
for concept in concepts:
if (float(concept['relevance'])>0.1):
conceptText.append(concept['text'])
return conceptText
示例6: findConcept
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import concepts [as 别名]
def findConcept(text):
# Create the AlchemyAPI Object
alchemyapi = AlchemyAPI()
#print('############################################')
#print('# Concept Tagging retrieval #')
#print('############################################')
#print('')
print('')
print('Processing text: ', text)
print('')
response = alchemyapi.concepts('text', text)
return response
示例7: setKeywords
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import concepts [as 别名]
def setKeywords(self):
alchemyapi = AlchemyAPI()
response = alchemyapi.keywords('text',self.content, { 'sentiment':1 })
if response['status'] == 'OK':
for keyword in response['keywords']:
self.keywords.add(keyword['text'].encode('ascii','ignore'))
else:
print('Error in concept tagging call: ', response['statusInfo'])
self.keywords = set(["Automatic keyword generation failed"])
response = alchemyapi.concepts('text',self.content, { 'sentiment':1 })
if response['status'] == 'OK':
for keyword in response['concepts']:
self.keywords.add(keyword['text'].encode('ascii','ignore'))
else:
print('Error in concept tagging call: ', response['statusInfo'])
self.keywords = set(["Automatic keyword generation failed"])
示例8: generate_concepts_for_company
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import concepts [as 别名]
def generate_concepts_for_company(company_id, tweets):
all_tweets_as_string = ' '.join(tweets)
alchemyapi = AlchemyAPI()
api_error = False
for apikey in engine.get_random_alchemy_credentials():
alchemyapi.apikey = apikey
response = alchemyapi.concepts('text', all_tweets_as_string)
related_words = []
if response['status'] == 'OK':
for concept in response['concepts']:
related_words.append(concept['text'])
elif response['status'] == 'ERROR' and tweets != []:
print "ERROR getting concepts" + response['statusInfo']
api_error = True
# Move onto the next api key
continue
# Return null when all api keys are exhausted
if api_error and len(related_words) == 0:
return None
return related_words
示例9: open
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import concepts [as 别名]
for(dirpath, dirnames,filenames) in walk(out_txt_path):
txt_name.extend(filenames)
break
json_data = {}
entity_list = []
keywords_list = []
concept_list = []
for f in txt_name:
if f[-3:] == "txt":
full_text_path = out_txt_path + f
with open(full_text_path, 'r') as current_txt_file:
txt_data = current_txt_file.read().replace('\n','')
response_entities = alchemyapi.entities('text', txt_data)
response_keywords = alchemyapi.keywords('text', txt_data)
response_concepts = alchemyapi.concepts('text', txt_data)
if response_entities['status'] == 'OK' and response_keywords['status'] == 'OK':
print "status OK"
for entity in response_entities["entities"]:
dict_temp = {'entity': entity['text'],
'type': entity['type'],
'relevance': entity['relevance']}
entity_list.append(dict_temp)
for keyword in response_keywords["keywords"]:
dict_temp = {'keyword': keyword['text'],
'relevance': keyword['relevance']}
keywords_list.append(dict_temp)
for concept in response_concepts['concepts']:
dict_temp = {'concept': concept['text'],
'relevance': concept['relevance']}
concept_list.append(dict_temp)
示例10: Article
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import concepts [as 别名]
# CONTENT EXTRACTION FOR TRENDS
trend_url = 'http://www.philly.com/philly/blogs/things_to_do/Uber-will-deliver-kittens-to-your-office-in-honor-of-National-Cat-Day--.html';
trend_article = Article(trend_url)
trend_article.download()
trend_article.parse()
trend_title = trend_article.title
trend_text = trend_article.text
trend_tags = []
trend = alchemyapi.concepts('text', trend_text)
if trend['status'] == 'OK':
for concept in trend['concepts']:
if ' ' in concept['text'].encode('utf-8'):
split = concept['text'].encode('utf-8').split()
for c in split:
trend_tags.append(c)
else:
trend_tags.append(concept['text'].encode('utf-8'))
else:
print('Error in concept tagging call: ', trend['statusInfo'])
trend_db.insert_one({'keywords': trend_tags, 'title': trend_title})
print(trend_tags)
# CONTENT EXTRACTION FOR ARTICLES
示例11: print
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import concepts [as 别名]
if flag==1:
#response = json.loads(json.dumps(alchemyapi.sentiment("text", trans_text)))
###### GETTING AN ERROR HERE FOR SOME REASON ######
#senti=response["docSentiment"]["type"]
response = json.loads(json.dumps(alchemyapi.keywords('text', trans_text, {'sentiment': 1})))
#size=len(response['keywords'])
keywords=[]
if response['status'] == 'OK':
for word in response['keywords']:
keywords.append(word['text'])
else:
print('Error in entity extraction call: ', response['statusInfo'])
response=json.loads(json.dumps(alchemyapi.concepts("text",trans_text)))
#size=len(response['concepts'])
concept=[]
if response['status'] == 'OK':
for con in response['concepts']:
concept.append(con['text'])
else:
print('Error in entity extraction call: ', response['statusInfo'])
tweet_data['entities']=ent
tweet_data['ent_relevance']=ent_rele
tweet_data['ent_type']=ent_type
tweet_data['keywords']=keywords
tweet_data['concepts']=concept
tweet_data['sentiment']=senti
hashtagData = tweet.entities.get('hashtags')
示例12: print
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import concepts [as 别名]
print("Error in keyword extaction call: ", response["statusInfo"])
print("")
print("")
print("")
print("############################################")
print("# Concept Tagging Example #")
print("############################################")
print("")
print("")
print("Processing text: ", demo_text)
print("")
response = alchemyapi.concepts("text", demo_text)
if response["status"] == "OK":
print("## Object ##")
print(json.dumps(response, indent=4))
print("")
print("## Concepts ##")
for concept in response["concepts"]:
print("text: ", concept["text"])
print("relevance: ", concept["relevance"])
print("")
else:
print("Error in concept tagging call: ", response["statusInfo"])
示例13: __init__
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import concepts [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
#.........这里部分代码省略.........
示例14: user_analysis_sentiments
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import concepts [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")
示例15: extract_nouns
# 需要导入模块: from alchemyapi import AlchemyAPI [as 别名]
# 或者: from alchemyapi.AlchemyAPI import concepts [as 别名]
def extract_nouns(text):
alchemyapi = AlchemyAPI()
response = alchemyapi.concepts("text", text)
return str(response)