gpt4 book ai didi

javascript - 将语音字符串映射到可能的备用字符串以与 dynamoDB 匹配

转载 作者:太空宇宙 更新时间:2023-11-04 02:09:36 25 4
gpt4 key购买 nike

我们正在集成 Amazon 的 Alexa 以与我们的应用程序配合使用。我们在 DynamoDB 中创建了 Alexa 可能会询问的项目的字典。现在我们需要一种算法来将 Alexa 的文本与 DynamoDB 表中存储的字符串进行匹配,这些字符串在语音上相似,但可能拼写不同或中间有特殊字符,例如

"X-Men" may be requested as "xmen" or "ex men" or "x men"

"Claire" may be requested as "clare" or "clair"

我找到了Amazon DynamoDB-ElasticSearch Integration一个看似合理的选择,但我还没有了解足够的信息。这也可能相当昂贵。

我还尝试找出是否有 Node 模块可以帮助找到可以与数据库匹配的类似字符串。

fuzzy search node module可能对我们也不起作用,特别是因为我们有找到单个匹配项的约束,而不是单个字符串输入的可能匹配项列表。

例如。搜索 "Xmen" 应仅返回 "X-Men",而不返回 "X-Men""Ex-servicemen"

我最后的手段是从头开始编写一个近似字符串匹配算法以及Levenshtein距离计算算法

最佳答案

根据DynamoDB Query API Documentation :

Query :

A Query operation uses the primary key of a table or a secondary index to directly access items from that table or index.

Use the KeyConditionExpression parameter to provide a specific value for the partition key. The Query operation will return all of the items from the table or index with that partition key value. You can optionally narrow the scope of the Query operation by specifying a sort key value and a comparison operator in KeyConditionExpression. You can use the ScanIndexForward parameter to get results in forward or reverse order, by sort key.

KeyConditionExpression :

The condition that specifies the key value(s) for items to be retrieved by the Query action.

The condition must perform an equality test on a single partition key value. The condition can also perform one of several comparison tests on a single sort key value. Query can use KeyConditionExpression to retrieve one item with a given partition key value and sort key value, or several items that have the same partition key value but different sort key values.

The partition key equality test is required, and must be specified in the following format:

partitionKeyName = :partitionkeyval

If you also want to provide a condition for the sort key, it must be combined using AND with the condition for the sort key. Following is an example, using the = comparison operator for the sort key:

partitionKeyName = :partitionkeyval AND sortKeyName = :sortkeyval

Valid comparisons for the sort key condition are as follows:

  • sortKeyName = :sortkeyval - true if the sort key value is equal to :sortkeyval.
  • sortKeyName < :sortkeyval - true if the sort key value is less than :sortkeyval.
  • sortKeyName <= :sortkeyval - true if the sort key value is less than or equal to :sortkeyval.
  • sortKeyName > :sortkeyval - true if the sort key value is greater than :sortkeyval.
  • sortKeyName >= :sortkeyval - true if the sort key value is greater than or equal to :sortkeyval.
  • sortKeyName BETWEEN :sortkeyval1 AND :sortkeyval2 - true if the sort key value is greater than or equal to :sortkeyval1, and less than or equal to :sortkeyval2.
  • begins_with ( sortKeyName, :sortkeyval ) - true if the sort key value begins with a particular operand. (You cannot use this function with a sort key that is of type Number.)

You can optionally use the ExpressionAttributeNames parameter to replace the names of the partition key and sort key with placeholder tokens. This option might be necessary if an attribute name conflicts with a DynamoDB reserved word. For example, the following KeyConditionExpression parameter causes an error because Size is a reserved word:

Size = :myval

To work around this, define a placeholder (such a #S) to represent the attribute name Size. KeyConditionExpression then is as follows:

#S = :myval

For a list of reserved words, see Reserved Words in the Amazon DynamoDB Developer Guide.

For more information on ExpressionAttributeNames and ExpressionAttributeValues, see Using Placeholders for Attribute Names and Values in the Amazon DynamoDB Developer Guide.

Type: String

Required: No

您的场景可以转换为以下代码:

$tableName = "genericTable";
$response = $dynamodb->query([
'TableName' => $tableName,
'IndexName' => 'OrderCreationDateIndex',
'KeyConditionExpression' => 'partitionKeyName = :partitionkeyval AND sortKeyName = :sortkeyval',
'ExpressionAttributeValues' => [
':partitionkeyval' => ['S' => 'pkey'],
':sortkeyval' => ['S' => 'sortkey']
],
'Select' => 'ALL_PROJECTED_ATTRIBUTES',
'ScanIndexForward' => false,
'ConsistentRead' => true,
'Limit' => 5,
'ReturnConsumedCapacity' => 'TOTAL'
]);

关于javascript - 将语音字符串映射到可能的备用字符串以与 dynamoDB 匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42853269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com