/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package nl2sql.matcher;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import nl2sql.lexicon.Database;
import nl2sql.matcher.Attach;
/**
*
* @author TAMHD
* Get the corresponding attributes of one value
*
*/
public class GetAttribute {
String v; // value
String a; // corresponding attribute
// Hàm lấy tên cột tương ứng
public GetAttribute(String parser, Database db, String value, ArrayList<String> Left, ArrayList<String> Right) throws Exception{
this.v = value;
int Index = Right.indexOf(value);
Attach At = new Attach(parser);
int eIndex = At.getAttach(Left, Index);
//System.out.println(Right.get(eIndex));
// Phải trừ ra những từ mà chắc chắn câu đã nhận ra
String[] forbit = {"số báo danh", "mssv", "thí sinh", "giới tính", "ngành"};
List<String> forbiden = Arrays.asList(forbit);
// Chỉ cần từ đó có cặp là ok hết, không cần biết trong db có hay không
/*
if(eIndex!= -1 && db.attribute.contains(Right.get(eIndex)) && !forbiden.contains(Right.get(eIndex))){
a = Right.get(eIndex);
return;
}*/
if(eIndex!= -1 && db.attribute.contains(Right.get(eIndex))){
// Lấy tọa độ của cột
String col = Right.get(eIndex);
int pos = db.attribute.indexOf(col);
for(ArrayList<String> S:db.Value){
if(S.get(pos).contains(value)){
System.out.println("Pairing attribute of " + value + " in sentence : " + a);
a = Right.get(eIndex);
return;
}
}
}
// Duyệt trong câu xem có cột nào khớp với value không
// Chỉ chạy khi mà không tìm được giá trị thui
for(String s:Right){
if(!s.equals(value) && !forbiden.contains(s)){
int pos = db.attribute.indexOf(s);
if(pos != -1){
for(ArrayList<String> S:db.Value){
if(S.get(pos).equalsIgnoreCase(value)){
a = s;
System.out.println("Retrieving attribute of " + value + " in sentence : " + a);
return;
}
}
}
}
}
/*
// Duyệt trên toàn bộ db xem có cột nào khớp với value không
for(int i = db.Value.size() -1; i > 0 ; i --){
if(db.Value.get(i).contains(v)){
int pos = db.Value.get(i).indexOf(v);
a = db.Value.get(0).get(pos);
System.out.println("Retrieving missing attribute of " + value +" : " + a);
return;
}
}
* */
}
public String getAttr(){
return this.a;
}
public static boolean checkAttach(String value, String attribute, Database db){
int size = db.Value.size();
int pos;
if (db.Value.get(0).contains(attribute)){
pos = db.Value.get(0).indexOf(attribute);
} else return false;
for (int i= 0; i < size; i ++){
if(db.Value.get(i).get(pos).equalsIgnoreCase(value)) return true;
}
return false;
}
}