2015-04-03 123 views
2

我已標記的一個簡單的句子,這是我的代碼:編輯配置文件

package tagger; 

import edu.stanford.nlp.tagger.maxent.MaxentTagger; 

public class myTag { 

public static void main(String[] args) { 

    MaxentTagger tagger = new MaxentTagger("D:/tagger/english-bidirectional-distsim.tagger"); 


    String sample = "i go to school by bus"; 

    String tagged = tagger.tagString(sample); 

    System.out.println(tagged); 
} 

} 

這是輸出:

Reading POS tagger model from D:/tagger/english-bidirectional-distsim.tagger ... done [3.0 sec]. 
i_LS go_VB to_TO school_NN by_IN bus_NN 

編輯屬性文件後它不完全有效果。 例如我已經將標記分隔符更改爲(*),但在輸出中它仍然打印(_)。

如何在eclipse中使用模型配置文件?在直接props對象

Properties props = new Properties(); 
props.load(new FileReader("path/to/properties")); 
MaxentTagger tagger = new MaxentTagger("D:/tagger/english-bidirectional-distsim.tagger", props); 

您還可以設置屬性:

props.setProperty("tagSeparator", "*"); 

NB:

+1

請注意,雙向模型[通常不是在實際應用中使用的最佳模型](http://nlp.stanford.edu/software/pos-tagger-faq.shtml#h)。還有其他一些模型爲了大幅提高速度而犧牲了少量精度。 – 2015-04-03 12:46:17

回答

1

您可以加載屬性文件,並將其傳遞到最大墒的構造函數,這樣的事情,如果你使用原始的屬性文件,它會失敗,例外如

java.io.FileNotFoundException: /u/nl 
p/data/pos_tags_are_useless/egw4-reut.512.clusters (No such file or directory) 

然後刪除archtrainFile屬性。

+0

你能解釋更多關於第一個解決方案>>虛擬機參數嗎? – 2015-04-03 10:27:13

+0

究竟是什麼來解釋? – 2015-04-03 10:53:23

+0

第二個解決方案工作正常,但我想更改屬性文件中的多個參數,所以在這種情況下,第一個解決方案更好。問題是如何將屬性文件作爲參數傳遞給程序? – 2015-04-03 11:03:33

0

不用爲此編寫java代碼,您可以使用下載的ZIP文件中包含的bash文件。 提取postagger的ZIP文件後,編輯以下的bash文件:

stanford-postagger.sh

應該有下面這行:

java -mx300m -cp 'stanford-postagger.jar:lib/*' edu.stanford.nlp.tagger.maxent.MaxentTagger -model $1 -textFile $2 

添加一個名爲「參數-tagSeparator [YourTag]」-model $ 1「

java -mx300m -cp 'stanford-postagger.jar:lib/*' edu.stanford.nlp.tagger.maxent.MaxentTagger -model $1 -tagSeparator * -textFile $2 

要運行它(確保必要的權限給出):

./stanford-postagger.sh models/model_name.tagger in_filename > out_filename 

瞧!