将自然语言问题转换为SQL查询 [英] Convert Natural Language Questions to SQL Queries

查看:446
本文介绍了将自然语言问题转换为SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用opennlp Java库将自然语言问题转换为SQL查询,即

I want to convert Natural Language questions to SQL queries using opennlp Java library, i.e.


谁赢得了索契的女子花样滑冰? / p>

Who won women figure skating in Sochi?

应转换为

select name from winners where event='skating_woman'

任何人都知道哪些类有用以及如何实现这个?

Anybody know which classes will be useful and how to achieve this?

还粘贴了我试过的代码。

我已将问题转换为语句,后来转换为代币。

Also pasting code which I tried.
I have converted question into statements and later into tokens.

/////////1st part

String paragraph = "Did Matt win the men slalom?";
InputStream is1 = new FileInputStream("bins/en-sent.bin");
SentenceModel model1 = new SentenceModel(is1);
SentenceDetectorME sdetector = new SentenceDetectorME(model1);
String sentences[] = sdetector.sentDetect(paragraph);
System.out.println(sentences[0]);
is1.close();

////////2nd part
InputStream is2 = new FileInputStream("bins/en-token.bin");
TokenizerModel model2 = new TokenizerModel(is2);
Tokenizer tokenizer = new TokenizerME(model2);
String tokens[] = tokenizer.tokenize(sentences[0]);
for (String a : tokens)
    System.out.println(a);
is2.close();


推荐答案

对此的简单回答是你可以做它 - 但它不切实际,几乎不可能。简单的原因是你可以有许多类似的问题可以完全相同。使用SQL查询,您必须保留特定的语法以获取数据...使用标准语言,您可能有100种不同的语法来获得相同的东西。将其映射到SQL语言是非常不切实际的,几乎不可能。

Simple answer to this is that you "Could" do it - but its impractical and close to impossible.. Simple reason is that you can have many similar Questions that can mean exactly the same. With SQL query you have specific syntax you have to keep in order to get data... with standard language you have potentially 100's of different syntax to get same thing. Mapping this into SQL language is very impractical and close to impossible.

是的,您可以强制用户使用特定的单词或特定的问题格式,这样您的程序就可以将它们解释为SQL查询 - 但这又打破了你的范式想要实现不是吗

Yes you could force the user to use specific words or specific question format so your program can interpret them as SQL queries - but than again this is breaking the paradigm of what you want to achieve isn't it

编辑:

因为你太绝望了;)你可能潜在地 做这样的事情

Since you are soooo desperate ;) you could "potentially" do something like this

让我们想象一个非常简单的查询

Lets imagine a very simple Query

SELECT * FROM USERS WHERE AGE = '20';

有哪些可能的人类语言问题?

What are the possible Human language questions?


  • 你能告诉我所有20岁的人吗?

  • 显示年龄为20岁的人

  • 该群体中有多少人年龄为20岁?

  • 此表中有多少人有20年?

  • 我们是否有20岁的人?

  • 搜索所有年满20岁的人

  • Can you show me all people with age of 20?
  • Display humans with the age of 20
  • How many people in this group have age of 20?
  • How many people in this table have 20 years ?
  • Do we have any people aged 20 ?
  • Search for all people with age of twenty

你可以做的是创建某种 Map< key,value> 哪里ie。

What you could then do is create some sort of Map<key,value> where ie.

Key = USERS;
Value = people, humans, 

另一张地图

Key = SELECT;
Value = Can you show me, Display, Do we have, Search, How many;

依此类推 - 这将创建一个复杂的地图,其中包含所有可能含义相同的短语事物并对应于给定的SQL查询语法元素。这可能不是最好的解决方案,但这是我可能会做的 - 至少这是我要开始的

And so on so forth - This will create a complex map with all possible phrases that can mean the same thing and correspond to a given SQL query syntax element. This may not be the best solution but it's what I would probably do - at least this is what I would start with

这篇关于将自然语言问题转换为SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆