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

Commit

Permalink
여러 문장의 형태소 분석 지원 related #45
Browse files Browse the repository at this point in the history
  • Loading branch information
9bow committed Nov 11, 2019
1 parent 3d7dcba commit f1dae1a
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 17 deletions.
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
5 changes: 2 additions & 3 deletions src/main/resources/static/analyze.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
<li><a href="./fwduser.html"><i class="fa fa-book"></i> <span>기분석 사전 관리</span></a></li>
<li class="header">비교</li>
<li><a href="./compareStr.html"><i class="fa fa-arrows-h"></i> <span>문장 단위 비교</span></a></li>
<li><a href="./compareFile.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 @@ -114,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 @@ -202,7 +201,7 @@ <h1>
if (passStrToAnalyze) {
$.ajax({
method: "POST",
url: "/analyze/default",
url: "/analyze/multiple",
data: {
strToAnalyze: $("#strToAnalyze").val(),
modelName: $("#selectModel").val()
Expand Down
3 changes: 1 addition & 2 deletions src/main/resources/static/compareStr.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
<li><a href="./fwduser.html"><i class="fa fa-book"></i> <span>기분석 사전 관리</span></a></li>
<li class="header">비교</li>
<li class="active"><a href="./compareStr.html"><i class="fa fa-arrows-h"></i> <span>문장 단위 비교</span></a></li>
<li><a href="./compareFile.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 @@ -265,7 +264,7 @@ <h1>
if (passStrToAnalyze) {
$.ajax({
method: "POST",
url: "/analyze/compare",
url: "/analyze/multicompare",
data: {
strToAnalyze: $("#strToAnalyze").val(),
modelNameSrc: $("#selectSrcModel").val(),
Expand Down
1 change: 0 additions & 1 deletion src/main/resources/static/dicuser.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
<li><a href="./fwduser.html"><i class="fa fa-book"></i> <span>기분석 사전 관리</span></a></li>
<li class="header">비교</li>
<li><a href="./compareStr.html"><i class="fa fa-arrows-h"></i> <span>문장 단위 비교</span></a></li>
<li><a href="./compareFile.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
1 change: 0 additions & 1 deletion src/main/resources/static/dicword.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
<li><a href="./fwduser.html"><i class="fa fa-book"></i> <span>기분석 사전 관리</span></a></li>
<li class="header">비교</li>
<li><a href="./compareStr.html"><i class="fa fa-arrows-h"></i> <span>문장 단위 비교</span></a></li>
<li><a href="./compareFile.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
1 change: 0 additions & 1 deletion src/main/resources/static/fwduser.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
<li class="active"><a href="./fwduser.html"><i class="fa fa-book"></i> <span>기분석 사전 관리</span></a></li>
<li class="header">비교</li>
<li><a href="./compareStr.html"><i class="fa fa-arrows-h"></i> <span>문장 단위 비교</span></a></li>
<li><a href="./compareFile.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
1 change: 0 additions & 1 deletion src/main/resources/static/grammarin.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
<li><a href="./fwduser.html"><i class="fa fa-book"></i> <span>기분석 사전 관리</span></a></li>
<li class="header">비교</li>
<li><a href="./compareStr.html"><i class="fa fa-arrows-h"></i> <span>문장 단위 비교</span></a></li>
<li><a href="./compareFile.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
1 change: 0 additions & 1 deletion src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
<li><a href="./fwduser.html"><i class="fa fa-book"></i> <span>기분석 사전 관리</span></a></li>
<li class="header">비교</li>
<li><a href="./compareStr.html"><i class="fa fa-arrows-h"></i> <span>문장 단위 비교</span></a></li>
<li><a href="./compareFile.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
1 change: 0 additions & 1 deletion src/main/resources/static/models.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
<li><a href="./fwduser.html"><i class="fa fa-book"></i> <span>기분석 사전 관리</span></a></li>
<li class="header">비교</li>
<li><a href="./compareStr.html"><i class="fa fa-arrows-h"></i> <span>문장 단위 비교</span></a></li>
<li><a href="./compareFile.html"><i class="fa fa-arrows-h"></i> <span>파일 단위 비교</span></a></li>
<li class="header">배포</li>
<li class="active"><a href="./models.html"><i class="fa fa-code"></i> <span>모델 관리 및 배포</span></a></li>
<li class="header">참조</li>
Expand Down

0 comments on commit f1dae1a

Please sign in to comment.