计算AWS理解情感成本

时间:2020-05-02 17:48:24

标签: python-3.x amazon-web-services amazon-comprehend

我想以编程方式估算调用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)

1 个答案:

答案 0 :(得分:1)

否,此计算不正确。具体来说:

  • 您需要四舍五入,因此请使用undefined
  • 前10M,后40M和50M之后的任何东西的成本/单位都不同,并且您误认为所有单位都是按边际费率收费。您的代码将计算10M + 1个单位的成本为(10M + 1)* 0.00005,当它应为10M * 0.0001 + 1 * 0.00005
  • 此外,您的代码将完全以10000000或50000000个单位崩溃