开始使用免费开始使用

检测语言

市议会正在考虑是否值得为 Get It Done 应用构建西班牙语版本。虽然有大量说西班牙语的选民,但他们不确定这些人是否会使用。为系统加入多语言翻译会增加复杂度,需要有充分理由。

他们请 Sam 统计有多少人用西班牙语提交请求。

她已经将 CSV 加载到变量 dumping_df 中,并将其子集化为以下列:

Get It Done requests in many languages

请帮助 Sam 量化是否需要西班牙语版本的 Get It Done 应用。找出有多少请求者使用西班牙语,并打印最终结果!

本练习是课程的一部分

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))
编辑并运行代码