2014-04-14 3 views
2

훈련하는 방법 : 나는 트윗을 분류 maxent 분류기를 사용하려는maxent 분류를

[프로젝트 스택 자바, Opennlp, Elasticsearch (데이터 저장소)를 twitter4j는 트위터에서 데이터를 읽기]. 초기 단계는 모델을 훈련하는 것임을 이해합니다. 문서에서 나는 모델을 훈련시키는 GISTrainer 기반의 열차 방법을 발견했습니다. OpenNlp의 최대 분류자를 사용하여 모델을 학습하고 결과를 예측하는 간단한 코드를 작성했습니다.

나는 두 개의 파일을 사용하고

모델을 positive.txt의

내용 negative.txt의

positive This is good 
positive This is the best 
positive This is fantastic 
positive This is super 
positive This is fine 
positive This is nice 

내용

negative This is bad 
negative This is ugly 
negative This is the worst 
negative This is worse 
negative This sucks 

그리고 훈련을 postive.txt 및 negative.txt 아래의 자바 메소드는 결과를 생성합니다.

@Override 
public void trainDataset(String source, String destination) throws Exception { 
    File[] inputFiles = FileUtil.buildFileList(new File(source)); // trains both positive and negative.txt 
    File modelFile = new File(destination); 
    Tokenizer tokenizer = SimpleTokenizer.INSTANCE; 
    CategoryDataStream ds = new CategoryDataStream(inputFiles, tokenizer); 
    int cutoff = 5; 
    int iterations = 100; 
    BagOfWordsFeatureGenerator bowfg = new BagOfWordsFeatureGenerator(); 
    DoccatModel model = DocumentCategorizerME.train("en", ds, cutoff,iterations, bowfg); 
    model.serialize(new FileOutputStream(modelFile)); 
} 

@Override 
public void predict(String text, String modelFile) { 
    InputStream modelStream = null; 
    try{ 
     Tokenizer tokenizer = SimpleTokenizer.INSTANCE; 
     String[] tokens = tokenizer.tokenize(text); 
     modelStream = new FileInputStream(modelFile); 
     DoccatModel model = new DoccatModel(modelStream); 
     BagOfWordsFeatureGenerator bowfg = new BagOfWordsFeatureGenerator(); 
     DocumentCategorizer categorizer = new DocumentCategorizerME(model, bowfg); 
     double[] probs = categorizer.categorize(tokens); 
     if(null!=probs && probs.length>0){ 
      for(int i=0;i<probs.length;i++){ 
       System.out.println("double[] probs index " + i + " value " + probs[i]); 
      } 
     } 
     String label = categorizer.getBestCategory(probs); 
     System.out.println("label " + label); 
     int bestIndex = categorizer.getIndex(label); 
     System.out.println("bestIndex " + bestIndex); 
     double score = probs[bestIndex]; 
     System.out.println("score " + score); 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
    } 
    finally{ 
     if(null!=modelStream){ 
      try { 
       modelStream.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

public static void main(String[] args) { 
    try { 
     String outputModelPath = "/home/**/sd-sentiment-analysis/models/trainPostive"; 
     String source = "/home/**/sd-sentiment-analysis/sd-core/src/main/resources/datasets/"; 
     MaximunEntropyClassifier me = new MaximunEntropyClassifier(); 
     me.trainDataset(source, outputModelPath); 
     me.predict("This is bad", outputModelPath); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

다음 질문이 있습니다.

1) 모델을 반복적으로 훈련시키는 방법은 무엇입니까? 또한 모델에 새 문장/단어를 추가하려면 어떻게합니까? 데이터 파일의 특정 형식이 있습니까? 파일에 탭으로 구분 된 최소 두 단어가 있어야 함을 발견했습니다. 내 이해가 유효합니까? 2) 모델을 교육하는 데 사용할 수있는 공개 데이터 세트가 있습니까? 영화 리뷰를위한 소스를 찾았습니다. 내가하고있는 프로젝트는 영화 리뷰뿐 아니라 제품 리뷰, 브랜드 감성 등 다른 것들도 포함합니다. 3) This은 어느 정도 도움이됩니다. 공개적으로 사용할 수있는 예제가 있습니까? maxent에 대한 설명서를 찾을 수 없습니다.

도와주세요. 나는 이것에 막상 막혔다.

답변

0

1) 데이터베이스에 샘플을 저장할 수 있습니다. 나는 이것을 위해 한 번 accumulo를 사용했다. 그런 다음 일정한 간격으로 모델을 다시 작성하고 데이터를 다시 처리합니다. 2) 형식은 : categoryname space sample newline입니다. 탭이 없음 3) 일반적인 정서와 주제 또는 엔티티를 결합하려는 것처럼 들립니다. 네임 파인더 또는 그냥 정규식을 사용하여 엔티티를 찾거나 클래스 이름에 엔티티를 추가 할 수 있습니다. 제품 이름 등을 포함하면 샘플이 매우 구체적이어야합니다.

+0

안녕하세요 마크, 답장을 보내 감사 :

여기 내 최소한의 작업 예를 살펴 보자. 아이디어는 특정 개체의 정서를 포착하는 것입니다. 엔티티는 브랜드, 제품 또는 회사 일 수 있습니다. 나는 데이터 형식으로 놀았고, 데이터가 카테고리의 탭으로 분리 된 경우에만 작동했습니다. 현재 https://opennlp.apache.org/documentation/1.5.3/manual/opennlp.html#opennlp을 검토 중입니다.ml 최대 또는 순진 베이를 사용하는 작업 분류기가 있습니까? –

0

AFAIK, 새로운 훈련 샘플을 추가하려면 MaxEnt 모델을 재교육하십시오. 그것은 온라인으로을 점진적으로 수행 할 수 없습니다.

opennlp maxent의 기본 입력 형식은 텍스트 파일로서 각 행은 단일 샘플을 나타냅니다. 샘플은 공백으로 구분 된 토큰 (기능)으로 구성됩니다. 훈련 중 첫 번째 토큰은 결과를 나타냅니다. Training models using openNLP maxent

관련 문제