Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #47 from shineware/1.2
Browse files Browse the repository at this point in the history
KOMORAN Admin v1.2
  • Loading branch information
9bow authored Nov 13, 2019
2 parents e4cae62 + f1dae1a commit 7fa8ecf
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 29 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ KOMORAN 관리도구는 크게 4가지 기능을 제공하는 메뉴들로 구
* `java -version` 명령어로 현재 버전을 확인할 수 있습니다.
* Chrome 브라우저
* 최신 버전의 Safari, Firefox 등에서도 동작합니다.
* KOMORAN 3.3.5 이상
* 별도로 설치하실 필요는 없습니다.
* 단, 본 관리도구에서 생성한 사전 및 모델은 3.3.5 이상 버전에서 사용 가능합니다.

### 설치 방법

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Map;


Expand Down Expand Up @@ -40,6 +42,22 @@ public ResponseDetail analyzeStr(@RequestParam("strToAnalyze") String strToAnaly
}


@PostMapping(value = "/multiple")
public ResponseDetail analyzeMultipleStrs(@RequestParam("strToAnalyze") String strToAnalyze,
@RequestParam("modelName") String modelNameToUse) {
ModelValidator.CheckValidModelName(modelNameToUse);

ResponseDetail responseDetail = new ResponseDetail();

ArrayList<String> analyzedResults = morphAnalyzeService.analyzeMultipleLinesWithUserModel(strToAnalyze, modelNameToUse);
String result = String.join("\n", analyzedResults);

responseDetail.setData(result);

return responseDetail;
}


@PostMapping(value = "/compare")
public ResponseDetail analyzeStrWithNewModel(@RequestParam("strToAnalyze") String strToAnalyze,
@RequestParam("modelNameSrc") String modelNameSrc,
Expand All @@ -55,4 +73,19 @@ public ResponseDetail analyzeStrWithNewModel(@RequestParam("strToAnalyze") Strin
return responseDetail;
}


@PostMapping(value = "/multicompare")
public ResponseDetail analyzeMultipleStrsWithNewModel(@RequestParam("strToAnalyze") String strToAnalyze,
@RequestParam("modelNameSrc") String modelNameSrc,
@RequestParam("modelNameDest") String modelNameDest) {
ModelValidator.CheckValidModelName(modelNameSrc);
ModelValidator.CheckValidModelName(modelNameDest);

ResponseDetail responseDetail = new ResponseDetail();

Map<String, String> result = morphAnalyzeService.getDiffsFromAnalyzedMultipleResults(strToAnalyze, modelNameSrc, modelNameDest);
responseDetail.setData(result);

return responseDetail;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
import kr.co.shineware.nlp.komoran.admin.util.ModelValidator;
import kr.co.shineware.nlp.komoran.constant.DEFAULT_MODEL;
import kr.co.shineware.nlp.komoran.core.Komoran;
import kr.co.shineware.nlp.komoran.model.KomoranResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

@Service
@Transactional
Expand All @@ -37,6 +35,9 @@ public class MorphAnalyzeService {
@Value("${files.name.fwduser}")
private String filenameFwdUser;

@Value("${komoran.options.numThreads}")
private int numThreads;


private static Komoran komoran;

Expand All @@ -55,6 +56,19 @@ private String analyzeWithLightModel(String strToAnalyze) {
}


private ArrayList<String> analyzeMultipleLinesWithLightModel(String strToAnalyzeWithNewLines) {
String[] splittedStrs = strToAnalyzeWithNewLines.split("\n");
List<KomoranResult> analyzedResults = komoran.analyze(Arrays.asList(splittedStrs), numThreads);
ArrayList<String> results = new ArrayList<String>();

for (KomoranResult analyzedResult : analyzedResults) {
results.add(analyzedResult.getPlainText());
}

return results;
}


private boolean loadUserModel(String modelPathName) {
ModelValidator.CheckValidUserModel(modelPathName);

Expand Down Expand Up @@ -88,13 +102,40 @@ public String analyzeWithUserModel(String strToAnalyze, String userModelName) {
this.loadUserModel(userModelName);
result = this.userKomoran.analyze(strToAnalyze).getPlainText();
} catch (NullPointerException e) {
throw new ServerErrorException("사용자 모델을 이용한 분석 중 에러가 발생하였습니다.\\n사전 문제일 수 있습니다.");
throw new ServerErrorException("사용자 모델을 이용한 분석 중 에러가 발생하였습니다.\n사전 문제일 수 있습니다.");
}

return result;
}


public ArrayList<String> analyzeMultipleLinesWithUserModel(String strToAnalyzeWithNewLines, String userModelName) {
ModelValidator.CheckValidModelName(userModelName);

ArrayList<String> results;

if ("DEFAULT".equals(userModelName)) {
results = this.analyzeMultipleLinesWithLightModel(strToAnalyzeWithNewLines);
return results;
}

try {
this.loadUserModel(userModelName);
String[] splittedStrs = strToAnalyzeWithNewLines.split("\n");
List<KomoranResult> analyzedResults = this.userKomoran.analyze(Arrays.asList(splittedStrs), numThreads);
results = new ArrayList<String>();

for (KomoranResult analyzedResult : analyzedResults) {
results.add(analyzedResult.getPlainText());
}
} catch (NullPointerException e) {
throw new ServerErrorException("사용자 모델을 이용한 분석 중 에러가 발생하였습니다.\n사전 문제일 수 있습니다.");
}

return results;
}


public Map<String, String> getDiffsFromAnalyzedResults(String strToAnalyze, String modelNameSrc, String modelNameDest) {
ModelValidator.CheckValidModelName(modelNameSrc);
ModelValidator.CheckValidModelName(modelNameDest);
Expand Down Expand Up @@ -137,7 +178,6 @@ public Map<String, String> getDiffsFromAnalyzedResults(String strToAnalyze, Stri
resultSrcHtml.append(row.getOldLine());
resultDestHtml.append(row.getNewLine());
}

} catch (DiffException e) {
throw new ServerErrorException("분석 결과 비교 중 문제가 발생하였습니다.");
}
Expand All @@ -148,4 +188,64 @@ public Map<String, String> getDiffsFromAnalyzedResults(String strToAnalyze, Stri
return result;
}


public Map<String, String> getDiffsFromAnalyzedMultipleResults(String strToAnalyzeWithNewLines, String modelNameSrc, String modelNameDest) {
ModelValidator.CheckValidModelName(modelNameSrc);
ModelValidator.CheckValidModelName(modelNameDest);

ArrayList<String> resultSrc;
ArrayList<String> resultDest;
Map<String, String> result = new HashMap<>();

if ("DEFAULT".equals(modelNameSrc)) {
resultSrc = this.analyzeMultipleLinesWithLightModel(strToAnalyzeWithNewLines);
} else {
resultSrc = this.analyzeMultipleLinesWithUserModel(strToAnalyzeWithNewLines, modelNameSrc);
}

if ("DEFAULT".equals(modelNameDest)) {
resultDest = this.analyzeMultipleLinesWithLightModel(strToAnalyzeWithNewLines);
} else {
resultDest = this.analyzeMultipleLinesWithUserModel(strToAnalyzeWithNewLines, modelNameDest);
}

if (resultSrc.size() != resultDest.size()) {
throw new ServerErrorException("KOMORAN 오류가 발생하였습니다.");
}

StringBuffer resultSrcHtml = new StringBuffer();
StringBuffer resultDestHtml = new StringBuffer();
boolean isFirst = true;

DiffRowGenerator generator = DiffRowGenerator.create()
.showInlineDiffs(true)
.inlineDiffByWord(true)
.build();
try {
for (int i = 0; i < resultSrc.size(); i++) {
List<DiffRow> rows = generator.generateDiffRows(Arrays.asList(resultSrc.get(i).split(" ")), Arrays.asList(resultDest.get(i).split(" ")));

for (DiffRow row : rows) {
if (!isFirst) {
resultSrcHtml.append(" ");
resultDestHtml.append(" ");
} else {
isFirst = false;
}

resultSrcHtml.append(row.getOldLine());
resultDestHtml.append(row.getNewLine());
}
resultSrcHtml.append("<br />");
resultDestHtml.append("<br />");
}
} catch (DiffException e) {
throw new ServerErrorException("분석 결과 비교 중 문제가 발생하였습니다.");
}

result.put("srcHtml", resultSrcHtml.toString());
result.put("destHtml", resultDestHtml.toString());

return result;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ files:
fwduser: fwd.user
irregular: dic.irregular

komoran:
options:
numThreads: 4

models:
default:
Expand Down
15 changes: 9 additions & 6 deletions src/main/resources/static/analyze.html
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<li><a href="./dicuser.html"><i class="fa fa-book"></i> <span>사용자 사전 관리</span></a></li>
<li><a href="./fwduser.html"><i class="fa fa-book"></i> <span>기분석 사전 관리</span></a></li>
<li class="header">비교</li>
<li><a href="./compare.html"><i class="fa fa-arrows-h"></i> <span>형태소 분석 비교</span></a></li>
<li><a href="./compareStr.html"><i class="fa fa-arrows-h"></i> <span>문장 단위 비교</span></a></li>
<li class="header">배포</li>
<li><a href="./models.html"><i class="fa fa-code"></i> <span>모델 관리 및 배포</span></a></li>
<li class="header">참조</li>
Expand Down Expand Up @@ -113,7 +113,7 @@ <h1>

<div class="form-group">
<label for="strToAnalyze"><h4>입력 문장</h4></label>
<input type="text" class="form-control" id="strToAnalyze" name="strToAnalyze" placeholder="형태소 분석할 문장을 입력하세요.">
<textarea class="form-control" id="strToAnalyze" name="strToAnalyze" rows="5" placeholder="형태소 분석할 문장을 입력하세요."></textarea>
</div>

<div class="form-group">
Expand Down Expand Up @@ -177,11 +177,14 @@ <h1>
// Analyze
$("#analyzeBtn").on("click", analyzeForm);

$("#strToAnalyze").on("keypress", function(e) {
if(e.which == 13) {
$("#strToAnalyze").on("keypress", submitOnEnter);
$("#selectModel").on("change", analyzeForm);

function submitOnEnter(e) {
if (e.which == 13) {
analyzeForm();
}
});
}

function analyzeForm() {
var passStrToAnalyze = false;
Expand All @@ -198,7 +201,7 @@ <h1>
if (passStrToAnalyze) {
$.ajax({
method: "POST",
url: "/analyze/default",
url: "/analyze/multiple",
data: {
strToAnalyze: $("#strToAnalyze").val(),
modelName: $("#selectModel").val()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<li><a href="./dicuser.html"><i class="fa fa-book"></i> <span>사용자 사전 관리</span></a></li>
<li><a href="./fwduser.html"><i class="fa fa-book"></i> <span>기분석 사전 관리</span></a></li>
<li class="header">비교</li>
<li class="active"><a href="./compare.html"><i class="fa fa-arrows-h"></i> <span>형태소 분석 비교</span></a></li>
<li class="active"><a href="./compareStr.html"><i class="fa fa-arrows-h"></i> <span>문장 단위 비교</span></a></li>
<li class="header">배포</li>
<li><a href="./models.html"><i class="fa fa-code"></i> <span>모델 관리 및 배포</span></a></li>
<li class="header">참조</li>
Expand Down Expand Up @@ -108,11 +108,11 @@ <h1>

<div class="box">
<div class="box-body">
<p><strong>[사용법]</strong> 분석을 원하는 문장 입력 후, 모델을 선택하여 형태소 분석 결과를 비교할 수 있습니다. [모델 생성] 버튼을 눌러 현재 사전 내용으로 신규 모델을 생성할 수 있습니다.</p>
<p><strong>[사용법]</strong> 분석을 원하는 문장 입력 후, 모델을 선택하여 형태소 분석의 전체 결과를 비교할 수 있습니다. [모델 생성] 버튼을 눌러 현재 사전 내용으로 신규 모델을 생성할 수 있습니다.</p>

<div class="form-group">
<label for="strToAnalyze"><h4>입력 문장</h4></label>
<input type="text" class="form-control" id="strToAnalyze" name="strToAnalyze" placeholder="형태소 분석할 문장을 입력하세요.">
<textarea class="form-control" id="strToAnalyze" name="strToAnalyze" rows="5" placeholder="형태소 분석할 문장을 입력하세요."></textarea>
</div>
<div class="row btn-row">
<div class="pull-left">
Expand Down Expand Up @@ -211,11 +211,15 @@ <h1>
$("#analyzeBtn").on("click", analyzeAndCompare);
$("#anotherAnalyzeBtn").on("click", analyzeAndCompare);

$("#strToAnalyze").on("keypress", function(e) {
if(e.which == 13) {
$("#strToAnalyze").on("keypress", submitOnEnter);
$("#selectSrcModel").on("change", analyzeAndCompare);
$("#selectDestModel").on("change", analyzeAndCompare);

function submitOnEnter(e) {
if (e.which == 13) {
analyzeAndCompare();
}
});
}

$(function () {
// Initialize Model List
Expand Down Expand Up @@ -260,7 +264,7 @@ <h1>
if (passStrToAnalyze) {
$.ajax({
method: "POST",
url: "/analyze/compare",
url: "/analyze/multicompare",
data: {
strToAnalyze: $("#strToAnalyze").val(),
modelNameSrc: $("#selectSrcModel").val(),
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/static/dicuser.html
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<li class="active"><a href="./dicuser.html"><i class="fa fa-book"></i> <span>사용자 사전 관리</span></a></li>
<li><a href="./fwduser.html"><i class="fa fa-book"></i> <span>기분석 사전 관리</span></a></li>
<li class="header">비교</li>
<li><a href="./compare.html"><i class="fa fa-arrows-h"></i> <span>형태소 분석 비교</span></a></li>
<li><a href="./compareStr.html"><i class="fa fa-arrows-h"></i> <span>문장 단위 비교</span></a></li>
<li class="header">배포</li>
<li><a href="./models.html"><i class="fa fa-code"></i> <span>모델 관리 및 배포</span></a></li>
<li class="header">참조</li>
Expand Down Expand Up @@ -295,7 +295,7 @@ <h4 class="modal-title" id="dictAddItemModalTitle">새로운 단어 등록하기
$("#dictAddItemModalPosSelect").on("keypress", submitOnEnter);

function submitOnEnter(e) {
if(e.which == 13) {
if (e.which == 13) {
addNewItem();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/static/dicword.html
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<li><a href="./dicuser.html"><i class="fa fa-book"></i> <span>사용자 사전 관리</span></a></li>
<li><a href="./fwduser.html"><i class="fa fa-book"></i> <span>기분석 사전 관리</span></a></li>
<li class="header">비교</li>
<li><a href="./compare.html"><i class="fa fa-arrows-h"></i> <span>형태소 분석 비교</span></a></li>
<li><a href="./compareStr.html"><i class="fa fa-arrows-h"></i> <span>문장 단위 비교</span></a></li>
<li class="header">배포</li>
<li><a href="./models.html"><i class="fa fa-code"></i> <span>모델 관리 및 배포</span></a></li>
<li class="header">참조</li>
Expand Down Expand Up @@ -308,7 +308,7 @@ <h4 class="modal-title" id="dictAddItemModalTitle">새로운 단어 등록하기
$("#dictAddItemModalTfInput").on("keypress", submitOnEnter);

function submitOnEnter(e) {
if(e.which == 13) {
if (e.which == 13) {
addNewItem();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/static/fwduser.html
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<li><a href="./dicuser.html"><i class="fa fa-book"></i> <span>사용자 사전 관리</span></a></li>
<li class="active"><a href="./fwduser.html"><i class="fa fa-book"></i> <span>기분석 사전 관리</span></a></li>
<li class="header">비교</li>
<li><a href="./compare.html"><i class="fa fa-arrows-h"></i> <span>형태소 분석 비교</span></a></li>
<li><a href="./compareStr.html"><i class="fa fa-arrows-h"></i> <span>문장 단위 비교</span></a></li>
<li class="header">배포</li>
<li><a href="./models.html"><i class="fa fa-code"></i> <span>모델 관리 및 배포</span></a></li>
<li class="header">참조</li>
Expand Down Expand Up @@ -291,7 +291,7 @@ <h4 class="modal-title" id="dictAddItemModalTitle">새로운 기분석 어절
$("#dictAddItemModalAnalyzedInput").on("keypress", submitOnEnter);

function submitOnEnter(e) {
if(e.which == 13) {
if (e.which == 13) {
addNewItem();
}
}
Expand Down
Loading

0 comments on commit 7fa8ecf

Please sign in to comment.