<jupyter_start><jupyter_text>Alchemy API exercise- Team: [CognitiveBuild-TW](https://apps.na.collabserv.com/communities/service/html/communitystart?communityUuid=8e4d5ccf-5360-452b-8436-2fc1e649c348)- Author: <NAME>- Date: 2016/06/19pre {    background-color: f5f5f5;}.highlight {    background: f8f8f8;} Alchemy LanguageThis is part of AlchemyAPI, other family member include Speech, Vision, and Data Insights. Scope* [Keyword Extraction](http://www.alchemyapi.com/api/keyword-extraction) : Returns a list of topic keywords from the document* [Relation Extraction](http://www.alchemyapi.com/api/relation-extraction) : Identifies Subject-Action-Object relations in the text* Others.. API Reference:API 相關參考文件* [Alchemy@Bluemix](https://console.ng.bluemix.net/catalog/services/alchemyapi/)* [API doc](https://www.alchemyapi.com/api/keyword/htmlc.html), [API dev Doc](https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/alchemy-language/api/v1/?nodekeywords)* [Demo](http://www.alchemyapi.com/products/demo/alchemylanguage)* [SDK](https://github.com/watson-developer-cloud?utf8=%E2%9C%93&query=sdk): [Python](https://github.com/watson-developer-cloud/python-sdk), [Node.js](https://github.com/watson-developer-cloud/node-sdk)* **[This.API](https://github.com/watson-developer-cloud/python-sdk/blob/master/examples/alchemy_language_v1.py)** This Notebook Resource: 和本篇Notebook 有關的資源* [Community](https://apps.na.collabserv.com/communities/service/html/communitystart?communityUuid=8e4d5ccf-5360-452b-8436-2fc1e649c348)* [Docker](https://hub.docker.com/r/jessewei/jupyter_nodejs/)* Python learn[@Codecaemy](https://www.codecademy.com/learn/python),[@learnpython](http://www.learnpython.org/)* [Python Cheatsheet](http://www.astro.up.pt/~sousasag/Python_For_Astronomers/Python_qr.pdf)* [Markdown](https://help.github.com/articles/basic-writing-and-formatting-syntax/)* [FB Messager bot](http://tsaprailis.com/2016/06/02/How-to-build-and-deploy-a-Facebook-Messenger-bot-with-Python-and-Flask-a-tutorial)  Q&A - Update: Source from example have minor changed from 3.x -> 2.7 for this engineSYNTAX|3.x|2.7---|---|---print function/statement|print(x)|print x or print(x)keyword argument|relations(max_items=5, url=url)|relations(5, url)- Q: **Keyword API** issue, return status ok, but empty keyword list why?    - ANS: Parameter in examples, max_items=5, is not work. Fixed as below sample.- Q: Is the  tasklist@markdown not working on jupyter 3.1.0?    - ANS: No, test fail- Q: Is **Keyword API** support Chinese ?    - ANS: No, Only for English, German, French, Italian, Portuguese, Russian, Spanish and Swedish. [Ref to doc](https://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/doc/alchemylanguage/)- Q: Is **Relation API** support Chinese ?    - ANS: No, test fail    - Q: *Why **API** all fail rerun at morning ?*    - ANS: It is not your fault, caused by ....      Exercise purpose and procedure1. [Change to your apikey, replace the myAPIkey and myURL to yours](p1)2. [Follow steps in examples to run and review](p2)3. [Check out API doc, write our own samples](p3)4. [Review visual output](p4)5. Build an application, like Facebook Messager robot?Lab1, bar chart|Lab2, word cloud--|--![bc](../resources/barchart.png)|![wc](../resources/wordcloud.png)  1. Change to your apikey<jupyter_code># DEMO Program Entry Point


import json
from os.path import join, dirname
from watson_developer_cloud import AlchemyLanguageV1

#'ChangetoYourAPIkey'
myAPIkey = 'ChangetoYourAPIkey' 
#myURL = 'https://developer.ibm.com/watson/blog/2015/11/03/price-reduction-for-watson-personality-insights/'
myURL = 'http://www.washingtonpost.com/blogs/capital-weather-gang/wp/2013/08/14/d-c-area-forecast-ultra-nice-weather-dominates-next-few-days/'<jupyter_output><empty_output><jupyter_text>2. Follow steps in examples to run and review<jupyter_code># Find api_key after register your Bluemix AlchemyLanguage service

alchemy_language = AlchemyLanguageV1(api_key= myAPIkey)

# Set url to the content you want analysis
url = myURL
# follow examples, but skip indent for test only
print json.dumps(alchemy_language.targeted_sentiment(text='I love cats! Dogs are smelly.', targets=['cats', 'dogs'], language='english'))
# Keyword Extraction, follow examples
print(json.dumps(alchemy_language.keywords(2,url=url),  indent=2))
# Relation Extraction, follow examples
print(json.dumps(alchemy_language.relations(url=url), indent=2))<jupyter_output>{
  "status": "OK", 
  "usage": "By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html", 
  "relations": [
    {
      "action": {
        "text": "is", 
        "verb": {
          "text": "be", 
          "tense": "present"
        }, 
        "lemmatized": "be"
      }, 
      "subject": {
        "text": "A refreshing burst of Canadian coolness"
      }, 
      "object": {
        "text": "sweeping into our area"
      }, 
      "sentence": "A refreshing burst of Canadian coolness is now sweeping into our area, eradicating the heat and humidity of the past few days."
    }, 
    {
      "action": {
        "text": "sweeping", 
        "verb": {
          "text": "sweep", 
          "tense": "present"
        }, 
        "lemmatized": "sweep"
      }, 
      "subject": {
        "text": "A refreshing burst of Canadian coolness"
      }, 
      "sentence": "A refr[...]<jupyter_text>3. Check out API doc, write our own samples- Target: Plot top n keywords in bar or cloud. Keyword Extraction  ```alchemy_language.keywords(url=url)```**Keywords** - important topics in your content that are typically used when indexing data, generating tag clouds, or when searching. The AlchemyLanguage service automatically identifies supported languages (see the next bullet) in your input content, and then identifies and ranks keywords in that content. Sentiment can also be associated with each keyword by using the AlchemyLanguage stiment analysis capabilities.The argument _ **  maxLens ** not works _  Relation Extraction ```alchemy_language.relations(url=url)```**Relations** - identifies subject, action, and object relations within sentences in the input content. After parsing sentences into subject, action, and object form, the Relation Extraction API functions can use this information for subsequent processing by other AlchemyLanguage functions such as entity extraction, keyword extraction, sentiment analysis, and location identification. Relation information can be used to automatically identify buying signals, key events, and other important actions. Reference Library- [WordCloud](http://amueller.github.io/word_cloud/generated/wordcloud.WordCloud.htmlwordcloud.WordCloud.fit_words)- [matplotlib](http://matplotlib.org/)- [Plotly](https://plot.ly/ipython-notebooks/)<jupyter_code>rtn = alchemy_language.keywords(url=url)
keywords= rtn["keywords"]
print keywords[1:10]
rtn = alchemy_language.relations(url=url)
relations =  rtn["relations"]
print relations[1:5]
# Initial plot 
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np

# Show top 10 in plot
n = 20
X = np.arange(n)
# Convert string dict to float list
fixed_list = [x.items() for x in keywords]
vals,keys = zip(*fixed_list)
value = zip(*vals)
keyword = zip(*keys)
s = list((value[1][:n]))
Y=[float(i) for i in s]
#print keyword[1][1]<jupyter_output><empty_output><jupyter_text>4. Review visual output<jupyter_code># Plot
# 4. Review visual output
plt.bar(X, Y, facecolor='#9999ff', edgecolor='white')
for x,y in zip(X,Y):
    plt.text(x+0.4, -.2, keyword[1][x], ha='center', va= 'bottom', rotation='vertical')
#plt.ylim(0,+1)
plt.show()    
from wordcloud import WordCloud
wordcloud = WordCloud(background_color='white').generate_from_frequencies(zip(keyword[1], Y)) 
plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13505674653215638, 'fraction_numerical': 0.01828499369482976, 'mean_word_length': 4.751269035532995, 'pattern_counts': {'":': 23, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 15, 'lorem ipsum': 0, 'www.': 11, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '661554', 'license': 'Apache-2.0', 'n_tokens_mistral': 2717, 'n_tokens_neox': 2496, 'n_words': 659, 'path': 'files/nb_demo/watson/alchemy_language-py.ipynb', 'repo_name': 'jessewei/docker-tensorflow-watson', 'size': '7933'}