[問題] 一個程式的比對問題

看板Perl作者 (成就你的大事)時間18年前 (2007/03/26 02:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/2 (看更多)
Q.若我想將檔案A中的文字中出現的 /JJ /DET /CD /RB 將含有這些字的詞拿掉? 但若出現某些字則不進行取代的動作(在此假設不只三個而) ex: binding interact bound 某一個檔案A內有下列的資料: binds/NNS to/TO kissing/JJ -/PPS rich/JJ region/NN presumably/RB derepresses/NNS from/IN the/DET PTN/NNP interaction/NN site/NN the/DET two/CD PTN/NNP -/PPS binding/JJ sites/NNS ------------------------------------------------------------------ 希望處理後: binds/NNS to/TO -/PPS region/NN presumably/RB derepresses/NNS from/IN PTN/NNP interaction/NN site/NN PTN/NNP -/PPS binding/JJ sites/NNS <--此處沒刪掉binding/JJ 是因為出現了binding interact bound -------------------------------------- 目前的想法是先進行檔案A的讀入後進行比對動作 open(LINE,$file) or die "No file"; $ActionPattern="binding|interact|bound"; while(<LINE>){ if($_=~ /(w+)(\/JJ|\/RB|\/DET|\/CD)/g){ #先進行句中有些其中的字詞比對 #如果$1則寫入檔案,但要如何在這邊加入s///g;取代的動作呢? print NEWLINE "$_" unless($1=~ /\b($ActionPattern)\b/g) ; } else{ print NEWLINE "$_"; } } 或者是大家還有其它的方法或意見,可供參考的嗎^^? 不好意思又來讓大家動動腦筋了^^ -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.142.234
文章代碼(AID): #161hZZkO (Perl)
文章代碼(AID): #161hZZkO (Perl)