/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nl2sql.equivalenceChecker.MarkCorrection;
import nl2sql.lexicon.Database;
import nl2sql.lexicon.GetElement;
import nl2sql.lexicon.Synonym;
import nl2sql.matcher.Attach;
import nl2sql.matcher.PairWithDB;
import nl2sql.matcher.TagProcess;
import nl2sql.parserQuestion.ColtechParser;
import nl2sql.parserQuestion.LinguisticComponent;
import nl2sql.sqlGenerator.QuestionWordAction;
import nl2sql.sqlGenerator.ToSql;
import nl2sql.utils.Directory;
import tools.tokenizer.Formulate;
import tools.tokenizer.Stopword;
/**
* @author TAMHD
*/
public class Run extends HttpServlet {
/*
* System.out.println(token); }
*/
ServletContext ctx;
static Attach At = null;
static Stopword st = null;
static HashSet<String> hs = null;
static Database db;
public static String Process(String parserC, String question, LinguisticComponent chunk, String ambiguity) throws Exception {
//System.out.println(TokenVn_ann);
// List chứa các từ có ý nghĩa so sánh
ArrayList<String> CompareWords = new ArrayList<String>();
// List chứa các action tương ứng với compareWords
ArrayList<String> CompareActions = new ArrayList<String>();
// List chứa các từ để hỏi
ArrayList<String> QuestionWords = new ArrayList<String>();
// List chứa các node tương ứng bên trái
ArrayList<String> Left = new ArrayList<String>();
// List chứa các node tương ứng bên phải
ArrayList<String> Right = new ArrayList<String>();
// List chứa các nhãn bên trái
ArrayList<String> Tag = new ArrayList<String>();
// List chứa các node bên trái sau khi match
ArrayList<String> NewLeft = new ArrayList<String>();
// List chứa các node bên phải sau khi match
ArrayList<String> NewRight = new ArrayList<String>();
// List chứa các nhãn sau khi match
ArrayList<String> NewTag = new ArrayList<String>();
TagProcess tagP = new TagProcess();
ArrayList<int[]> pairs = new ArrayList<int[]>();
if (chunk.TokenVn_ann != null) {
for (int i = 0; i < chunk.TokenVn_ann.length; i++) {
String token;
if(chunk.TokenVn_ann[i].getFeature("POS").equals("Major")
|chunk.TokenVn_ann[i].getFeature("POS").equals("RegNum")
|chunk.TokenVn_ann[i].getFeature("POS").equals("Region")){
token = chunk.TokenVn_ann[i].getSentence().toLowerCase();
} else{
token = chunk.TokenVn_ann[i].getSentence();
}
System.out.println(token);
String _tag = chunk.TokenVn_ann[i].getFeature("POS");
// Nhận diện và phân loại từ để hỏi
if(_tag.equals("GeneralMark") && !Tag.contains("Subject")){
System.err.println("Question: " + question);
String disambiguity = ambiguity;
if(disambiguity.isEmpty()){
System.out.println("---------------------> Hỏi về nhiều thông tin " + token);
} else{
System.out.println("---------------------> Xử lý nhập nhằng " + token + " trả về " + disambiguity);
Left.add(token);
Right.add(disambiguity);
int[] pair = {Left.size() - 1, Right.size() - 1};
pairs.add(pair);
Tag.add(_tag);
}
} else
// nhận diện và phân loại từ so sánh
if(_tag.equals("Compare")){
System.out.println("---------------------> Nhận dạng từ so sánh " + token);
CompareWords.add(token);
CompareActions.add(chunk.TokenVn_ann[i].getFeature("Opt"));
System.out.println(CompareActions);
} else
if (!hs.contains(token)) {
// Add vào list các từ
Left.add(token);
Tag.add(_tag);
// Kiểm tra nếu đó là value đã phân loại tag thì nhận lun
// Xử lý semantic trước
if(tagP.getTag(_tag)!= -1 && !token.equals("SBD") && !token.equals("ĐHCN")){
System.out.println("---------------------> Xử lý semantic " + token);
if(_tag.equals("Date")){
token = Formulate.ConvertDate(token);
} else if (_tag.equals("RegNum")){
token = Formulate.ConvertRegNum(token);
}
Right.add(token);
int[] pair = {Left.size() - 1, Right.size() - 1};
pairs.add(pair);
} else {
// Xử lý với csdl
// Tìm ra các element tương ứng với từ đó
GetElement getE = new GetElement(token);
getE.detechElement(db);
System.out.println("---------------------> Xử lý database " + token + " " + getE.VALUE);
ArrayList<String> e = getE.GetElementFromToken(db);
// Thêm tất cả các token này vào list các element, nếu trùng thì thôi
// Lưu lại tọa độ của cặp
for (int j = 0; j < e.size(); j++){
if (Right.indexOf(e.get(j)) != -1) {
int[] pair = {Left.size() - 1, Right.indexOf(e.get(j))};
pairs.add(pair);
} else {
Right.add(e.get(j));
int[] pair = {Left.size() - 1, Right.size() - 1};
pairs.add(pair);
}
}
}
} else {
System.out.println("---------------------> stopword: " + token);
}
}
}
System.out.println("Tag = " + Tag);
System.out.println("Left = " + Left);
System.out.println("Right = " + Right);
int L = Left.size();
int R = Right.size();
int s = L + R;
int t = L + R + 1;
FlowNetwork G = new FlowNetwork(L + R + 2);
for (int i = 0; i < pairs.size(); i++) {
G.addEdge(new FlowEdge(pairs.get(i)[0], L + pairs.get(i)[1], 1));
}
for (int i = 0; i < L; i++) {
G.addEdge(new FlowEdge(s, i, 1.0));
}
for (int i = 0; i < R; i++) {
G.addEdge(new FlowEdge(i + L, t, 100));
}
// compute maximum flow and minimum cut
FordFulkerson maxflow = new FordFulkerson(G, s, t);
StdOut.println();
StdOut.println("Size of maximum matching = " + (int) maxflow.value());
for (int v = 0; v < L; v++) {
for (FlowEdge e : G.adj(v)) {
if (e.from() == v && e.flow() > 0) {
int l = e.from();
int r = e.to() - L;
System.out.println(l + "-" + r);
NewLeft.add(Left.get(l));
NewTag.add(Tag.get(l));
NewRight.add(Right.get(r));
//System.out.println(Left.get(l) + " -- " + Right.get(r));
}
}
}
System.out.println("NewTag = " + NewTag);
System.out.println("NewLeft = " + NewLeft);
System.out.println("NewRight = " + NewRight);
// Produce a sql queries
PairWithDB p = new PairWithDB(NewRight);
p.matchingElementPair(parserC, db, NewLeft, NewRight, NewTag);
System.out.println("FinalTag = " + NewTag);
System.out.println("FinalLeft = " + NewLeft);
System.out.println("FinalRight = " + NewRight);
QuestionWordAction QWA = new QuestionWordAction(question, chunk, NewLeft, NewRight);
ToSql to = new ToSql(QWA, p, At, CompareWords, CompareActions, Left, Tag);
System.out.println(to.getSelect() + to.getFrom() + to.getWhere());
//System.out.println("SELECT " + attribute + " WHERE " + c1 + " FROM " + relation);
/*
Writer outputFile = new BufferedWriter(new OutputStreamWriter
(new FileOutputStream(Directory.LogFile), "UTF-8"));
outputFile.write(question + " ----- " + to.getSelect() + to.getFrom() + to.getWhere());
outputFile.close();
*/
String answer = to.getSelect() + to.getFrom() + to.getWhere();
try{
db.InsertLog(question, "answer");
} catch(Exception E){}
return (to.getSelect() + to.getFrom() + to.getWhere());
}
String EquivalenceCheck(LinguisticComponent chunk) {
String status = "unambiguous";
ArrayList<String> Tag = new ArrayList<String>();
if (chunk.TokenVn_ann != null) {
for (int i = 0; i < chunk.TokenVn_ann.length; i++) {
String token = token = chunk.TokenVn_ann[i].getSentence();
String _tag = chunk.TokenVn_ann[i].getFeature("POS");
// Nhận diện và phân loại từ để hỏi
if (_tag.equals("GeneralMark") && !Tag.contains("Subject")) {
status = token;
return status;
}
Tag.add(_tag);
}
}
return status;
}
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
ctx = getServletContext();
Directory.setRealPath(ctx);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
* Get the value of form parameter
*/
if(db==null){
}
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String typeInput = request.getParameter("option");
String inputQuestion = null;
String outputString = null;
ArrayList<ArrayList<String>> outputResult = null;
String ambString = request.getParameter("ambiguity");
String ambAlert = request.getParameter("alert_ambiguity");
if (ambString.equals("null")) ambString = "0";
if (ambAlert.equals("null")) ambAlert = "0";
int amb = Integer.parseInt(ambString);
if (typeInput.equals("2")) {
inputQuestion = request.getParameter("manualInput");
} else {
inputQuestion = request.getParameter("selectInput");
}
inputQuestion = inputQuestion.replace(",", ".");
LinguisticComponent chunk = null;
String col_data = null;
try {
chunk = new LinguisticComponent(Directory.directoryLinguisticComponent);
chunk.parseQuestion(inputQuestion, col_data);
} catch (Exception ex) {
Logger.getLogger(Run.class.getName()).log(Level.SEVERE, null, ex);
}
String status = null;
status = EquivalenceCheck(chunk);
System.out.println("ambiguity :: " + amb + " " + status + " " + ambAlert);
if (!status.equals("unambiguous") && ambAlert.equals("0")) {
request.setAttribute("manualInput", inputQuestion);
request.setAttribute("alert_ambiguity", "1");
request.setAttribute("ambiguity", "0");
request.setAttribute("option", typeInput);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
if (status.equals("unambiguous") || ambAlert.equals("1")){
ColtechParser col = new ColtechParser(chunk);
String parser = null;
try {
parser = col.Process(chunk);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Run.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(Run.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(Run.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Run.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(Run.class.getName()).log(Level.SEVERE, null, ex);
}
//col.writeInput(inputQuestion, chunk);
//col.toOutput();
// Chuẩn hóa viết phần thập phân tiếng anh
System.out.println("question: " + inputQuestion);
System.out.println("parser tree: " + parser);
String parsing = parser;
try {
if(db == null){
At = new Attach(parsing);
st = new Stopword();
hs = st.getStopWord();
db = new Database("đại học công nghệ");
}
String ambiguity = MarkCorrection.IdentifyColumn(amb);
outputString = Process(parsing, inputQuestion, chunk, ambiguity);
if(outputString != null && db!= null){
outputResult = db.ExecuteQuery(outputString);
}
} catch (Exception ex) {
Logger.getLogger(Run.class.getName()).log(Level.SEVERE, null, ex);
}
request.setAttribute("manualInput", inputQuestion);
request.setAttribute("alert_ambiguity", "0");
request.setAttribute("ambiguity", "0");
request.setAttribute("output", outputString);
request.setAttribute("option", typeInput);
request.setAttribute("result", outputResult);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
@Override
public void destroy() {
}
}