计算 AWS Comprehend Sentiment 成本

Calculate AWS Comprehend Sentiment cost

我想以编程方式估算调用 AWS Comprehend Sentiment 的成本 API。我搜索了 SO 和 AWS calculators 但找不到方法。另外,我确信我发送的文本量的成本会很小,但我真的很想知道。

根据定价信息 here 我写了下面的代码。正确吗?

text = ["What a horrible rainy day today", 
        "What a great day today", 
        "This is a neutral statement"]

numChars = sum(len(i) for i in text)

#Sentiment is measured in units of 100 characters, with a 3 unit (300 character) minimum charge per request.
numUnits = int(math.ceil(numChars / 100))

# Up to 10M units
if numUnits < 10000000:
    pricePerunit = 0.0001
    sentimentCost = numUnits * pricePerunit

# From 10M-50M units
elif numUnits >= 10000000 and numUnits <= 50000000:
    pricePerunit = 0.0001
    sentimentCost = 9999999 * pricePerunit

    pricePerunit = 0.00005
    sentimentCost = sentimentCost + ((numUnits - 10000000) * pricePerunit)

# Over 50M units.
elif numUnits > 50000000:
    pricePerunit = 0.0001
    sentimentCost = 9999999 * pricePerunit

    pricePerunit = 0.00005
    sentimentCost = sentimentCost + (40000000 * pricePerunit)

    pricePerunit = 0.000025
    sentimentCost = sentimentCost + ((numUnits - 49999999) * pricePerunit)

print("\nEstimated $ charges to call AWS Comprehend Sentiment are: %0.5f\n" % sentimentCost)

不,这个计算不正确。具体来说:

  • 您需要对单位进行四舍五入,因此请使用 math.ceil(numChars / 100)
  • 前 10M、后 40M 和超过 50M 的 cost/unit 是不同的,您错误地认为 所有 单位都按边际收费速度。您的代码会将 10M+1 个单位的成本计算为 (10M+1) * 0.00005,而它应该是 10M*0.0001 + 1*0.00005
  • 此外,您的代码将在恰好 10000000 或 50000000 个单位时崩溃