sql - 从自由格式 STT 输入中查询子字符串
问题描述
我有一个表中有词汇表的 PostgreSQL 数据库。
我想接收语音转文本 (STT) 输入并查询我的词汇表以查找匹配项。
这很棘手,因为 STT 在某种程度上是自由形式的。
假设该表包含以下词汇和短语:
- 你好吗?
- 你好
- 很高兴见到你
- 爬坡道
- 好的
并提示用户说话:"Hi, nice to meet you"
我转录他们的输入,"Hi nice to meet you"
并查询我的数据库以查找单个词汇匹配。我想返回:
[
{
id: 2,
word: "Hi"
},
{
id: 3,
word: "Nice to meet you"
}
]
我可以使用通配符进行查询,where word ilike '%${term}%
但是我需要传入正确的子字符串,以便它找到匹配项,例如 ,where word ilike '%Hi%
但这可能会错误地返回Hill
。我还可以按空格分割语音输入,给 me ["Hi", "nice", "to", "meet", you"]
,然后循环遍历每个单词以寻找匹配项,但这可能会返回Nice
而不是短语Nice to meet you
。
问:如何正确地将子字符串传递给查询并为自由格式的语音返回准确的结果?
解决方案
- 两个 PostgreSQL 函数可以在这里为您提供帮助:
to_tsvector
:创建标记的文本搜索列表(词位:词义单位)to_tsquery
用于查询向量中某些单词或短语的出现。
如果这还不够,您需要求助于自然语言处理 (NLP)。
像PyTextRank这样的东西可能会有所帮助(超出词袋技术的东西):import spacy import pytextrank text = "Hi, how are you?" # load a spaCy model, depending on language, scale, etc. nlp = spacy.load("en_core_web_sm") # add PyTextRank to the spaCy pipeline tr = pytextrank.TextRank() nlp.add_pipe(tr.PipelineComponent, name="textrank", last=True) doc = nlp(text) # examine the top-ranked phrases in the document for p in doc._.phrases: print("{:.4f} {:5d} {}".format(p.rank, p.count, p.text)) print(p.chunks)
推荐阅读
- typescript - 如何使用 TypeScript 键入枚举值?
- flutter - 如何在飞镖中转换地图的关键数据类型?例如来自地图
到地图 - c - 为什么我的 searchPuzzle 函数中的 printf() 语句不起作用?
- dialogflow-es - 如果 Intent 数量超过 200,我们是否应该为所有 Intent 设置单独的函数处理程序
- jquery - 无法使用 jQuery 打开新窗口
- python - LSTM 模型实现
- python - 用漂亮的汤刮 youtube 网站
- selenium - 我在 Capturescreensot 方法和自定义侦听器中收到“java.lang.NullPointerException”
- azure - 有没有办法使用 Graph API 获取管理员同意请求列表?
- javascript - 如何在javascript中将变量修剪并保存到字符串中的第一个空格