2013年7月28日星期日

Perl一些代码

1) 得到当前目录

use Cwd            qw( abs_path );
use File::Basename qw( dirname );

my $current_dir = dirname(abs_path($0));


2) 使用regex进行部分lowercase替换

$line =~ s/( [^\(\)]+\))/lc($1)/ge;

((S (NP (DT The)(NN luxury)(NN auto)(NN maker))(NP (JJ last)(NN year))(VP (VBD sold)(NP (CD 1,214)(NNS cars))(PP (IN in)(NP (DT the)(NNP U.S.))))))

替换为
((S (NP (DT the)(NN luxury)(NN auto)(NN maker))(NP (JJ last)(NN year))(VP (VBD sold)(NP (CD 1,214)(NNS cars))(PP (IN in)(NP (DT the)(NNP u.s.))))))

3) 使用regex提取字符数组
my @terms = ($line =~ /\([^\(\)]+\)/g);

((S (NP (DT The)(NN luxury)(NN auto)(NN maker))(NP (JJ last)(NN year))(VP (VBD sold)(NP (CD 1,214)(NNS cars))(PP (IN in)(NP (DT the)(NNP U.S.))))))
提取出(DT The)  (NN luxury)  (NN auto)  (NN maker)  (JJ last)  (NN year) ....  (NNP U.S.)

2013年7月19日星期五

使用maxent zhang le

1) 下载
git clone https://github.com/lzhang10/maxent.git

2) 安装
按照INSTALL说明, 依次安装
$ brew install gfortran  (需要先安装howbrew (ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"), 见http://mxcl.github.io/homebrew/)

$./configure

$make

$make install

3) 使用, 在eclipse中,


#include "maxent/maxentmodel.hpp"
MaxentModel *pModel = new MaxentModel();
pModel->begin_add_event();
.....
pModel->add_event(vecContext, p, 1);
.....
pModel->end_add_event();
pModel->train(100, "lbfgs"); // train the model with 30 iterations of L-BFGS method

编译时在link这一步会提示关于gfortran的错误
需要在Properties属性中添加linker libraries (-l): gfortan, 以及linker libraries path: /usr/local/Cellar/gfortran/4.8.1/gfortran/lib


2013年7月15日星期一

cdec中的cube pruning

cdec的cube pruning代码对应于apple_models.cc的CubePruningRescorer类.


在传统的cky解码过程中, 我们会为每一个chart定义一个heap, 如heap[i, j]用来存放所以souce side span为[i, j]的hypothese. 该heap的大小通常限置为某个常数(如200). 并且要求每个hypothesis的概率不能小于heap[i,j][0]的某个倍数(如0.05).
在cube pruning的时候, 通过设置pop_limit来限制最多生成多少个hypotheses. 
heap中各项的排列通常是按照score + heuristic_lm_score (即未计算在score中的前面几个单词的lm值)

在cdec的void KBestFast(const int vert_index, const bool is_goal) 方法中, 并没有为每个span[i, j]设置一个heap. 在cube pruning的时候, 设置pop_limit来限制最多生成多少个hypotheses. 虽然没有为每个span[i, j]设置一个heap,  cdec用candidate heap(没有大小限制)来保存:

      D_v.resize(state2node.size());
      int c = 0;
      for (State2Node::iterator i = state2node.begin(); i != state2node.end(); ++i){
          D_v[c++] = i->second;
          // cerr << "MERGED: " << *i->second << endl;
      }
      //cerr <<"Node id: "<< vert_index<< endl;
      //#ifdef MEASURE_CA
      // cerr << "countInProcess (pop/tot): node id: " << vert_index << " (" << count_in_process_pop << "/" << count_in_process_tot << ")"<      // cerr << "countAtEnd (pop/tot): node id: " << vert_index << " (" << count_at_end_pop << "/" << count_at_end_tot << ")"<      //#endif
      sort(D_v.begin(), D_v.end(), EstProbSorter());


cdec默认设置pop_limit为200, 为了增加搜索空间, 似乎只能放大pop_limit的值.

cdec是这样做cube pruning,

假设source side span为[i, j], 其source端看以表达为  a X1 b X2 c, ..., a X1 c,..., 各个source端对应的目标端翻译, 加起来共有m种, t1, t2, ..., tm; X1可能的hypotheses有n种, X11, ..., X1n; X2可能的hypotheses有k种, X21, ..., X2k.

1:  生成一个candidate list = {(0, 0, 0), (1, 0, 0), ..., (m, 0, 0)}. 第1/2/3维分别为上面三个变量的下标. 
2:  pop = 0;
3:  while (pop < pop_limit && candidate_list not empty) do
4:     对candidate list排序;
5:     从candidate_list中取出最大项(i, j, k);
6      生成新的hypothesis;
7      对(i, j, k)扩展, 往candidate_list中添加(i, j+1, k)和(i, j, k+1);
8      pop ++


X11, ..., X1n和X21, ..., X2k是按大小排序的. 不确定t1, t2, ..., tm是否也是按大小排序, 但这应该没有关系.