如何使用lambda从aws3存储中获取特定的文件字数?

2024-03-29 13:53:47 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的场景中,我尝试使用AWS lambdapython代码获取特定的aws3存储文本fileword count及其{}。在这里,下面的代码我正在尝试。它提供了行数,但我不知道如何获得字数和语言检测。请提供一些想法获取文件字数和语言检测。在

我试着数一数

import boto3

def lambda_handler(event, context):

    # create the s3 resource
    s3 = boto3.resource('s3')

    # get the file object
    obj = s3.Object('bucket name', 'sample.txt')

    # read the file contents in memory
    file_contents = obj.get()["Body"].read()

    # print the occurrences of the new line character to get the number of lines
    # print file_contents.count('\n')
    # TODO implement
    return {
        'Line Count': file_contents.count('\n')
    }

Current Response: { "Line Count": 48, }

Expected Response: { "Line Count": 48, "Word Count": : ?, // Here I want to show word count "Language": ? // Here language name }


Tags: the代码name语言objgets3count
1条回答
网友
1楼 · 发布于 2024-03-29 13:53:47

要获得字数,您可以尝试下面列出的任何方法:How to count the number of words in a sentence, ignoring numbers, punctuation and whitespace?

要检测语言,您可以尝试下面列出的方法:NLTK and language detection

不幸的是,你的问题相当宽泛。另外,检测文本语言的任务是相当困难的。计算字数很容易,但在很大程度上取决于你将如何定义一个词。在

相关问题 更多 >