Mụ hỡnh 1

Một phần của tài liệu Xây dựng chương trình dịch tự động Anh - Việt bằng phương pháp thống kê (Trang 70 - 76)

Input: - Tập cỏc vớ dụ huấn luyện - Mụ hỡnh 1

Output: Bảng cỏc giỏ trị xỏc suất t(ei|vj).

Do việc huấn luyện dựa vào Model 1, nờn đầu tiờn ta mụ tả cấu trỳc của Model1 dƣới dạng class trong C++:

class CModel1 {

protected:

CString se, sv;

tmodel<COUNT, PROB> tTable; int c_voca_E; int c_voca_V; int solan_em; public: CModel1(); ~CModel1(); float GetHesoLagrang();

void GeneralTable(const TCHAR *filename); // Test ttable

void TestTable(); //Test voi du lieu lon

int InitPramater(const TCHAR *filename); int TrainningParamater();

void SavePramater(const TCHAR *filename); friend class CModel2;

};

Khi ta sử dụng mụ hỡnh 1, ta chỉ việc gọi đồng thời 3 hàm theo thứ tự sau để huấn luyện tham số:

InitInitPramater: Khởi tạo giỏ trị xỏc suất t(ei|vj) của bảng tTable.

TrainningParamater(): Hàm huấn luyện tham số dựa vào thuõt toỏn EM,

SavePramater(const TCHAR *filename): Lƣu cỏc tham số đƣợc huấn luyện (bảng tTable) ra file.

Thuật toỏn huấn luyện dựa vào EM đƣợc mụ tả trong ngụn ngữ Visual C++ nhƣ sau:

int iem = 0; do {

POSITION pos = listPair.GetHeadPosition(); while (pos != NULL){

SSenPair ss = listPair.GetNext(pos);

CList<CString, LPCTSTR> listword1, listword2; GetWordEnglish(ss.s_eng, listword1);

GetWordVietnamese(ss.s_viet,listword2); //Tinh count(e|v) -> ar_count

POSITION pos1 = listword1.GetHeadPosition(); for (int i = 0 ; i < listword1.GetCount(); i++){

int iE = 0;

POSITION pos2 = listword2.GetHeadPosition(); float sum = 0;

CString strEng = listword1.GetNext(pos1); map_Eng->GetKey(strEng,iE);

for (int j = 0 ; j < listword2.GetCount(); j++) {

int iV = 0; float aa = 0.0;

CString strViet = listword2.GetNext(pos2); map_Viet->GetKey(strViet, iV); float bb = tTable.GetProb(iV,iE); if (bb > PROB_SMOOTH) aa = bb; else aa = (float)PROB_SMOOTH; sum += aa; } pos2 = listword2.GetHeadPosition(); for (j = 0 ; j < listword2.GetCount(); j++) { int iV = 0;

CString strViet = listword2.GetNext(pos2); map_Viet->GetKey(strViet, iV); float aa; float bb = tTable.GetProb(iV,iE); if ( bb > PROB_SMOOTH){ aa = bb; }else aa = (float)PROB_SMOOTH; if (sum != 0){

float val = aa/sum;

tTable.IncCount(iV, iE, val); }

cout<<"iV:"<<iV<<"iE" << iE << endl; AfxMessageBox(_T("Loi tong bang 0")); }

} }

} //Ket thuc cua listPair

//Tinh lai prob thong qua ham nay tTable.NormalizeTable();

iem++;

}while (iem < solan_em);

Ta dễ dàng tỡm đƣợc giúng hàng từ tốt nhất theo mụ hỡnh 1 (giúng hàng mà cú xỏc suất lớn nhất):

for (int i = 1; i <= I; i++) ) | ( max arg max i j j v e t j4.4.2. Mụ hỡnh 2

Input: - Tập cỏc vớ dụ huấn luyện - Mụ hỡnh 2

Output: Bảng cỏc giỏ trị xỏc suất t(ei|vj).

Nhƣ ta đó biết mụ hỡnh 1 là trƣờng hợp đặc biệt của mụ hỡnh 2. Do đú khi ta mụ tả cấu trỳc của Model2 ta sẽ kế thừa từ mụ hỡnh 1:

class CModel2 : public CModel1 { public: amodel<PROB> aTable; amodel<COUNT> aCountTable; public: CModel2(); virtual ~CModel2(); CModel2(CModel1& m1,amodel<PROB>&,amodel<COUNT>&); public: void InitializeTable();

void LoadTable(const TCHAR* filename); inline amodel<PROB>& GetATable(void) {

return aTable; };

inline amodel<COUNT>& GetACountTable(void) {

return aCountTable; };

int TrainningParamater(); void TestModel2();

};

Khi ta sử dụng mụ hỡnh 2, ta chỉ việc gọi đồng thời 5 hàm theo thứ tự sau để huấn luyện tham số:

InitInitPramater: Khởi tạo giỏ trị xỏc suất t(ei|vj) của bảng tTable, chỳ ý hàm này là của lớp CModel1.

InitializeTable(): Khởit tạo giỏ trị a(i|j,v,e) của bảng amodel.

TrainningParamater(): Hàm huấn luyện tham số dựa vào thuõt toỏn EM,

kết quả lƣu trong bảng tTable và amodel.

SavePramater_tTable(const TCHAR *filename): Lƣu cỏc tham số

đƣợc huấn luyệnt(ei|vj) trong bảng tTable ra file.

SaveParamater_amodel(const TCHAR *filename): Lƣu cỏc tham số

a(i|j,v,e) của bảng amodel ra file.

Thuật toỏn huấn luyện cỏc tham số t(ei|vj) của mụ hỡnh 2 dựa vào thuật toỏn EM đƣợc mụ tả bằng ngụn ngữ Visual C++ nhƣ sau:

int lap_em = 0; do{

aCountTable.clear();

POSITION pos = listPair.GetHeadPosition(); while (pos != NULL){

SSenPair ss = listPair.GetNext(pos);

CList<CString, LPCTSTR> listword1, listword2; GetWordEnglish(ss.s_eng, listword1);

GetWordVietnamese(ss.s_viet,listword2); int m = listword1.GetCount() - 1;

int l = listword2.GetCount() - 1; //Tinh count(e|v) -> ar_count

POSITION pos1 = listword1.GetHeadPosition(); for (int i = 0 ; i < listword1.GetCount(); i++){

int iE = 0;

POSITION pos2 = listword2.GetHeadPosition(); float sum = 0.0;

CString strEng = listword1.GetNext(pos1); map_Eng->GetKey(strEng,iE);

{

int iV = 0; float aa = 0.0;

CString strViet = listword2.GetNext(pos2); map_Viet->GetKey(strViet, iV); float bb = tTable.getProb(iV,iE); if (bb > PROB_SMOOTH) aa = bb*aTable.getValue(j,i, l, m); else aa = (float)(PROB_SMOOTH* aTable.getValue(j,i, l, m)); sum += aa; } pos2 = listword2.GetHeadPosition(); for (j = 0 ; j < listword2.GetCount(); j++) { int iV = 0;

CString strViet = listword2.GetNext(pos2); map_Viet->GetKey(strViet, iV);

if (sum > 0){

float val = 1.0/sum; PROB e = 0.0;

float bb = tTable.getProb(iV,iE); if (e > PROB_SMOOTH) e = bb; else e = (float) PROB_SMOOTH; e *= aTable.getValue(j,i, l, m); COUNT temp = COUNT(e) * val ; tTable.incCount(iV, iE, temp);

aCountTable.getRef(j,i, l, m) += temp; }

else{

cout << "iV:" <<iV <<"iE" << iE << endl; AfxMessageBox(_T("Loi tong bang 0")); }

} } }

} //Ket thuc cua listPair tTable.NormalizeTable();

aCountTable.normalize(aTable); lap_em++;

} while(lap_em < solan_em);

Ta dễ dàng tỡm đƣợc giúng hàng từ tốt nhất theo mụ hỡnh 2 (giúng hàng mà cú xỏc suất lớn nhất):

) , , | ( * ) | ( max arg max t e v a j i J I j i j j4.5. Modul tỡm kiếm

Nhiệm vụ của modul này là ta phải đi tỡm cõu nguồn mà đó biết trƣớc đƣợc cõu đớch sao cho P(v|e) là lớn nhất. Dựa vào cỏc xỏc suất t(ei|vj) đó đƣợc tớnh trong modul huấn luyện tham số và đồng thời sử dụng thuật toỏn ngăn xếp nhanh. Cấu trỳc của quỏ trỡnh tỡm kiếm đƣợc mụ tả trong C++ nhƣ sau:

class CSearchThongke {

public:

CSearchThongke();

virtual ~CSearchThongke();

int SearchSMT(CString &strE, CString &strV); int GetScore(CString &str);

}

Thủ tục SearchSMT đƣợc mụ tả nhƣ sau:

for (int i = 0; i < Lm; i++){

CSearchQueue &sq = cs.GetAt(i);

POSTION pos = sq.data.GetHeadPosition(); for (int j = 0 ; j < sq.data.GetCount(); j++){

CString str_gt = sq.data.GetNext(pos)

if (sq.GetScore(str_gt) > sq.thres_hold){

POSITION pos1 = list_wordpromising.GetHeadPosition(); for (int k = 0; k < list_wordpromising.GetCount(); k++){

CString strword = list_wordpromising.GetNext(pos); if (Check(strword)){

str_gt += " " + strword;

double diem = GetScore(str_gt); CSearchQueue &sq1 = cs.GetAt(i + 1); sq1.SetScore(diem, str_gt); } } } } if (n_complete) break; } GetHighestScore(strV); 4.6. Từ điển dữ liệu Bao gồm từ điển:

- Song ngữ Anh –Việt: Giỳp cho việc huấn luyện và tỡm kiếm đƣợc nhanh hơn: Đƣợc lƣu bằng cấu trỳc Btree.

- Từ vựng tiếng Anh, Từ vựng tiếng Việt: Đƣợc lƣu bằng cấu trỳc Suffix tree. - Bigram tiếng Việt: Đƣợc lƣu băng cấu trỳc SuffixTree.

- Cỏc bảng giỏ trị xỏc suất tTable, amodel: Đƣợc lƣu dƣới dạng file txt.

4.7. Tiền xử lý và hậu xử lý

Phần tiền xử lý cú nhiệm vụ chia văn bản thành cỏc cõu, loại đi cỏc kớ tự điều khiển, cỏc hỡnh, cỏc đối tƣơng khụng phải là văn bản.

Phần hậu xử lý cú nhiệm vụ đƣa ra cỏc cõu dịch và lấy lại cỏc định dạng của cỏc cõu đầu vào.

Vớ dụ nhƣ cõu đƣợc dịch ở giữa hai dấu ngoặc kộp (“”) thỡ đến khi dịch đƣợc cũng phải nằm trong hai dấu ngoặc này. Hậu xử lý cũn cú thể lƣu cỏc ngữ cảnh của cõu dịch để kết hợp với cỏc cõu sau loại bỏ những cõu dịch khụng thớch hợp khi xảy ra trƣờng hợp một cõu vào mà cú nhiều cõu dịch khỏc nhau

4.8. Chương trỡnh thử nghiệm

Một phần của tài liệu Xây dựng chương trình dịch tự động Anh - Việt bằng phương pháp thống kê (Trang 70 - 76)