参考
Natural Language API の基本 | Cloud Natural Language API ドキュメント | Google Cloud
python-docs-samples/snippets.py at master · GoogleCloudPlatform/python-docs-samples · GitHub
https://cloud.google.com/natural-language/docs/analyzing-entities
https://cloud.google.com/natural-language/docs/reference/rest/v1/Entity?hl=ja#Type
インプットテキスト
AIスピーカー「アレクサ」に潜む弱点 部屋に侵入される恐れも - ライブドアニュース
アマゾンのAIスピーカー「エコー(アレクサ)」の発売以来、急速に人気が高まってきたスマート・デバイスの音声操作機能。だが、最近そこに思わぬセキュリティ・ホールが発見された。
私たち人間には聞き取れないが、機械にだけは聞き取れるサブリミナル・メッセージを音声や音楽などに忍ばせることで、スマホやAIスピーカーを外部の第三者が自由自在に操れるというのだ。
このショッキングな研究成果を発表したのは、米カリフォルニア大学バークレイ校の科学者チーム。彼らは録音された音声メッセージや音楽に巧妙な命令を忍ばせ、これをユーチューブなどから流すことで、AIスピーカーのようなスマート・デバイスを秘かに操作することに成功した。
これにより本来の利用者がスマート・デバイスの電源を入れた状態で音楽を聴いたり、動画を見たりしている間に、(自分のアカウントを通じて)悪意を持った第三者から勝手にオンライン・ショッピングをされたり、自分のお金をどこかに送金されたり、甚だしい場合にはドアのロックを外されて部屋に侵入されたりする恐れが出てきたという。
サンプルコード
from google.cloud import language from google.cloud.language import enums from google.cloud.language import types import six def entities_text(text): client = language.LanguageServiceClient() if isinstance(text, six.binary_type): text = text.decode('utf-8') # Instantiates a plain text document. document = types.Document( content=text, type=enums.Document.Type.PLAIN_TEXT) entities = client.analyze_entities(document).entities lang = client.analyze_entities(document).language entity_type = ('UNKNOWN', 'PERSON', 'LOCATION', 'ORGANIZATION', 'EVENT', 'WORK_OF_ART', 'CONSUMER_GOOD', 'OTHER') for entity in entities: print('=' * 20) print(u'{:<16}: {}'.format('name', entity.name)) print(u'{:<16}: {}'.format('type', entity_type[entity.type])) print(u'{:<16}: {}'.format('salience', entity.salience)) print(u'{:<16}: {}'.format('wikipedia_url', entity.metadata.get('wikipedia_url', '-'))) for i in entity.mentions: print(u'{:<16}: {}'.format('mentions', 'content: ' + i.text.content + ' begin_offset: ' + str(i.text.begin_offset) + ' type: ' + entity_type[i.type])) def main(): text = 'アマゾンのAIスピーカー「エコー(アレクサ)」の発売以来、急速に人気が高まってきたスマート・デバイスの音声操作機能。だが、最近そこに思わぬセキュリティ・ホールが発見された。私たち人間には聞き取れないが、機械にだけは聞き取れるサブリミナル・メ ッセージを音声や音楽などに忍ばせることで、スマホやAIスピーカーを外部の第三者が自由自在に操れるというのだ。このショッキングな研究成果を発表したのは、米カリフォルニア大学バークレイ校の科学者チーム。彼らは録音された音声メッセージや音楽に巧妙な命令を忍ば せ、これをユーチューブなどから流すことで、AIスピーカーのようなスマート・デバイスを秘かに操作することに成功した。これにより本来の利用者がスマート・デバイスの電源を入れた状態で音楽を聴いたり、動画を見たりしている間に、(自分のアカウントを通じて)悪意を 持った第三者から勝手にオンライン・ショッピングをされたり、自分のお金をどこかに送金されたり、甚だしい場合にはドアのロックを外されて部屋に侵入されたりする恐れが出てきたという。' entities_text(text) main()
実行結果
mentionsの出力がおかしな感じ。typeはLOCATIONのみ。
==================== name : AIスピーカー type : CONSUMER_GOOD salience : 0.1747267097234726 wikipedia_url : - mentions : content: AIスピーカー begin_offset: -1 type: LOCATION mentions : content: AIスピーカー begin_offset: -1 type: LOCATION mentions : content: AIスピーカー begin_offset: -1 type: LOCATION ==================== name : スマート type : OTHER salience : 0.17224037647247314 wikipedia_url : - mentions : content: スマート begin_offset: -1 type: LOCATION mentions : content: スマート begin_offset: -1 type: LOCATION ==================== name : アマゾン type : OTHER salience : 0.112796850502491 wikipedia_url : https://en.wikipedia.org/wiki/Amazon_(company) mentions : content: アマゾン begin_offset: -1 type: PERSON mentions : content: アレクサ begin_offset: -1 type: LOCATION ==================== name : 音楽 type : WORK_OF_ART salience : 0.05859414488077164 wikipedia_url : - mentions : content: 音楽 begin_offset: -1 type: LOCATION mentions : content: 音楽 begin_offset: -1 type: LOCATION mentions : content: 音楽 begin_offset: -1 type: LOCATION ==================== name : デバイス type : CONSUMER_GOOD salience : 0.05645248666405678 wikipedia_url : - mentions : content: デバイス begin_offset: -1 type: LOCATION ==================== name : 第三者 type : PERSON salience : 0.03569682314991951 wikipedia_url : - mentions : content: 第三者 begin_offset: -1 type: LOCATION mentions : content: 第三者 begin_offset: -1 type: LOCATION ==================== name : エコー type : OTHER salience : 0.0339825302362442 wikipedia_url : - mentions : content: エコー begin_offset: -1 type: LOCATION ==================== name : 発売 type : EVENT salience : 0.0339825302362442 wikipedia_url : - mentions : content: 発売 begin_offset: -1 type: LOCATION ==================== name : 人気 type : OTHER salience : 0.0339825302362442 wikipedia_url : - mentions : content: 人気 begin_offset: -1 type: LOCATION ==================== name : 音声操作機能 type : OTHER salience : 0.0339825302362442 wikipedia_url : - mentions : content: 音声操作機能 begin_offset: -1 type: LOCATION ==================== name : スマホ type : CONSUMER_GOOD salience : 0.021044226363301277 wikipedia_url : - mentions : content: スマホ begin_offset: -1 type: LOCATION ==================== name : 音声 type : OTHER salience : 0.016728678718209267 wikipedia_url : - mentions : content: 音声 begin_offset: -1 type: LOCATION ==================== name : ホール type : LOCATION salience : 0.014126484282314777 wikipedia_url : - mentions : content: ホール begin_offset: -1 type: LOCATION ==================== name : セキュリティ type : OTHER salience : 0.014126484282314777 wikipedia_url : - mentions : content: セキュリティ begin_offset: -1 type: LOCATION ==================== name : スマート・デバイス type : CONSUMER_GOOD salience : 0.01232805848121643 wikipedia_url : - mentions : content: スマート・デバイス begin_offset: -1 type: LOCATION ==================== name : こと type : OTHER salience : 0.011985300108790398 wikipedia_url : - mentions : content: こと begin_offset: -1 type: LOCATION ==================== name : サブリミナル・メッセージ type : OTHER salience : 0.009465305134654045 wikipedia_url : - mentions : content: サブリミナル・メッセージ begin_offset: -1 type: LOCATION ==================== name : 機械 type : OTHER salience : 0.009465305134654045 wikipedia_url : - mentions : content: 機械 begin_offset: -1 type: LOCATION ==================== name : 人間 type : PERSON salience : 0.009465305134654045 wikipedia_url : - mentions : content: 人間 begin_offset: -1 type: LOCATION ==================== name : ショッピング type : EVENT salience : 0.008759783580899239 wikipedia_url : - mentions : content: ショッピング begin_offset: -1 type: LOCATION ==================== name : アカウント type : OTHER salience : 0.007321797776967287 wikipedia_url : - mentions : content: アカウント begin_offset: -1 type: LOCATION ==================== name : 外部 type : OTHER salience : 0.006733151618391275 wikipedia_url : - mentions : content: 外部 begin_offset: -1 type: LOCATION ==================== name : 米 type : LOCATION salience : 0.006372390780597925 wikipedia_url : - mentions : content: 米 begin_offset: -1 type: PERSON ==================== name : カリフォルニア大学 type : ORGANIZATION salience : 0.006372390780597925 wikipedia_url : - mentions : content: カリフォルニア大学 begin_offset: -1 type: PERSON ==================== name : 音声メッセージ type : WORK_OF_ART salience : 0.006255020387470722 wikipedia_url : - mentions : content: 音声メッセージ begin_offset: -1 type: LOCATION ==================== name : 科学者チーム type : PERSON salience : 0.005568326450884342 wikipedia_url : - mentions : content: 科学者チーム begin_offset: -1 type: LOCATION ==================== name : 研究成果 type : OTHER salience : 0.005568326450884342 wikipedia_url : - mentions : content: 研究成果 begin_offset: -1 type: LOCATION ==================== name : バークレイ校 type : ORGANIZATION salience : 0.005568326450884342 wikipedia_url : - mentions : content: バークレイ校 begin_offset: -1 type: LOCATION ==================== name : こと type : OTHER salience : 0.005289978347718716 wikipedia_url : - mentions : content: こと begin_offset: -1 type: LOCATION ==================== name : デバイス type : CONSUMER_GOOD salience : 0.005289978347718716 wikipedia_url : - mentions : content: デバイス begin_offset: -1 type: LOCATION ==================== name : こと type : OTHER salience : 0.005289978347718716 wikipedia_url : - mentions : content: こと begin_offset: -1 type: LOCATION ==================== name : 恐れ type : OTHER salience : 0.005215971264988184 wikipedia_url : - mentions : content: 恐れ begin_offset: -1 type: LOCATION ==================== name : 電源 type : OTHER salience : 0.005215971264988184 wikipedia_url : - mentions : content: 電源 begin_offset: -1 type: LOCATION ==================== name : 命令 type : OTHER salience : 0.0044446708634495735 wikipedia_url : - mentions : content: 命令 begin_offset: -1 type: LOCATION ==================== name : オンライン type : OTHER salience : 0.004084674641489983 wikipedia_url : - mentions : content: オンライン begin_offset: -1 type: LOCATION ==================== name : 金 type : OTHER salience : 0.004084674641489983 wikipedia_url : - mentions : content: 金 begin_offset: -1 type: LOCATION ==================== name : どこか type : OTHER salience : 0.004084674641489983 wikipedia_url : - mentions : content: どこか begin_offset: -1 type: LOCATION ==================== name : 悪意 type : OTHER salience : 0.003994716331362724 wikipedia_url : - mentions : content: 悪意 begin_offset: -1 type: LOCATION ==================== name : 状態 type : OTHER salience : 0.0038634329102933407 wikipedia_url : - mentions : content: 状態 begin_offset: -1 type: LOCATION ==================== name : 動画 type : WORK_OF_ART salience : 0.0038634329102933407 wikipedia_url : - mentions : content: 動画 begin_offset: -1 type: LOCATION ==================== name : 利用者 type : PERSON salience : 0.0038634329102933407 wikipedia_url : - mentions : content: 利用者 begin_offset: -1 type: LOCATION ==================== name : 場合 type : OTHER salience : 0.0036949925124645233 wikipedia_url : - mentions : content: 場合 begin_offset: -1 type: LOCATION ==================== name : ドア type : OTHER salience : 0.0036949925124645233 wikipedia_url : - mentions : content: ドア begin_offset: -1 type: LOCATION ==================== name : ロック type : EVENT salience : 0.0036949925124645233 wikipedia_url : - mentions : content: ロック begin_offset: -1 type: LOCATION ==================== name : 部屋 type : LOCATION salience : 0.0036949925124645233 wikipedia_url : - mentions : content: 部屋 begin_offset: -1 type: LOCATION ==================== name : ユーチューブ type : OTHER salience : 0.0029422540683299303 wikipedia_url : - mentions : content: ユーチューブ begin_offset: -1 type: LOCATION