class ItemMaster{
public $id;
public $item_id;
public function updateItem(){
//このマスターから作られたItemを更新する
}
//以下続く
}
class Item{
public $id;
public $item_master_id;
public function updateItemMaster(){
//このアイテムから作られたItemMasterを更新する
}
//以下続く
}
これも、やってみたレベルですし、前回に引き続き、Googleさんがサンプルコードを提供してくれているのでコードのオリジナリティはまったくないですが(笑)、やり方と現時点(2021年7月21日)での Cloud Natural Language が、日本語の住所が文章から抜き出せるかという結果などをまとめておきたいので書いておきます。
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started
というエラーが出ます。前回と一緒ですね!なので、キーファイルをコード内で指定します。
全文は下記の通りです。
from google.cloud import language_v1
import os
#下記はサンプルなので、ご自分のキーファイルの場所に置き換えてください。
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="D:/natural_language_api_key/key.json"
def sample_analyze_entities(text_content):
"""
Analyzing Entities in a String
Args:
text_content The text content to analyze
"""
client = language_v1.LanguageServiceClient()
type_ = language_v1.Document.Type.PLAIN_TEXT
# サンプルではen ですが、日本語でjaに変更します。
language = "ja"
document = {"content": text_content, "type_": type_, "language": language}
# Available values: NONE, UTF8, UTF16, UTF32
encoding_type = language_v1.EncodingType.UTF8
response = client.analyze_entities(request = {'document': document, 'encoding_type': encoding_type})
# Loop through entitites returned from the API
for entity in response.entities:
print(u"Representative name for the entity: {}".format(entity.name))
print(u"Entity type: {}".format(language_v1.Entity.Type(entity.type_).name))
print(u"Salience score: {}".format(entity.salience))
# Loop over the metadata associated with entity. For many known entities,
# the metadata is a Wikipedia URL (wikipedia_url) and Knowledge Graph MID (mid).
# Some entity types may have additional metadata, e.g. ADDRESS entities
# may have metadata for the address street_name, postal_code, et al.
for metadata_name, metadata_value in entity.metadata.items():
print(u"{}: {}".format(metadata_name, metadata_value))
# Loop over the mentions of this entity in the input document.
# The API currently supports proper noun mentions.
for mention in entity.mentions:
print(u"Mention text: {}".format(mention.text.content))
# Get the mention type, e.g. PROPER for proper noun
print(
u"Mention type: {}".format(language_v1.EntityMention.Type(mention.type_).name)
)
# Get the language of the text, which will be the same as
# the language specified in the request or, if not specified,
# the automatically-detected language.
print(u"Language of the text: {}".format(response.language))
sample_analyze_entities('昔々、あるところに一匹のこざるがおりました。')
‘昔々、あるところに一匹のこざるがおりました。’
という文章をエンティティ分析にかけたわけです。
結果は次の通り。
Representative name for the entity: ところ Entity type: OTHER Salience score: 1.0 Mention text: ところ Mention type: COMMON Representative name for the entity: 1 Entity type: NUMBER Salience score: 0.0 value: 1 Mention text: 1 Mention type: TYPE_UNKNOWN Language of the text: ja
import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.abspath('resources/wakeupcat.jpg')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description)
上記を実行してみるも…
Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see...
import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="D:/vision_api_key/key.json"
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.abspath('wakeupcat.jpg')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description)
実行すると
Labels: Cat Window Felidae Carnivore Jaw Ear Small to medium-sized cats Window blind Gesture Whiskers