python_tools.zip をダウンロードして解凍します。ライブラリを OS にインストールした場合は、必要な場所にプログラムを作成できます。それ以外の場合は、テスト プログラムを dstk.py があるディレクトリに書き込むだけです。
ここに簡単なテストプログラムがあります。サービスから情報を取得する人のリストがあります。次に、民族情報を調べて、最も可能性の高い民族とそのパーセンテージを出力します。
import dstk
from pprint import pprint
dstk = dstk.DSTK()
# List of people you want to search for
people_names = ["Samuel L. Jackson", "Michelle Yeoh", "Danny Trejo", "Vanessa Minnillo","Naomi Campbell","Chuck Norris"]
# Query information for each person in the list
people = dstk.text2people(",".join(people_names))
# Print the structure of the received information
#print people
# Prints the structure of the people in more readable way
#pprint(people)
# Print name and ethnicity information of person
for person in people:
if person['ethnicity'] == None:
print (person['first_name'] + " " + person['surnames']).ljust(26), "Unknown ethnicity"
else:
ethnics = ['percentage_american_indian_or_alaska_native','percentage_asian_or_pacific_islander','percentage_black','percentage_hispanic','percentage_two_or_more','percentage_white']
highest_probability = 0
highest_index = 0
# Find highest percentage
for eth_index in ethnics:
if person['ethnicity'][eth_index] > highest_probability:
highest_probability = person['ethnicity'][eth_index]
highest_index = eth_index
print (person['first_name'] + " " + person['surnames']).ljust(20), str(person['ethnicity'][highest_index]).ljust(5), highest_index
上記のコードは以下を出力します。
Samuel L Jackson 53.02 percentage_black
Michelle Yeoh 87.74 percentage_asian_or_pacific_islander
Danny Trejo 94.15 percentage_hispanic
Vanessa Minnillo Unknown ethnicity
Naomi Campbell 76.47 percentage_white
Chuck Norris 82.01 percentage_white
サーバーから受け取った構造体を出力することで変数の名前を確認できます ( pprint(people)
)。名前は非常に明白です。
多民族またはアメリカインディアンとして数えられる人を見つけるのに苦労しました. データベースは彼らが白人であると主張しているようです.