2014-05-12 2 views
0

나는 opennlp에 익숙하지 않고 파일에서 도시 이름을 추출하는 프로그램을 구현하려고하지만 문자열에 대한 코드를 먼저 테스트하고 일부 오류가 발생한다. 코드는opennlp 이클립스의 스팬 클래스

이다.
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;