훈련하는 방법 : 나는 트윗을 분류 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에 대한 설명서를 찾을 수 없습니다.
도와주세요. 나는 이것에 막상 막혔다.
안녕하세요 마크, 답장을 보내 감사 :
여기 내 최소한의 작업 예를 살펴 보자. 아이디어는 특정 개체의 정서를 포착하는 것입니다. 엔티티는 브랜드, 제품 또는 회사 일 수 있습니다. 나는 데이터 형식으로 놀았고, 데이터가 카테고리의 탭으로 분리 된 경우에만 작동했습니다. 현재 https://opennlp.apache.org/documentation/1.5.3/manual/opennlp.html#opennlp을 검토 중입니다.ml 최대 또는 순진 베이를 사용하는 작업 분류기가 있습니까? –