2014-05-12 89 views
0

我是新來的java opennlp,我想實現一個程序,從文件中提取城市名稱,但我測試我的代碼在字符串上,我得到一些錯誤 代碼是opennlp在Eclipse日曆類java

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 

import main.java.opennlp.tools.namefind.NameFinderME; 
import main.java.opennlp.tools.namefind.TokenNameFinderModel; 
import main.java.opennlp.tools.util.InvalidFormatException; 
import main.java.opennlp.tools.util.Span; 
import opennlp.tools.tokenize.Tokenizer; 
import opennlp.tools.tokenize.TokenizerME; 
import opennlp.tools.tokenize.TokenizerModel; 
import opennlp.tools.tokenize.SimpleTokenizer; 
import opennlp.tools.sentdetect.SentenceDetectorME; 
import opennlp.tools.sentdetect.SentenceModel; 

    import org.xml.sax.SAXException; 

public class CityFinder { 

public String Tokens[]; 

public static void main(String[] args) throws IOException, SAXException { 

    CityFinder toi = new CityFinder(); 
    String cnt; 
    cnt="John is planning to specialize in Electrical Engineering in UC Berkley and pursue a career with IBM."; 
    toi.tokenization(cnt); 
    String cities = toi.namefind(toi.Tokens); 
    String org = toi.orgfind(toi.Tokens); 

    System.out.println("City name is : "+cities); 
    System.out.println("organization name is: "+org); 

} 
    public String namefind(String cnt[]) { 
    InputStream is; 
    TokenNameFinderModel tnf; 
    NameFinderME nf; 
    String sd = ""; 
    try { 
     is = new FileInputStream("en-ner-location.bin"); 
     tnf = new TokenNameFinderModel(is); 
     nf = new NameFinderME(tnf); 
     Span sp[] = nf.find(cnt); 
     StringBuilder fd = new StringBuilder(); 
     int l = a.length; 

     for (int j = 0; j < l; j++) { 
      fd = fd.append(a[j] + "\n"); 

     } 
     sd = fd.toString(); 

    } catch (FileNotFoundException e) { 

     e.printStackTrace(); 
    } catch (InvalidFormatException e) { 

     e.printStackTrace(); 
    } catch (IOException e) { 

     e.printStackTrace(); 
    } 
    return sd; 
} 

public String orgfind(String cnt[]) { 
    InputStream is; 
    TokenNameFinderModel tnf; 
    NameFinderME nf; 
    String sd = ""; 
    try { 
     is = new FileInputStream("en-ner-organization.bin"); 
     tnf = new TokenNameFinderModel(is); 
     nf = new NameFinderME(tnf); 
     Span sp[] = nf.find(cnt); 
     String a[] = Span.spansToStrings(sp, cnt); 
     StringBuilder fd = new StringBuilder(); 
     int l = a.length; 
     for (int j = 0; j < l; j++) { 
      fd = fd.append(a[j] + "\n"); 

     } 

     sd = fd.toString(); 

    } catch (FileNotFoundException e) { 

     e.printStackTrace(); 
    } catch (InvalidFormatException e) { 

     e.printStackTrace(); 
    } catch (IOException e) { 

     e.printStackTrace(); 
    } 
    return sd; 

} 
public void tokenization(String tokens) { 

    InputStream is; 
    TokenizerModel tm; 
    try { 
     is = new FileInputStream("en-token.bin"); 
     tm = new TokenizerModel(is); 
     Tokenizer tz = new TokenizerME(tm); 
     Tokens = tz.tokenize(tokens); 
     // System.out.println(Tokens[1]); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

    } 

和我得到的錯誤與跨度verialble 跨度sp [] = nf.find(cnt); 錯誤是類型不匹配:不能從opennlp.tools.util.Span []轉換到main.java.opennlp.tools.util.Span [] 我不知道如何解決它在兩個位置

有什麼建議麼....?? 在此先感謝

回答

0

您應該從opennlp.tools包中的軟件包中導入所有類。

import opennlp.tools.namefind.NameFinderME; 
import opennlp.tools.namefind.TokenNameFinderModel; 
import opennlp.tools.util.InvalidFormatException; 
import opennlp.tools.util.Span;