|
| 1 | +import os |
| 2 | + |
| 3 | +from azure.cognitiveservices.language.textanalytics import TextAnalyticsAPI |
| 4 | +from msrest.authentication import CognitiveServicesCredentials |
| 5 | + |
| 6 | +SUBSCRIPTION_KEY_ENV_NAME = "TEXTANALYTICS_SUBSCRIPTION_KEY" |
| 7 | +TEXTANALYTICS_LOCATION = os.environ.get("TEXTANALYTICS_LOCATION", "westcentralus") |
| 8 | + |
| 9 | +def language_extraction(subscription_key): |
| 10 | + """Language extraction. |
| 11 | +
|
| 12 | + This will detect the language of a few strings. |
| 13 | + """ |
| 14 | + client = TextAnalyticsAPI(TEXTANALYTICS_LOCATION, CognitiveServicesCredentials(subscription_key)) |
| 15 | + |
| 16 | + try: |
| 17 | + documents = [{ |
| 18 | + 'id': 1, |
| 19 | + 'text': 'This is a document written in English.' |
| 20 | + }, { |
| 21 | + 'id': 2, |
| 22 | + 'text': 'Este es un document escrito en Español.' |
| 23 | + }, { |
| 24 | + 'id': 3, |
| 25 | + 'text': '这是一个用中文写的文件' |
| 26 | + }] |
| 27 | + for document in documents: |
| 28 | + print("Asking language detection on '{}' (id: {})".format(document['text'], document['id'])) |
| 29 | + response = client.detect_language( |
| 30 | + documents=documents |
| 31 | + ) |
| 32 | + |
| 33 | + for document in response.documents: |
| 34 | + print("Found out that {} is {}".format(document.id, document.detected_languages[0].name)) |
| 35 | + |
| 36 | + except Exception as err: |
| 37 | + print("Encountered exception. {}".format(err)) |
| 38 | + |
| 39 | +def key_phrases(subscription_key): |
| 40 | + """Key-phrases. |
| 41 | +
|
| 42 | + The API returns a list of strings denoting the key talking points in the input text. |
| 43 | + """ |
| 44 | + client = TextAnalyticsAPI(TEXTANALYTICS_LOCATION, CognitiveServicesCredentials(subscription_key)) |
| 45 | + |
| 46 | + try: |
| 47 | + documents = [{ |
| 48 | + 'language': 'ja', |
| 49 | + 'id': 1, |
| 50 | + 'text': "猫は幸せ" |
| 51 | + }, { |
| 52 | + 'language': 'de', |
| 53 | + 'id': 2, |
| 54 | + 'text': "Fahrt nach Stuttgart und dann zum Hotel zu Fu." |
| 55 | + }, { |
| 56 | + 'language': 'en', |
| 57 | + 'id': 3, |
| 58 | + 'text': "My cat is stiff as a rock." |
| 59 | + }, { |
| 60 | + 'language': 'es', |
| 61 | + 'id': 4, |
| 62 | + 'text': "A mi me encanta el fútbol!" |
| 63 | + }] |
| 64 | + |
| 65 | + for document in documents: |
| 66 | + print("Asking key-phrases on '{}' (id: {})".format(document['text'], document['id'])) |
| 67 | + |
| 68 | + response = client.key_phrases( |
| 69 | + documents=documents |
| 70 | + ) |
| 71 | + |
| 72 | + for document in response.documents: |
| 73 | + print("Found out that in document {}, key-phrases are:".format(document.id)) |
| 74 | + for phrase in document.key_phrases: |
| 75 | + print("- {}".format(phrase)) |
| 76 | + |
| 77 | + except Exception as err: |
| 78 | + print("Encountered exception. {}".format(err)) |
| 79 | + |
| 80 | + |
| 81 | +def sentiment(subscription_key): |
| 82 | + """Sentiment. |
| 83 | +
|
| 84 | + Scores close to 1 indicate positive sentiment, while scores close to 0 indicate negative sentiment. |
| 85 | + """ |
| 86 | + client = TextAnalyticsAPI(TEXTANALYTICS_LOCATION, CognitiveServicesCredentials(subscription_key)) |
| 87 | + |
| 88 | + try: |
| 89 | + documents = [{ |
| 90 | + 'language': 'en', |
| 91 | + 'id': 0, |
| 92 | + 'text': "I had the best day of my life." |
| 93 | + }, { |
| 94 | + 'language': 'en', |
| 95 | + 'id': 1, |
| 96 | + 'text': "This was a waste of my time. The speaker put me to sleep." |
| 97 | + }, { |
| 98 | + 'language': 'es', |
| 99 | + 'id': 2, |
| 100 | + 'text': "No tengo dinero ni nada que dar..." |
| 101 | + }, { |
| 102 | + 'language': 'it', |
| 103 | + 'id': 3, |
| 104 | + 'text': "L'hotel veneziano era meraviglioso. È un bellissimo pezzo di architettura." |
| 105 | + }] |
| 106 | + |
| 107 | + for document in documents: |
| 108 | + print("Asking sentiment on '{}' (id: {})".format(document['text'], document['id'])) |
| 109 | + |
| 110 | + response = client.sentiment( |
| 111 | + documents=documents |
| 112 | + ) |
| 113 | + |
| 114 | + for document in response.documents: |
| 115 | + print("Found out that in document {}, sentimet score is {}:".format(document.id, document.score)) |
| 116 | + |
| 117 | + except Exception as err: |
| 118 | + print("Encountered exception. {}".format(err)) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + import sys, os.path |
| 123 | + sys.path.append(os.path.abspath(os.path.join(__file__, "..", ".."))) |
| 124 | + from tools import execute_samples |
| 125 | + execute_samples(globals(), SUBSCRIPTION_KEY_ENV_NAME) |
0 commit comments