首页 > 解决方案 > 从自由格式 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

问:如何正确地将子字符串传递给查询并为自由格式的语音返回准确的结果?

标签: sqlstringpostgresqlsearch

解决方案


  1. 两个 PostgreSQL 函数可以在这里为您提供帮助:
  1. 如果这还不够,您需要求助于自然语言处理 (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)
    

推荐阅读