In [1]:
%load_ext autoreload

%autoreload 2

In [2]:
from IPython.display import Audio

Import Modules


In [96]:
from services import load_credentials, get_database, get_watson_service, reset_services

In [97]:
vcap = load_credentials()

In [98]:
reset_services()

In [99]:
from watson_developer_cloud import LanguageTranslationV2, TextToSpeechV1, NaturalLanguageUnderstandingV1

Database


In [104]:
db, client = get_database('test')

In [106]:
client.all_dbs()


Out[106]:
['mydb', 'test']

In [107]:
[doc for doc in db]


Out[107]:
[]

In [108]:
db.all_docs(include_docs=True)


Out[108]:
{'offset': 0, 'rows': [], 'total_rows': 0}

In [7]:
db.create_document({'name' : 'Manolo'})
db.create_document({'name' : 'Pedro'})


Out[7]:
{'_id': 'c0cdf6be7e6aa5c805dc86c7f33ff736',
 '_rev': '1-3ff6cb90ca13159d9caa69e42917573c',
 'name': 'Pedro'}

In [8]:
list(map(lambda doc: doc['name'], db))


Out[8]:
['Manolo', 'Pedro']

In [83]:
db.delete()

Translator


In [10]:
translator = get_watson_service('language_translator')

In [11]:
translator.get_identifiable_languages()


Out[11]:
{'languages': [{'language': 'af', 'name': 'Afrikaans'},
  {'language': 'ar', 'name': 'Arabic'},
  {'language': 'az', 'name': 'Azerbaijani'},
  {'language': 'ba', 'name': 'Bashkir'},
  {'language': 'be', 'name': 'Belarusian'},
  {'language': 'bg', 'name': 'Bulgarian'},
  {'language': 'bn', 'name': 'Bengali'},
  {'language': 'bs', 'name': 'Bosnian'},
  {'language': 'cs', 'name': 'Czech'},
  {'language': 'cv', 'name': 'Chuvash'},
  {'language': 'da', 'name': 'Danish'},
  {'language': 'de', 'name': 'German'},
  {'language': 'el', 'name': 'Greek'},
  {'language': 'en', 'name': 'English'},
  {'language': 'eo', 'name': 'Esperanto'},
  {'language': 'es', 'name': 'Spanish'},
  {'language': 'et', 'name': 'Estonian'},
  {'language': 'eu', 'name': 'Basque'},
  {'language': 'fa', 'name': 'Persian'},
  {'language': 'fi', 'name': 'Finnish'},
  {'language': 'fr', 'name': 'French'},
  {'language': 'gu', 'name': 'Gujarati'},
  {'language': 'he', 'name': 'Hebrew'},
  {'language': 'hi', 'name': 'Hindi'},
  {'language': 'ht', 'name': 'Haitian'},
  {'language': 'hu', 'name': 'Hungarian'},
  {'language': 'hy', 'name': 'Armenian'},
  {'language': 'id', 'name': 'Indonesian'},
  {'language': 'is', 'name': 'Icelandic'},
  {'language': 'it', 'name': 'Italian'},
  {'language': 'ja', 'name': 'Japanese'},
  {'language': 'ka', 'name': 'Georgian'},
  {'language': 'kk', 'name': 'Kazakh'},
  {'language': 'km', 'name': 'Central Khmer'},
  {'language': 'ko', 'name': 'Korean'},
  {'language': 'ku', 'name': 'Kurdish'},
  {'language': 'ky', 'name': 'Kirghiz'},
  {'language': 'lt', 'name': 'Lithuanian'},
  {'language': 'lv', 'name': 'Latvian'},
  {'language': 'ml', 'name': 'Malayalam'},
  {'language': 'mn', 'name': 'Mongolian'},
  {'language': 'nb', 'name': 'Norwegian Bokmal'},
  {'language': 'nl', 'name': 'Dutch'},
  {'language': 'nn', 'name': 'Norwegian Nynorsk'},
  {'language': 'pa', 'name': 'Panjabi'},
  {'language': 'pl', 'name': 'Polish'},
  {'language': 'ps', 'name': 'Pushto'},
  {'language': 'pt', 'name': 'Portuguese'},
  {'language': 'ro', 'name': 'Romanian'},
  {'language': 'ru', 'name': 'Russian'},
  {'language': 'sk', 'name': 'Slovakian'},
  {'language': 'so', 'name': 'Somali'},
  {'language': 'sq', 'name': 'Albanian'},
  {'language': 'sv', 'name': 'Swedish'},
  {'language': 'ta', 'name': 'Tamil'},
  {'language': 'te', 'name': 'Telugu'},
  {'language': 'tr', 'name': 'Turkish'},
  {'language': 'uk', 'name': 'Ukrainian'},
  {'language': 'ur', 'name': 'Urdu'},
  {'language': 'vi', 'name': 'Vietnamese'},
  {'language': 'zh', 'name': 'Chinese'},
  {'language': 'zh-TW', 'name': 'Traditional Chinese'}]}

In [12]:
translator.identify('Je ne sais pas francais')['languages'][0]


Out[12]:
{'confidence': 0.997267, 'language': 'fr'}

In [13]:
s = translator.translate('I am very happy', source='en', target='ja')
s


Out[13]:
'とても幸せ '

In [14]:
translator.translate(s, source='ja', target='en')


Out[14]:
'I am very happy '

In [15]:
s = translator.translate('I am on top of the world', model_id='en-es-conversational')
s


Out[15]:
'Estoy en la cima del mundo '

In [16]:
translator.translate(s, model_id='es-en-conversational')


Out[16]:
"I'm on top of the world "

Text to Speech


In [17]:
text_to_speech = get_watson_service('text_to_speech')

In [18]:
audioEN = text_to_speech.synthesize('Hello world!', accept='audio/ogg',
voice="en-GB_KateVoice")

In [19]:
Audio(audioEN)


Out[19]:

In [20]:
[x['name'] for x in text_to_speech.voices()['voices']]


Out[20]:
['es-LA_SofiaVoice',
 'pt-BR_IsabelaVoice',
 'ja-JP_EmiVoice',
 'en-US_AllisonVoice',
 'fr-FR_ReneeVoice',
 'it-IT_FrancescaVoice',
 'es-ES_LauraVoice',
 'de-DE_BirgitVoice',
 'es-ES_EnriqueVoice',
 'de-DE_DieterVoice',
 'en-US_LisaVoice',
 'en-GB_KateVoice',
 'en-US_MichaelVoice',
 'es-US_SofiaVoice']

In [21]:
audioES = text_to_speech.synthesize('Hola Mundo', accept='audio/ogg',
voice="es-ES_LauraVoice")

In [22]:
Audio(audioES)


Out[22]:

Speech to Text


In [23]:
speech_to_text = get_watson_service('speech_to_text')

In [24]:
speech_to_text.recognize(
        audioEN, content_type='audio/ogg')


Out[24]:
{'result_index': 0,
 'results': [{'alternatives': [{'confidence': 0.996,
     'transcript': 'hello world '}],
   'final': True}]}

In [25]:
speech_to_text.recognize(
        audioEN, content_type='audio/ogg', timestamps=True,
        word_confidence=True)


Out[25]:
{'result_index': 0,
 'results': [{'alternatives': [{'confidence': 0.996,
     'timestamps': [['hello', 0.04, 0.31], ['world', 0.31, 0.85]],
     'transcript': 'hello world ',
     'word_confidence': [['hello', 1.0], ['world', 0.994]]}],
   'final': True}]}

In [26]:
[(x['language'], x['name']) for x in speech_to_text.models()['models'] ]


Out[26]:
[('fr-FR', 'fr-FR_BroadbandModel'),
 ('en-US', 'en-US_NarrowbandModel'),
 ('pt-BR', 'pt-BR_BroadbandModel'),
 ('en-UK', 'en-UK_BroadbandModel'),
 ('zh-CN', 'zh-CN_BroadbandModel'),
 ('ja-JP', 'ja-JP_BroadbandModel'),
 ('pt-BR', 'pt-BR_NarrowbandModel'),
 ('es-ES', 'es-ES_BroadbandModel'),
 ('ar-AR', 'ar-AR_BroadbandModel'),
 ('zh-CN', 'zh-CN_NarrowbandModel'),
 ('ja-JP', 'ja-JP_NarrowbandModel'),
 ('es-ES', 'es-ES_NarrowbandModel'),
 ('en-UK', 'en-UK_NarrowbandModel'),
 ('en-US', 'en-US_BroadbandModel')]

In [27]:
speech_to_text.recognize(
        audioES, content_type='audio/ogg', model='es-ES_BroadbandModel', timestamps=True,
        word_confidence=True, )


Out[27]:
{'result_index': 0,
 'results': [{'alternatives': [{'confidence': 0.998,
     'timestamps': [['hola', 0.03, 0.33], ['mundo', 0.33, 0.8]],
     'transcript': 'hola mundo ',
     'word_confidence': [['hola', 1.0], ['mundo', 0.996]]}],
   'final': True}]}

Natural Language Understanding


In [28]:
import watson_developer_cloud.natural_language_understanding.features.v1 as features

In [29]:
nlu = get_watson_service('natural-language-understanding')

In [30]:
nlu.analyze(text='this is my experimental text.  Bruce Banner is the Hulk and Bruce Wayne is BATMAN! Superman fears not Banner, but Wayne.',
            features=[features.Entities(), features.Keywords()])


Out[30]:
{'entities': [{'count': 3,
   'relevance': 0.915411,
   'text': 'Bruce Banner',
   'type': 'Person'},
  {'count': 1, 'relevance': 0.296395, 'text': 'Wayne', 'type': 'Person'}],
 'keywords': [{'relevance': 0.984789, 'text': 'Bruce Banner'},
  {'relevance': 0.958833, 'text': 'Bruce Wayne'},
  {'relevance': 0.853322, 'text': 'experimental text'},
  {'relevance': 0.627454, 'text': 'Hulk'},
  {'relevance': 0.619956, 'text': 'Superman'},
  {'relevance': 0.583188, 'text': 'BATMAN'}],
 'language': 'en'}

In [31]:
nlu.analyze(text='I love you', features=[features.Emotion(), features.Sentiment()], language='en')


Out[31]:
{'emotion': {'document': {'emotion': {'anger': 0.0,
    'disgust': 0.0,
    'fear': 0.0,
    'joy': 0.9,
    'sadness': 0.0}}},
 'language': 'en',
 'sentiment': {'document': {'label': 'positive', 'score': 0.839255}}}

In [32]:
nlu.analyze(text='Lucia odia a Margarita', features=[features.Entities(), features.Sentiment()], language='es')


Out[32]:
{'entities': [{'count': 1,
   'relevance': 0.963296,
   'text': 'Lucia',
   'type': 'Person'},
  {'count': 1, 'relevance': 0.247607, 'text': 'Margarita', 'type': 'Person'}],
 'language': 'es',
 'sentiment': {'document': {'label': 'negative', 'score': -0.924307}}}

In [33]:
text='One day, there was a ferret in the Wellness Room (at Petco, the Wellness Room is where animals who are ill or having any type of issue are kept in order to receive special attention and any medical care needed.) He was by himself and he dooked, a lot. “Dooking” is how ferrets “talk.” Cats meow. Dogs bark. Birds chirp. Ferrets dook. It almost sounds like a chicken clucking. In talking with the General Manager, we decided that the ferret was lonely, so I made it a point to go visit him in the Wellness Room whenever possible. This ferret had a skin infection and was receiving oral medications to help clear up the infection. Well, the day finally came when he finished all of his medication and was ready to join the others on the sales floor. However, I couldn’t give him up; we had bonded during our daily visits. I’d grown accustomed to him being there in the Wellness Room, waiting for me each morning; greeting me with enthusiasm that grew each day he got better. So, I bought him and named him Gizmo since his dooking reminded me of Gizmo the Gremlin. As I was getting ready to take Gizmo home, I happened to walk past the habitat on the sales floor (I’m sure I was just heading to the ladies room, yeah, that’s it). Well, low and behold, there was a single, little female ferret all by her lonesome. How could I pack up Gizmo and just walk past her and leave her by herself? Especially knowing that ferrets do better in pairs or multiples, especially when raised together or introduced correctly. Welcome Mink to the family.'

nlu.analyze(text=text, features=[features.Entities(), features.Keywords(), features.Sentiment(), 
                                 features.Emotion(), features.Relations(), features.Concepts()])


Out[33]:
{'concepts': [{'dbpedia_resource': 'http://dbpedia.org/resource/Medicine',
   'relevance': 0.943962,
   'text': 'Medicine'},
  {'dbpedia_resource': 'http://dbpedia.org/resource/Ferret',
   'relevance': 0.90459,
   'text': 'Ferret'},
  {'dbpedia_resource': 'http://dbpedia.org/resource/Mustelidae',
   'relevance': 0.833048,
   'text': 'Mustelidae'},
  {'dbpedia_resource': 'http://dbpedia.org/resource/Health_care',
   'relevance': 0.750068,
   'text': 'Health care'},
  {'dbpedia_resource': 'http://dbpedia.org/resource/Illness',
   'relevance': 0.678776,
   'text': 'Illness'},
  {'dbpedia_resource': 'http://dbpedia.org/resource/Carnivora',
   'relevance': 0.669715,
   'text': 'Carnivora'},
  {'dbpedia_resource': 'http://dbpedia.org/resource/Bird',
   'relevance': 0.611337,
   'text': 'Bird'},
  {'dbpedia_resource': 'http://dbpedia.org/resource/Pharmaceutical_drug',
   'relevance': 0.610033,
   'text': 'Pharmaceutical drug'}],
 'emotion': {'document': {'emotion': {'anger': 0.12589,
    'disgust': 0.120863,
    'fear': 0.136424,
    'joy': 0.613796,
    'sadness': 0.589325}}},
 'entities': [{'count': 2,
   'disambiguation': {'dbpedia_resource': 'http://dbpedia.org/resource/Skin_infection',
    'name': 'Skin infection',
    'subtype': []},
   'relevance': 0.912955,
   'text': 'skin infection',
   'type': 'HealthCondition'},
  {'count': 1, 'relevance': 0.583707, 'text': 'Mink', 'type': 'Person'},
  {'count': 1,
   'relevance': 0.576345,
   'text': 'General Manager',
   'type': 'JobTitle'},
  {'count': 1, 'relevance': 0.576345, 'text': 'One day', 'type': 'Quantity'}],
 'keywords': [{'relevance': 0.938985, 'text': 'Wellness Room'},
  {'relevance': 0.785899, 'text': 'little female ferret'},
  {'relevance': 0.759898, 'text': 'sales floor'},
  {'relevance': 0.665783, 'text': 'Gizmo home'},
  {'relevance': 0.644266, 'text': 'special attention'},
  {'relevance': 0.643426, 'text': 'medical care'},
  {'relevance': 0.636675, 'text': 'Cats meow'},
  {'relevance': 0.629607, 'text': 'chicken clucking'},
  {'relevance': 0.621038, 'text': 'daily visits'},
  {'relevance': 0.618607, 'text': 'General Manager'},
  {'relevance': 0.616606, 'text': 'oral medications'},
  {'relevance': 0.616594, 'text': 'skin infection'},
  {'relevance': 0.589167, 'text': 'ferrets'},
  {'relevance': 0.580846, 'text': 'ladies room'},
  {'relevance': 0.467606, 'text': 'Petco'},
  {'relevance': 0.45231, 'text': 'multiples'},
  {'relevance': 0.452062, 'text': 'Gremlin'},
  {'relevance': 0.451798, 'text': 'behold'},
  {'relevance': 0.449356, 'text': 'Mink'},
  {'relevance': 0.447822, 'text': 'animals'},
  {'relevance': 0.447711, 'text': 'type'},
  {'relevance': 0.447679, 'text': 'issue'},
  {'relevance': 0.447345, 'text': 'enthusiasm'},
  {'relevance': 0.447268, 'text': 'Dogs'},
  {'relevance': 0.446953, 'text': 'Birds'},
  {'relevance': 0.446447, 'text': 'pairs'},
  {'relevance': 0.444282, 'text': 'talk.'},
  {'relevance': 0.442427, 'text': 'point'},
  {'relevance': 0.440964, 'text': 'family'}],
 'language': 'en',
 'relations': [{'arguments': [{'entities': [{'text': 'General Manager',
       'type': 'Person'}],
     'text': 'General Manager'},
    {'entities': [{'text': 'talking', 'type': 'EventMeeting'}],
     'text': 'talking'}],
   'score': 0.735591,
   'sentence': 'In talking with the General Manager, we decided that the ferret was lonely, so I made it a point to go visit him in the Wellness Room whenever possible.',
   'type': 'participantIn'},
  {'arguments': [{'entities': [{'text': 'herself', 'type': 'Person'}],
     'text': 'me'},
    {'entities': [{'text': 'Gizmo', 'type': 'GeopoliticalEntity'}],
     'text': 'Gizmo'}],
   'score': 0.802773,
   'sentence': 'So, I bought him and named him Gizmo since his dooking reminded me of Gizmo the Gremlin.',
   'type': 'residesIn'},
  {'arguments': [{'entities': [{'text': 'Gizmo',
       'type': 'GeopoliticalEntity'}],
     'text': 'Gizmo'},
    {'entities': [{'text': 'Gremlin', 'type': 'GeopoliticalEntity'}],
     'text': 'Gremlin'}],
   'score': 0.79441,
   'sentence': 'So, I bought him and named him Gizmo since his dooking reminded me of Gizmo the Gremlin.',
   'type': 'locatedAt'},
  {'arguments': [{'entities': [{'text': 'herself', 'type': 'Person'}],
     'text': 'I'},
    {'entities': [{'text': 'home', 'type': 'Facility'}], 'text': 'home'}],
   'score': 0.362787,
   'sentence': "As I was getting ready to take Gizmo home, I happened to walk past the habitat on the sales floor (I'm sure I was just heading to the ladies room, yeah, that's it).",
   'type': 'locatedAt'},
  {'arguments': [{'entities': [{'text': 'home', 'type': 'Facility'}],
     'text': 'home'},
    {'entities': [{'text': 'Gizmo', 'type': 'GeopoliticalEntity'}],
     'text': 'Gizmo'}],
   'score': 0.951987,
   'sentence': "As I was getting ready to take Gizmo home, I happened to walk past the habitat on the sales floor (I'm sure I was just heading to the ladies room, yeah, that's it).",
   'type': 'locatedAt'}],
 'sentiment': {'document': {'label': 'positive', 'score': 0.321502}}}

Visual Recognition


In [34]:
visual_recognition = get_watson_service('watson_vision_combined')

In [35]:
url = "http://weknowyourdreams.com/images/volcano/volcano-05.jpg"
visual_recognition.classify(images_url=url)


Out[35]:
{'custom_classes': 0,
 'images': [{'classifiers': [{'classes': [{'class': 'volcano',
       'score': 1.0,
       'type_hierarchy': '/nature/volcano'},
      {'class': 'nature', 'score': 1.0},
      {'class': 'reddish orange color', 'score': 0.893},
      {'class': 'orange color', 'score': 0.711}],
     'classifier_id': 'default',
     'name': 'default'}],
   'resolved_url': 'http://weknowyourdreams.com/images/volcano/volcano-05.jpg',
   'source_url': 'http://weknowyourdreams.com/images/volcano/volcano-05.jpg'}],
 'images_processed': 1}

In [36]:
url="http://creator.keepcalmandcarryon.com/kcp-preview/NQOxtJXW"
visual_recognition.recognize_text(images_url=url)


Out[36]:
{'images': [{'resolved_url': 'http://creator.keepcalmandcarryon.com/kcp-preview/NQOxtJXW',
   'source_url': 'http://creator.keepcalmandcarryon.com/kcp-preview/NQOxtJXW',
   'text': 'keep\ncalm\nand\ncarry\non',
   'words': [{'line_number': 0,
     'location': {'height': 41, 'left': 69, 'top': 109, 'width': 132},
     'score': 0.9887,
     'word': 'keep'},
    {'line_number': 1,
     'location': {'height': 42, 'left': 54, 'top': 164, 'width': 160},
     'score': 0.9679,
     'word': 'calm'},
    {'line_number': 2,
     'location': {'height': 17, 'left': 110, 'top': 220, 'width': 50},
     'score': 0.9712,
     'word': 'and'},
    {'line_number': 3,
     'location': {'height': 82, 'left': 34, 'top': 231, 'width': 201},
     'score': 0.5648,
     'word': 'carry'},
    {'line_number': 4,
     'location': {'height': 49, 'left': 90, 'top': 305, 'width': 89},
     'score': 0.6411,
     'word': 'on'}]}],
 'images_processed': 1}

In [37]:
url = "https://fthmb.tqn.com/TbYcG1ZT3HMQNs2APucg94XyunE=/400x0/filters:no_upscale()/about/overlyattached-5900fa4a3df78c54563e3d8b.jpg"
visual_recognition.detect_faces(images_url=url)


Out[37]:
{'images': [{'faces': [{'age': {'max': 24, 'min': 18, 'score': 0.502411},
     'face_location': {'height': 203, 'left': 130, 'top': 104, 'width': 165},
     'gender': {'gender': 'FEMALE', 'score': 0.989013}}],
   'resolved_url': 'https://fthmb.tqn.com/TbYcG1ZT3HMQNs2APucg94XyunE=/400x0/filters:no_upscale()/about/overlyattached-5900fa4a3df78c54563e3d8b.jpg',
   'source_url': 'https://fthmb.tqn.com/TbYcG1ZT3HMQNs2APucg94XyunE=/400x0/filters:no_upscale()/about/overlyattached-5900fa4a3df78c54563e3d8b.jpg'}],
 'images_processed': 1}

In [ ]: