Ejemplo n.º 1
0
def analyze_audio():
    print(request.json['texto'])

    ''' Parte para programar y traducir al inglés '''
    language_translator = LanguageTranslator(
        version='2018-03-16',
        iam_api_key='XmyHrVcLnTgWC3Ou33zGB989tcrOxocykZeZDUJxdlP6',
        url='https://gateway.watsonplatform.net/language-translator/api')

    translation = language_translator.translate(
        text=request.json['texto'],
        model_id='es-en')

    ''' Parte para sacar insights del texto '''
    natural_language_understanding = NaturalLanguageUnderstandingV1(
      username='******',
      password='******',
      version='2018-03-16')

    response = natural_language_understanding.analyze(
      text=json.loads(json.dumps(translation, indent=2, ensure_ascii=False))["translations"][0]["translation"],
      features=Features(
        entities=EntitiesOptions(
          emotion=True,
          sentiment=True,
          limit=2),
        keywords=KeywordsOptions(
          emotion=True,
          sentiment=True,
          limit=2)))

    return jsonify(json.dumps(response, indent=2))
Ejemplo n.º 2
0
def translate(text_to_translate):
    language_translation = LanguageTranslation(
        version='2018-05-01',
        iam_apikey='0W0AoUw-0Yxg11L5DmNPFPbwMM7De0vT-5IeTyrES-co')

    translation = language_translation.translate(text=text_to_translate,
                                                 source='en',
                                                 target='fr')

    # return translation['result']
    return translation.result['translations'][0]['translation']
Ejemplo n.º 3
0
def post_list(request):
    posts = Post.objects.filter(
        published_date__lte=timezone.now()).order_by('published_date')
    tone_analyzer = ToneAnalyzerV3(
        username='******',
        password='******',
        version='2016-05-19 ')

    language_translator = LanguageTranslator(
        version='2018-07-22',
        iam_api_key='GQKCZYnTvRuyF7ETgKfqCrHfmDqi1b7JDsghX76zyYKO')

    # print(json.dumps(translation, indent=2, ensure_ascii=False))

    for post in posts:
        posting = post.text
        toneObj = json.dumps(tone_analyzer.tone(tone_input=posting,
                                                content_type="text/plain"),
                             indent=2)
        post.toneObj2 = json.loads(toneObj)
        post.angerScore = post.toneObj2['document_tone']['tone_categories'][0][
            'tones'][0]['score']
        post.disgustScore = post.toneObj2['document_tone']['tone_categories'][
            0]['tones'][1]['score']
        post.fearScore = post.toneObj2['document_tone']['tone_categories'][0][
            'tones'][2]['score']
        post.joyScore = post.toneObj2['document_tone']['tone_categories'][0][
            'tones'][3]['score']
        post.sadScore = post.toneObj2['document_tone']['tone_categories'][0][
            'tones'][4]['score']

        translation = language_translator.translate(text=post.text,
                                                    source='en',
                                                    target='es')
        obj = json.dumps(translation, indent=2, ensure_ascii=False)
        post.obj2 = json.loads(obj)

        post.translation = post.obj2['translations'][0]['translation']
        post.wordCount = post.obj2['word_count']
        post.characterCount = post.obj2['character_count']

    return render(request, 'blog/post_list.html', {'posts': posts})
def get_response_from_language_translator(version, iam_apikey, url, word):

    language_translator = LanguageTranslator(
        version = version,
        iam_apikey = iam_apikey,
        url= url
    )

    try:
        translation = language_translator.translate(
            text=str(word),
            model_id='tr-en').get_result()
        #print(json.dumps(translation, indent=2, ensure_ascii=False))

        # Returned answer string manupulation
        j = json.dumps(translation, indent=2)
        out_json = json.loads(j)
        output = out_json['translations'][0]['translation']  # *** check here ***
        
        return output

    except WatsonApiException as ex:
        return "Method failed with status code " + str(ex.code) + ": " + ex.message
from watson_developer_cloud import LanguageTranslatorV3 as LanguageTranslator

language_translator = LanguageTranslator(version='2018-12-03',
                                         iam_apikey={},
                                         url={})
# translation = language_translator.translate(
#     text='Hello',
#     model_id='en-ja').get_result()
# print(json.dumps(translation, indent=2, ensure_ascii=False))
num = 0
while (num < 1):
    text = input('和英翻訳するよ。日本語か英語を入力してね。\n')
    # 言語識別
    identify = language_translator.identify(text)
    language = identify.get_result()
    source = language['languages'][0]['language']
    if source == 'en':
        target = 'ja'
    elif source == 'ja':
        target = 'en'
    else:
        source = 'ja'
        target = 'en'
    # 翻訳
    translation = language_translator.translate(text=text,
                                                source=source,
                                                target=target)
    print(translation.result['translations'][0]['translation'])
    print()
    num += 1
Ejemplo n.º 6
0
def post_list(request):
    posts = Post.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    tone_analyzer = ToneAnalyzerV3(
        username='', #get your own API key and password from IBM Watson
        password='',
        version='2016-05-19')

    natural_language_understanding = NaturalLanguageUnderstandingV1(
        version='2018-07-30',
        username='', #get your own API key and password from IBM Watson
        password='')

    language_translator = LanguageTranslator(
        version='2018-07-22',
        iam_api_key='' #get your API key from IBM Watson
    )

    # print(json.dumps(translation, indent=2, ensure_ascii=False))

    for post in posts:
        posting = post.text
        posting = post.text
        toneObj = json.dumps(tone_analyzer.tone(tone_input=posting,
                                                content_type="text/plain"), indent=2)
        post.toneObj2 = json.loads(toneObj)
        post.angerScore = post.toneObj2['document_tone']['tone_categories'][0]['tones'][0]['score']
        post.disgustScore = post.toneObj2['document_tone']['tone_categories'][0]['tones'][1]['score']
        post.fearScore = post.toneObj2['document_tone']['tone_categories'][0]['tones'][2]['score']
        post.joyScore = post.toneObj2['document_tone']['tone_categories'][0]['tones'][3]['score']
        post.sadScore = post.toneObj2['document_tone']['tone_categories'][0]['tones'][4]['score']

        translation = language_translator.translate(
            text=post.text,
            source='en',
            target='es')

        translationFrench = language_translator.translate(
            text=post.text,
            source='en',
            target='fr')
        obj = json.dumps(translation, indent=2, ensure_ascii=False)
        obj2 = json.dumps(translationFrench, indent=2, ensure_ascii=False)
        post.obj = json.loads(obj)
        post.obj2 = json.loads(obj2)

        post.translationFrench = post.obj2['translations'][0]['translation']
        post.translation = post.obj['translations'][0]['translation']
        post.wordCount = post.obj2['word_count']
        post.characterCount = post.obj2['character_count']

        response = natural_language_understanding.analyze(
            text=posting,
            features=Features(
                entities=EntitiesOptions(emotion=True, sentiment=True, limit=2),
                keywords=KeywordsOptions(
                    emotion=True,
                    sentiment=True,
                    limit=2)))

        obj3 = (json.dumps(response, indent=2))
        post.obj3 = json.loads(obj3)
        post.entities = post.obj3['entities']
        post.name = None
        """if ('disambiguation' in post.entities[0]):
                post.name = post.entities[0]['disambiguation']['name']
                post.link = post.entities[0]['disambiguation']['dbpedia_resource']
        else:
            post.name = post.entities[1]['disambiguation']['name']
            post.link = post.entities[1]['disambiguation']['dbpedia_resource']"""
    return render(request, 'blog/post_list.html', {'posts': posts})
Ejemplo n.º 7
0
def post_detail(request, pk):
    post = get_object_or_404(Post, pk=pk)

    tone_analyzer = ToneAnalyzerV3(
        username='', #get your own API key and password from IBM Watson
        password='',
        version='2016-05-19 ')

    natural_language_understanding = NaturalLanguageUnderstandingV1(
        version='', #get your own API key and password from IBM Watson
        username='',
        password='******')

    language_translator = LanguageTranslator(
        version='', #get your own API key and password from IBM Watson
        iam_api_key='GQKCZYnTvRuyF7ETgKfqCrHfmDqi1b7JDsghX76zyYKO'
    )
    posting = post.text
    toneObj = json.dumps(tone_analyzer.tone(tone_input=posting,
                                            content_type="text/plain"), indent=2)
    post.toneObj2 = json.loads(toneObj)
    post.angerScore = post.toneObj2['document_tone']['tone_categories'][0]['tones'][0]['score']
    post.disgustScore = post.toneObj2['document_tone']['tone_categories'][0]['tones'][1]['score']
    post.fearScore = post.toneObj2['document_tone']['tone_categories'][0]['tones'][2]['score']
    post.joyScore = post.toneObj2['document_tone']['tone_categories'][0]['tones'][3]['score']
    post.sadScore = post.toneObj2['document_tone']['tone_categories'][0]['tones'][4]['score']

    translation = language_translator.translate(
        text=posting,
        source='en',
        target='es')

    translationFrench = language_translator.translate(
        text=posting,
        source='en',
        target='fr')
    obj = json.dumps(translation, indent=2, ensure_ascii=False)
    obj2 = json.dumps(translationFrench, indent=2, ensure_ascii=False)
    post.obj = json.loads(obj)
    post.obj2 = json.loads(obj2)

    post.translationFrench = post.obj2['translations'][0]['translation']
    post.translation = post.obj['translations'][0]['translation']
    post.wordCount = post.obj2['word_count']
    post.characterCount = post.obj2['character_count']

    response = natural_language_understanding.analyze(
        text=posting,
        features=Features(
            entities=EntitiesOptions(emotion=True, sentiment=True, limit=2),
            keywords=KeywordsOptions(
                emotion=True,
                sentiment=True,
                limit=2)))

    obj3 = (json.dumps(response, indent=2))
    post.obj3 = json.loads(obj3)
    post.entities = post.obj3['entities']
    post.length = len(post.entities)
    post.name = "IBM Watson cannot detect the country's name"
    if 'disambiguation' in post.entities[0]:
        post.disambiguation = post.entities[0]['disambiguation']
        if 'name' in post.disambiguation:
            post.name = post.disambiguation['name']
            post.link = post.disambiguation['dbpedia_resource']
    else:
        if 'disambiguation' in post.entities[1]:
            post.disambiguation = post.entities[1]['disambiguation']
            if 'name' in post.disambiguation:
                post.name = post.disambiguation['name']
                post.link = post.disambiguation['dbpedia_resource']
    # Post.objects.get(pk=pk)
    return render(request, 'blog/post_detail.html', {'post': post})
Ejemplo n.º 8
0
def post_list(request):
    posts = Post.objects.filter(
        published_date__lte=timezone.now()).order_by('published_date')
    tone_analyzer = ToneAnalyzerV3(
        username='******',
        password='******',
        version='2016-05-19 ')

    natural_language_understanding = NaturalLanguageUnderstandingV1(
        version='2018-07-30',
        username='******',
        password='******')

    language_translator = LanguageTranslator(
        version='2018-07-22',
        iam_api_key='GQKCZYnTvRuyF7ETgKfqCrHfmDqi1b7JDsghX76zyYKO')

    # print(json.dumps(translation, indent=2, ensure_ascii=False))

    for post in posts:
        posting = post.text
        toneObj = json.dumps(tone_analyzer.tone(tone_input=posting,
                                                content_type="text/plain"),
                             indent=2)
        post.toneObj2 = json.loads(toneObj)
        post.angerScore = post.toneObj2['document_tone']['tone_categories'][0][
            'tones'][0]['score']
        post.disgustScore = post.toneObj2['document_tone']['tone_categories'][
            0]['tones'][1]['score']
        post.fearScore = post.toneObj2['document_tone']['tone_categories'][0][
            'tones'][2]['score']
        post.joyScore = post.toneObj2['document_tone']['tone_categories'][0][
            'tones'][3]['score']
        post.sadScore = post.toneObj2['document_tone']['tone_categories'][0][
            'tones'][4]['score']

        translation = language_translator.translate(text=post.text,
                                                    source='en',
                                                    target='es')

        translationFrench = language_translator.translate(text=post.text,
                                                          source='en',
                                                          target='fr')
        obj = json.dumps(translation, indent=2, ensure_ascii=False)
        obj2 = json.dumps(translationFrench, indent=2, ensure_ascii=False)
        post.obj = json.loads(obj)
        post.obj2 = json.loads(obj2)

        post.translationFrench = post.obj2['translations'][0]['translation']
        post.translation = post.obj['translations'][0]['translation']
        post.wordCount = post.obj2['word_count']
        post.characterCount = post.obj2['character_count']

        response = natural_language_understanding.analyze(
            text=posting,
            features=Features(entities=EntitiesOptions(emotion=True,
                                                       sentiment=True,
                                                       limit=2),
                              keywords=KeywordsOptions(emotion=True,
                                                       sentiment=True,
                                                       limit=2)))

        obj3 = (json.dumps(response, indent=2))
        post.obj3 = json.loads(obj3)
        post.entities = post.obj3['entities']
        post.length = len(post.entities)
        post.name = None
        if ('disambiguation' in post.entities[0]):
            post.name = post.entities[0]['disambiguation']['name']
            post.link = post.entities[0]['disambiguation']['dbpedia_resource']
        else:
            post.name = post.entities[1]['disambiguation']['name']
            post.link = post.entities[1]['disambiguation']['dbpedia_resource']
    return render(request, 'blog/post_list.html', {'posts': posts})