当前位置: 首页>>代码示例>>Python>>正文


Python alchemyapi.AlchemyAPI类代码示例

本文整理汇总了Python中alchemyapi.AlchemyAPI的典型用法代码示例。如果您正苦于以下问题:Python AlchemyAPI类的具体用法?Python AlchemyAPI怎么用?Python AlchemyAPI使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了AlchemyAPI类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: AnalyzeSentiment

def AnalyzeSentiment(searchTerm):
    analysisAPI = AlchemyAPI()
    pos, neg, neu = (0,0,0)
    dataCollection = database_connection(searchTerm)
    dataDocuments = dataCollection.find()
    tweets = []
    sentimentByCountry = {}
    tweetLocation = ""
    for document in dataDocuments:
        try:
            if document.get("sentiment", None) == None:
                analysisResponse = analysisAPI.sentiment("text", document["text"])
                documentSentiment = analysisResponse["docSentiment"]["type"]
                dataCollection.update_one({"_id":document["_id"]}, {"$set": {"sentiment": analysisResponse["docSentiment"]}})
            else:
                documentSentiment = document["sentiment"]["type"]

            if documentSentiment == "positive":
                pos=pos+1
            elif documentSentiment == "negative":
                neg=neg+1
            else:
                neu=neu+1

            tweets.append(document["text"].strip()+"\n\n***Tweet-Sentiment: "+documentSentiment+"***\n"+"-"*70)
        except:
            print("Unable to parse a Tweet as the language is not understood\n")
            dataCollection.delete_one({'text':document['text']})
    return pos,neg,neu,tweets
开发者ID:saurabhpatil8989,项目名称:TwiSense,代码行数:29,代码来源:sentiment_analyzer.py

示例2: function

def function():
    string = sys.stdin.readline()
    alchemyapi = AlchemyAPI()
    myText = "I'm excited to get started with AlchemyAPI!"
    response = alchemyapi.sentiment("text", myText)
    string = "Sentiment: " + response["docSentiment"]["type"]
    print string
开发者ID:hannavas,项目名称:youtube-comment-analyzer,代码行数:7,代码来源:comments.py

示例3: extractKeywordsFromUrl

    def extractKeywordsFromUrl(self,url):
        """method for extracting keywords from given text"""
        
        #creating AlchemyAPI object
        alchemyapi = AlchemyAPI()
              
        #requesting json response from AlchemyAPI server
        response = alchemyapi.keywords('url', url)
        
        if response['status'] == 'OK':
    

            for keywords in response['keywords']:
                
                #concept object for storing the extracted concept
                keyword = AlchemyStructure.Keyword()
                
                #extracting the keyword
                keyword.setText(keywords['text'])
                
                #extracting the relevance of keyword
                keyword.setRelevance(keywords['relevance'])
                
                #append the concept into the list of retrieved concepts
                self.keywordsFromUrl.append(keyword)

        else:
            print('Error in keyword tagging call: ', response['statusInfo'])
开发者ID:B-Rich,项目名称:Fem-Coding-Challenge,代码行数:28,代码来源:ProcessAlchemy.py

示例4: sentiment_alchemy

def sentiment_alchemy(url):
    alchemyapi = AlchemyAPI()

    response = alchemyapi.sentiment('url', url)
    response['usage'] = None

    if response['status'] == 'OK':
        print('## Response Object ##')
        print(json.dumps(response, indent=4))

        print('')
        print('## Author ##')
        print('author: ', response.get('author', ''))
        print('')
    else:
        print('Error in author extraction call: ', response['statusInfo'])

    response = alchemyapi.keywords('url', url)
    del (response['usage'])

    if response['status'] == 'OK':
        print('## Response Object ##')
        print(json.dumps(response, indent=4))

        print('')
        print('## Keywords ##')
        for keyword in response['keywords']:
            print('text: ', keyword['text'].encode('utf-8'))
            print('relevance: ', keyword['relevance'])
            print('sentiment: ', keyword.get('sentiment', {}).get('type', ''))
            if 'score' in keyword.get('sentiment', {}):
                print('sentiment score: ' + keyword['sentiment']['score'])
            print('')
        else:
            print('Error in keyword extaction call: ', response.get('statusInfo', ''))
开发者ID:EHDEV,项目名称:market_sentimentalism2,代码行数:35,代码来源:old.py

示例5: performSA

def performSA(pname, text):
    alchemyapi = AlchemyAPI()
    response = alchemyapi.sentiment('text', text)
    sentiment = response['docSentiment']
    if (sentiment['type']=='neutral'):
        sentiment['score']='0'
    return sentiment
开发者ID:yallapragada,项目名称:social_persona,代码行数:7,代码来源:persona.py

示例6: extractSentimentFromUrl

    def extractSentimentFromUrl(self, url):
        """method for extracting the sentiment associated with the url of a document"""

        # creating AlchemyAPI object
        alchemyapi = AlchemyAPI()

        # requesting json response from AlchemyAPI server
        response = alchemyapi.sentiment("url", url)

        if response["status"] == "OK":

            # getting the sentiment type from the response
            sentimentType = response["docSentiment"]["type"]

            # checking the sentiment type
            if sentimentType == "neutral":
                sentimentScore = 0
            else:
                sentimentScore = response["docSentiment"]["score"]

            # instantiating sentiment object
            self.sentimentFromUrl = AlchemyStructure.Sentiment()

            # set the value for sentiment type
            self.sentimentFromUrl.setType(sentimentType)

            # set the value for sentiment score
            self.sentimentFromUrl.setScore(sentimentScore)

        else:
            print("Error in sentiment analysis call: ", response["statusInfo"])
开发者ID:B-Rich,项目名称:pyAlchemy,代码行数:31,代码来源:ProcessAlchemy.py

示例7: user_list_sentiments

def user_list_sentiments(request):
    """
    This function lists all users
    :param request: GET request from front end
    :return: list of all users
    """
    if request.method == 'GET':
        users = []
        user = User.objects.all()
        # docSentimentscore = 1
        for u in user:
            messages = []
            message = Message.objects.filter(user_send=u.user_name)
            for m in message:
                messages.append(m.message_text)
            text = ",".join(messages)
            alchemyapi = AlchemyAPI()
            response = alchemyapi.sentiment('text', text)
            if response["status"] == "OK":
                if response["docSentiment"]["type"] == "neutral":
                    docSentimentscore = 0
                else:
                    docSentimentscore = response["docSentiment"]["score"]
            usr = {'user_name': u.user_name, 'user_sentiment': docSentimentscore}
            users.append(usr)
        print(json.dumps(users))
        return HttpResponse(json.dumps(users), content_type="application/json")
开发者ID:pranav93,项目名称:Text-mining,代码行数:27,代码来源:views.py

示例8: extractConceptFromUrl

    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"])
开发者ID:B-Rich,项目名称:pyAlchemy,代码行数:28,代码来源:ProcessAlchemy.py

示例9: getKeywords

def getKeywords(uID, inputText):
    alchemyapi = AlchemyAPI()
    #alchemyapi.loadAPIKey("api_key.txt")
    response = alchemyapi.keywords('text',inputText)
    print inputText
    if response['status'] == 'OK':
        #print('## Response Object ##')
        #print(json.dumps(response, indent=4))


        #print('')
        #print('## Keywords ##')
        keywords = []
        posts = uID + " : "
        for keyword in response['keywords']:
                keywords.append(keyword['text'])
                posts = posts + keyword['text'] + "|"
        userKeywords[uID] = keywords
        posts = posts + "\n"
        with codecs.open("outNew.txt", "a") as f:
            f.write(posts.encode("UTF-8"))
        return True
    else:
        print('idError: ', uID)
        with codecs.open("keywordOut2.txt", "a") as f:
            text = uID + "\n"
            f.write(text.encode("UTF-8"))
        return False
开发者ID:marcelocra,项目名称:googleplussearch,代码行数:28,代码来源:alchemyCall.py

示例10: getKeywordPerPost

def getKeywordPerPost():
    reader = open('output_sony_posts.txt')
    all_json_posts = reader.read().splitlines()
    alchemyapi = AlchemyAPI()
    counter = 0
    for p in all_json_posts:
        print str(counter)
        if counter < 1000:
            counter = counter + 1
            continue
        #elif counter > 2000:
        #    break
        else:
            counter = counter + 1
        content = json.loads(p)["cleanContent"]   
        response = alchemyapi.keywords('text',content.encode("UTF-8"))
    
        if response['status'] == 'OK':
            keywords = []
            posts = ""
            for keyword in response['keywords']:
                    keywords.append(keyword['text'])
                    posts = posts + keyword['text'] + ","
            posts = posts[:-1] + "\n"
            if posts <> "\n":
                with codecs.open("keyPerPost.txt", "a") as f:
                    f.write(posts.encode("UTF-8"))
        else:
            print "error" + str(counter)
开发者ID:marcelocra,项目名称:googleplussearch,代码行数:29,代码来源:alchemyCall.py

示例11: _extract_content_alchemy

 def _extract_content_alchemy(self, url):
     alchemyapi = AlchemyAPI()
     response = alchemyapi.text('url', url)
     content = ''
     if response['status'] == 'OK':
         content = response['text'].encode('utf8')
     return content
开发者ID:t-mai,项目名称:KeywordExplorer,代码行数:7,代码来源:scraper.py

示例12: render_article

def render_article(request):	
	#if current aricle has content field
	#render as is
	#else call alchemy and save content

	article_id = request.POST['articleData']
	article = Article.objects.filter(id = article_id)[0]

	print(article_id.encode('utf-8'))
	print(article.content.encode('utf-8'))
	if article.content:
		return render_to_response('article.html', {'id' : article.id, 'data' : article.content, 'titleText' : article.title})
	else:				
		testURL = article.url
		#Create AlchemyAPI Object
		alchemyapi = AlchemyAPI()
		response = alchemyapi.text('url', testURL)
		titleData = alchemyapi.title('url', testURL)
		authorData = alchemyapi.author('url', testURL)
		article.content = response['text'].encode('utf-8')
		article.title = titleData['title'].encode('utf-8')
		article.save()

		return render_to_response('article.html', {'id' : article.id, 'data' : response['text'].encode('utf-8'), 'titleText' : titleData['title'].encode('utf-8')}
 )
开发者ID:JoshuaKGoldberg,项目名称:StacheIt,代码行数:25,代码来源:views.py

示例13: run_sentiment_analysis

def run_sentiment_analysis(tweets, text_key):
    def print_error(response):
        # This should be replaced with better logging
        print('Error with AlchemyAPI response:')
        print(sentiment, '\n')

    alchemyapi = AlchemyAPI()
    results = []
    for item in tweets:
        if text_key not in item:
            # Assume it's a bad tweet and continue
            print(text_key, 'not found in tweet')
            continue
        sentiment = alchemyapi.sentiment('text', item['words'])
        try:
            if sentiment['status'].lower() == 'error':
                # Unrecognized language, emoji only, etc...
                print_error(sentiment)
            # Make a deep copy (since it's a nested dictionary)
            new_item = copy.deepcopy(item)
            sentiment_type = sentiment['docSentiment']['type']
            new_item['sentiment_type'] = sentiment_type
            if sentiment_type == 'neutral':
                new_item['sentiment_score'] = 0
            else:
                new_item['sentiment_score'] = sentiment['docSentiment']['score']
            results.append(new_item)
        except Exception as ex:
            print(type(ex).__name__)
            print_error(sentiment)

    return results
开发者ID:shohs,项目名称:twitter-election-sentiment,代码行数:32,代码来源:sentiment.py

示例14: getCategory

def getCategory(demo_text):
	alchemyapi = AlchemyAPI()
	demo_text = unicode(demo_text);
	demo_text = demo_text.encode("ascii",'ignore');
	response = alchemyapi.entities('text', demo_text)

	if response['status'] == 'OK':
		#print(json.dumps(response, indent=4))
		if (not len(response['entities'])):
			category = []
			category.append("Undefined")
			return category

		entity = response['entities'][0]
		#print('text: ', entity['text'].encode('utf-8'))
		#print('type: ', entity['type'])
		#print('relevance: ', entity['relevance'])

		if entity.has_key('disambiguated') and entity['disambiguated'].has_key('subType'):
			category = entity['disambiguated']['subType']
		else:
			category = []
			category.append(entity['type'])
	else:
		category = []
		category.append("Undefined")
	return category
开发者ID:blee42,项目名称:testwiki,代码行数:27,代码来源:alchemy_category.py

示例15: Entity_Extraction

    def Entity_Extraction(self):

        print " ----------------------------"
        print "# STARTING ENTITY EXTRACTION:"
        print " ----------------------------"

        count = 0

        os.system("python templates/alchemyapi.py 32449e7b4f6b65f9ef5cfd84b7128a46440a9402")

        startTime = datetime.now()
        # Create the AlchemyAPI Object
        alchemyapi = AlchemyAPI()
        for paragraph in self.targeted_paragraphs:
            response = alchemyapi.entities('text', paragraph, {'sentiment': 1})

            if response['status'] == 'OK':

                print "DOCUMENT-LEVEL RESULTS:  "

                print "ARTICLE TITLE: " , self.article_title[len(self.article_title) - len(self.article_title) + count]
                print 'ARTICLE URL: ' , self.article_url[len(self.article_url) - len(self.article_url) + count]
                print "DATA FRAME: "
                count = count + 1

                for entity in response['entities']:

                    entity_text = entity['text']
                    entity_type = entity['type']
                    entity_relevance = entity['relevance']
                    entity_sentiment_type = entity['sentiment']['type']

                    if 'score' in entity['sentiment']:
                        entity_sentiment_score = entity['sentiment']['score']

                    df_entity_extraction = pd.DataFrame(data = {'text': [entity_text],
                                                         'type': [entity_type],
                                                         'relevance': [entity_relevance],
                                                         'sentiment': [entity_sentiment_type],
                                                         'sentiment_score': [entity_sentiment_score]})

                    print "***** ENTITY EXTRACTION RESULTS: *****"
                    print df_entity_extraction.T
                    df_transpose = df_entity_extraction.T


                    entity_json_results = df_transpose.to_dict() #######
                    self.result_entity_extraction.append(entity_json_results)

                else:
                    pass

            else:
                print 'Error in entity extraction call: ', response['statusInfo']

        print "----------- Entity Extraction is completed. ---------------"

        print "Time Elapsed: " , datetime.now() - startTime
        execution_time = datetime.now() - startTime
        self.list_time_elapsed.append(execution_time)
开发者ID:AlpArhan,项目名称:emontio,代码行数:60,代码来源:emontio_main_web.py


注:本文中的alchemyapi.AlchemyAPI类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。