偵測語言
市議會正在評估是否值得為 Get It Done 應用程式建立西班牙語版本。雖然有相當多說西班牙語的選民,但他們不確定是否會使用。支援多語系翻譯會讓系統更複雜,因此需要充分的理由。
他們請 Sam 找出有多少人是用西班牙語張貼需求。
她已經把 CSV 載入到變數 dumping_df,並篩選成下列欄位:

請協助 Sam 量化是否需要西班牙語版本的需求。找出有多少申請人使用西班牙語,並列印最終結果!
本練習屬於課程
Python 中的 AWS Boto 入門
練習說明
- 對 DataFrame 的每一列偵測主要語言。
- 將第一個選出的語言指定到
'lang'欄位。 - 計算以西班牙語發佈的貼文總數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# For each dataframe row
for index, row in dumping_df.iterrows():
# Get the public description field
description =dumping_df.loc[index, 'public_description']
if description != '':
# Detect language in the field content
resp = comprehend.____(____=description)
# Assign the top choice language to the lang column.
dumping_df.loc[index, 'lang'] = resp['____'][0]['____']
# Count the total number of spanish posts
spanish_post_ct = len(dumping_df[dumping_df.lang == 'es'])
# Print the result
print("{} posts in Spanish".format(spanish_post_ct))