Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
test
  • Loading branch information
kim1938 committed May 16, 2023
1 parent 82c948b commit 5e3b82c
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 64 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified board_exam_project-master/.gradle/checksums/checksums.lock
Binary file not shown.
Binary file modified board_exam_project-master/.gradle/checksums/md5-checksums.bin
Binary file not shown.
Binary file modified board_exam_project-master/.gradle/checksums/sha1-checksums.bin
Binary file not shown.
7 changes: 5 additions & 2 deletions board_exam_project-master/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ dependencies {
implementation 'org.springframework:spring-web:5.3.9'

// 자바-파이써 인데 안쓸듯
implementation group: 'org.python', name: 'jython-slim', version: '2.7.3'


// pdf text 추출
implementation 'org.apache.pdfbox:pdfbox:2.0.24'

// 명사 추출하는데 사용

implementation 'com.squareup.okhttp3:okhttp:4.9.3'
implementation 'org.json:json:20210307'
implementation 'org.apache.httpcomponents:httpclient:4.5.13'

//

implementation 'com.github.shin285:KOMORAN:3.3.9'
}

test {
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
margin: 10px;
}
</style>




</head>
<body>
<!--<div th:replace="fragments/header :: header"></div>-->
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.study.board;

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 kr.co.shineware.nlp.komoran.model.Token;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

Expand All @@ -14,20 +11,6 @@
public class BoardApplication {

public static void main(String[] args) {

Komoran komoran = new Komoran(DEFAULT_MODEL.FULL);
String strToAnalyze = "대한민국은 민주공화국이다.";

KomoranResult analyzeResultList = komoran.analyze(strToAnalyze);

System.out.println(analyzeResultList.getPlainText());

List<Token> tokenList = analyzeResultList.getTokenList();
for (Token token : tokenList) {
System.out.format("(%2d, %2d) %s/%s\n", token.getBeginIndex(), token.getEndIndex(), token.getMorph(), token.getPos());
}


SpringApplication.run(BoardApplication.class, args);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,32 @@
import com.study.board.entity.Freeboard;
import com.study.board.service.FreeboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

@Controller
import java.util.List;

//@Controller
//@RequestMapping("/freeboard")
@RestController
public class FreeboardController {

@Autowired
private FreeboardService freeboardService;

@GetMapping("/list")
/* @GetMapping("/list")
public String list(Model model) {
model.addAttribute("freeboards", freeboardService.getAllFreeboards());
return "freeboard/list";
}
}*/
@GetMapping(value = "/list")
public List<Freeboard> list() {

return freeboardService.getAllFreeboards();
}
@GetMapping("/add")
public String add(Model model) {
model.addAttribute("freeboard", new Freeboard());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package com.study.board.controller;

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 kr.co.shineware.nlp.komoran.model.Token;

import org.apache.http.HttpResponse;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.json.JSONException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.*;
import java.util.*;
import java.io.File;
import java.io.IOException;

import org.json.JSONObject;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;


//
@Controller
public class PdftextController {

@GetMapping("/pdftext")
public String pdftext() {
Random random = new Random();
PDDocument document = null;
String path = "C:\\asdasd\\test3.pdf";
String outputFilePath = "C:\\asdasd\\test3.txt";
Expand All @@ -26,47 +33,46 @@ public String pdftext() {
document = PDDocument.load(new File(path));
PDFTextStripper stripper = new PDFTextStripper();
int numPages = document.getNumberOfPages();
Komoran komoran = new Komoran(DEFAULT_MODEL.FULL);
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath));

writer.write("Number of pages: " + numPages + "\n"); // write number of pages to file
// Create HttpClient and HttpPost objects
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("http://localhost:5000/endpoint");
httpPost.setHeader("Content-type", "application/json");

for (int pageNum = 1; pageNum <= numPages; pageNum++) {
stripper.setStartPage(pageNum);
stripper.setEndPage(pageNum);
String pageText = stripper.getText(document);

KomoranResult analyzeResultList = komoran.analyze(pageText);
List<Token> tokenList = analyzeResultList.getTokenList();

writer.write("Processed text from page " + pageNum + ":\n"); // write page number to file
int numNouns = 0; // keep track of the number of nouns encountered
Map<Integer, Token> nounPositions = new HashMap<>(); // keep track of the positions of nouns
for (Token token : tokenList) {
if (token.getPos().startsWith("N")) { // check if token is a noun
numNouns++;
if (numNouns <= 3) { // only change the first 3 nouns encountered
nounPositions.put(token.getBeginIndex(), token);
} else {
// write the noun as is
writer.write(String.format("%s ", token.getMorph()));
}
} else {
// write non-nouns as is
writer.write(String.format("%s ", token.getMorph()));
}
// Create JSONObject to hold extracted text
JSONObject json = null;
try {
json = new JSONObject();
json.put("page", pageNum);
json.put("text", pageText);
} catch (JSONException e) {
e.printStackTrace();
return "error";
}
writer.write("\nOriginal text:\n");
for (int i = 0; i < pageText.length(); i++) {
if (nounPositions.containsKey(i)) {
writer.write(String.format("(%s)", "----")); // write "(----)" instead of the noun
} else {
writer.write(String.format("%s", pageText.charAt(i))); // write non-noun character
}

// Convert JSONObject to string
String jsonString = json.toString();
System.out.println(jsonString);
// Set JSON string as entity of HttpPost request
StringEntity stringEntity = new StringEntity(jsonString);
httpPost.setEntity(stringEntity);

// Execute HttpPost request
HttpResponse response = httpClient.execute(httpPost);

// Check the response code
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode >= 200 && statusCode < 300) {
System.out.println("HTTP POST request for page " + pageNum + " succeeded with status code " + statusCode);
} else {
System.out.println("HTTP POST request for page " + pageNum + " failed with status code " + statusCode);
}
writer.write("\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
Expand All @@ -89,6 +95,4 @@ public String pdftext() {





}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public class RestController {

@GetMapping("/data")
public String getData() {
return "Hello from Spring!";
return "Hello from ㅁㄴㅇㅁㅇㅁㅇㅁㄴㅇ!";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
margin: 10px;
}
</style>




</head>
<body>
<!--<div th:replace="fragments/header :: header"></div>-->
Expand Down

0 comments on commit 5e3b82c

Please sign in to comment.