2013年6月21日星期五

Fixfor is already running

Firefox is already running, but is not responding. To open a new window, you must first close the existing Firefox process, or restart your system.
http://www.mattcutts.com/blog/how-to-fix-firefox-is-already-running-error/

1) 通过 “ps auxwww | grep firefox” 找到firefox进程号, 然后“kill [processnumber]”.

2) 如果以上还不能解决, Step 1. 找到firefox的profile文件, 通常是 ~/.mozilla/firefox/profiles.ini 该文件里面会说明Path, 比如Path=src6ip5x.default Step 2. 在Path的路径下, 删除lock files. 需要删除“lock”和“.parentlock” .

2013年6月18日星期二

cdec forced decoding

cdec支持forced decoding, 数据准备格式为:

<seg grammar="grammar/gra.0.gz" id="0"> <s> 今年 前 两 月 广东 高新 技术 产品 出口 37.6亿 美元 </s> ||| <s> export of high-tech products in guangdong in first two months this year 37.6亿 us dollars </s> </seg>

有时候一些可以可以手工根据翻译规则拼成的translation, 在forced decoding的时候并没有找到, 可以关闭language model feature试试, 可能是由于language model的原因, 使得提供的translation并不在hyper-graph中.
 

2013年5月28日星期二

cdec添加一个模块

往cdec中添加一个模块, 以svmlight, maxent等classifiers为例.

1) 新建文件夹classifier, 在classifier/中实现svmlight.h/cpp, maxent.h/cpp, classifier.h/cpp

2) 在classifier/中新建Makefile.am, 内容可参照:

noinst_LIBRARIES = libclassifiers.a

libclassifiers_a_SOURCES = \
  svmlight.cpp \
  maxent.cpp \
  classifier.cpp
 
AM_CPPFLAGS = -W -Wall

这样libclassifiers.a可以在其他地方调用

3) 在主文件夹, 修改Makefile.am
在SUBDIRS项中添加classifier文件夹

4) 在主文件夹, 修改configure.ac
在末尾添加
AC_CONFIG_FILES([classifier/Makefile])

5) 重新开始编译(autoconf, configure, make等)


注意: 得看情况决定是否要修改decoder/ mira-vlad/下的Makefile.am文件, 即添加../classifier/libclassifiers.a和-I../classifier项

2013年4月3日星期三

往cdec中添加一个新feature

可按下面步骤:
1) 在decoder/目录下新建ff_feature_name.h和ff_feature_name.cc文件.
    在ff_feature_name.h中定义继承于FeatureFunction的类, 如何有需要, 定义继承于FactoryBase的factory类. 可参考ff_klm.h, ff_basic.h等.

2) 注册新定义的feature
    在decoder/cdec_ff.cc中注册新feature
    #include "ff_feature_name.h"
    在void register_feature_functions()中添加:
    ff_registry.Register("NewFeatureName", new NewFeatureFactory()); //如果定义了factory类
    或者
    ff_registry.Register("NewFeatureName", new FFFactory); //如果没有定义factory类

3) 在decoder/Makefile.am 中添加新定义的类(文件) ff_feature_name.cc, 可参考ff_klm类的写法.

4) 重新编译..


添加一个新的feature, contextual free, 但是需要用state来保留些信息, 用于快速计算该feature值.
这个时候需要注意, apply_models.cc/IncorporateIntoPlusLMForest(...), 里面(*s2n)[item->state_]会根据state值,决定是否产生一个新的node,或者丢弃. 因此在这之前需要将state里面的内容关于新feature的部分给erase掉. 按以下:
1) 在ff_set.h/ModelSet类里添加
public: 
FFState GetRealFFState(const FFState& state) const;
在该方法的实现中根据原state状态, 返回一个新的real_state, real_state里将新feature部分的state给删除掉了
2) 在apply_models.cc/IncorporateInputPlusLMForest(...)里:
Candidate*& o_item = (*s2n)[item->state_];
修改为

FFState real_state = models.GetRealFFState(item->state_);
Candidate*& o_item = (*s2n)[real_state];


assert(o_item->state_ == item->state_);    // sanity check!
修改为

assert(models.GetRealFFState(o_item->state_) == models.GetRealFFState(item->state_));  //sanity check!

附: GetRealFFState()实现
void ModelSet::GetRealFFState(const FFState& state, FFState& real_state) const {
real_state.resize(state.size());
for (int i = 0; i < state.size(); i++) {
real_state[i] = state[i];
}

if (state.size() == 0)
return;
assert(state.size() == state_size_);

//erase the states for SRLReorderFeature
for (int i = 0; i < models_.size(); i++) {
if (models_[i]->name_ == string( "NewFeatureName" )) {
int start_pos = model_state_pos_[i];
int end_pos;
if (i == models_.size() - 1)
end_pos = state_size_;
else
end_pos = model_state_pos_[i + 1];
for (int j = start_pos; j < end_pos; j++)
real_state[j] = 0;
}
}
}

FFState ModelSet::GetRealFFState(const FFState& state) const {
FFState real_state;
GetRealFFState(state, real_state);
return real_state;
}




2013年3月29日星期五

cdec一些細節

1) 讀取cdec.ini文件
line484: decoder.cc
DecoderImpl::DecoderImpl() at decoder.cc:484
Decoder::Decoder() at decoder.cc:740
main() at cdec.cc:14

2) 讀取weight.init文件
line15:weights.cc
Weights::InitFromFile() at weights.cc
DecoderImpl::DecoderImpl() at decoder.cc:571

Decoder::Decoder() at decoder.cc:740
main() at cdec.cc:14

3) 讀取language model文件
line91: ff_factory.h:92
FactoryRegistry::Create() at ff_factory.h:92
make_ff() at decoder.cc:131
DecoderImpl::DecoderImpl() at decoder.cc:604
Decoder::Decoder() at decoder.cc:740
main() at cdec.cc:14

4) 添加glue grammar
line51: scfg_translator.cc
GlueGrammar::GlueGrammar() at scfg_translator.cc:51
SCFGTranslatorImpl::SCFGTranslatorImpl() at scfg_translator.cc:134
SCFGTranslator::SCFGTranslator() at scfg_translator.cc:345
DecoderImpl::DecoderImpl() at decoder.cc:667
Decoder::Decoder() at decoder.cc:740
main() at cdec.cc:14


5) 讀取grammar文件
line303: rule_lexer.ll
RuleLexer::ReadRules() at rule_lexer.ll:303
TextGrammar::ReadFromStream() at grammar.cc:123
TextGrammar::ReadFromFile() at grammar.cc:119
TextGrammar::TextGrammar() at grammar.cc:80
SCFGTranslator::ProcessMarkupHintsImpl() at scfg_translator.cc:363
Translator::ProcessMarkupHints() at translator.cc:21
DecoderImpl::Decoder() at decoder.cc:791
Decoder::Decoder() at decoder.cc:746
main() at cdec.cc:30

6) input sentence轉換為lattice structure
line58: lattice.cc
LatticeTools::ConvertTextOrPLF() at lattice.cc:58
SCFGTranslatorImpl::Translate() at scfg_translator.cc:186
SCFGTranslator::TranslateImpl() at scfg_translator.cc:354
Translator::Translate() at translator.cc:33
DecoderImpl::Decode() at decoder.cc:794
Decoder::Decode() at decoder.cc:746
main() at cdec.cc:30

7) 添加pass through grammar
line65: scfg_translator.cc
PassThroughGrammar::PassThroughGrammar() at scfg_translator.cc:65
SCFGTranslatorImpl::Translate() at scfg_translator.cc:190
SCFGTranslator::TranslateImpl() at scfg_translator.cc:354
Translator::Translate() at translator.cc:33
DecoderImpl::Decode() at decoder.cc:794
Decoder::Decode() at decoder.cc:746
main() at cdec.cc:30



取消cdec中phrase長度限制

cdec中有三個參數來表示phrase span的單詞最大個數.

1) 抽取規則時, phrase在training sentences中的最大長度. 該變量的定義在sa-extract/sa-compile.pl中, my $max_size = 15; 可以通過sa-extract/sa-compile.pl -p max-size=10來修改;

2) 還是抽取規則時, phrase在test sentences中的最大長度. 既對於test sentences中的某個phrase, 如果長度超過該值, 那麼不去從training sentences中找匹配規則. 該變量的值在sa-extract/extract.ini文件中設置 (作為rulefactory.HieroCachingRuleFactory()的一個參數):
max_initial_size=15,             # maximum span of a grammar rule in TEST DATA

可以修改為max_initial_size=max_size, 既1)提到的phrase在training sentences中的最大長度. 雖然在sa-extract/extract.ini中也設置了max_size的值, 但該值會被sa-extract/sa-compile.pl中的值覆蓋掉.

3) Decoding的時候, scfg的長度, 在配置文件cdec.ini中設置. 如scfg_max_span_limit=12

往cdec取消phrase span單詞數的限制, 那麼需要修改以上三個參數, 比如:

1) 調用sa-compile.pl時, 設置-p, sa-extract/sa-compile.pl -p max-size=100000
2) 修改sa-extract/extract.ini, 將max_initial_size=15改為max_initial_size=max_size
    或者
    在調用sa-compile.pl生成的extract.ini文件中, 將max_initial_size=15改為max_initial_size=max_size
    (注: sa-extract/extract.ini是一個extract.ini的模板文件, sa-compile.pl輸出的extract.ini文件是以該文件為模板)
3)修改cdec.ini文件, 將scfg_max_span_limit=12改為scfg_max_span_limit=100000

2013年3月4日星期一

使用zpar的分词功能

1) 下载zpar http://faculty.sutd.edu.sg/~yue_zhang/doc/index.html
2) 阅读Chinese word segmentation文档http://faculty.sutd.edu.sg/~yue_zhang/doc/doc/segmenter.html 写得很清楚, zan
3) 编译make segmentor (将生成dict/segmentor/***)
4) 训练
     ./dist/segmentor/train <train-file> <model-file> <number of iterations>
     ./dist/segmentor/train ctb6.0/ctb6.0.train.segmented.txt ctb6.0/ctb6.0.seg.model 1
    (最后一个参数是迭代次数, 可以使用test.sh多次迭代然后选择最优值, 见Chinese word segmentation文档6 How to tune the performance of a system)
    (下载evaluate.py http://faculty.sutd.edu.sg/~yue_zhang/doc/doc/seg_files/evaluate.py)
    (需要修改test.sh里的一些文件名称路径等, 每次迭代会在原先迭代的基础上完成, 即为什么test.sh中总是执行$segmentor/train train.txt model 1)

#####test.sh#####
segmentor=dist/segmentor

rm model

for i in `seq 1 30`;
do
echo "iteration $i"
$segmentor/train ctb6.0/ctb6.0.train.segmented.txt model 1
$segmentor/segmentor model ctb6.0/ctb6.0.deve.unseg.txt ctb6.0.deve.autoseg.txt
python evaluate.py ctb6.0.deve.autoseg.txt ctb6.0/ctb6.0.deve.segmented.txt
cp model ctb6.0/ctb6.0.seg.model.$i
done
迭代18次的log:

239 iteration 18
240
241 Training started ...
242 Loading model ... done (8.39196s).
243 Computing averaged feature scores ... done
244 Saving model ... done.
245 Done.
246 Training has finished successfully. Total time taken is: 49.2729
247 Segmenting started
248 Loading model ... done (8.06785s).
249 Segmenting has finished successfully. Total time taken is: 11.2431
250 Word precision: 0.955604476243
251 Word recall: 0.955700108415
252 Word F-measure: 0.955652289936




5) 测试
    ./dist/segmentor/segmentor ctb6.0/ctb6.0.seg.model.18 ctb6.0/ctb6.0.test.unseg.txt ctb6.0.test.autoseg.txt

python evaluate.py ctb6.0.test.autoseg.txt ctb6.0/ctb6.0.test.segmented.txt

Word precision: 0.951947892344
Word recall: 0.949520704111
Word F-measure: 0.950732749098