Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] documentAi 응답값 수정 #38

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.gdsc.hearo.domain.item.dto.WishRequestDto;
import com.gdsc.hearo.domain.item.dto.WishResponseDto;
import com.gdsc.hearo.domain.item.service.WishService;
import com.gdsc.hearo.global.common.BaseException;
import com.gdsc.hearo.global.common.BaseResponse;
import com.gdsc.hearo.global.common.BaseResponseStatus;
import com.gdsc.hearo.global.security.CustomUserDetails;
Expand Down Expand Up @@ -35,9 +34,6 @@ public ResponseEntity<BaseResponse<WishResponseDto>> addToWishList(@Authenticati
Long userId = userDetails.getMember().getMemberId();
WishResponseDto result = wishService.addToWishList(userId, wishRequestDto.getItemId());




response = new BaseResponse<>(BaseResponseStatus.SUCCESS, result);
return ResponseEntity.ok(response);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,92 +77,85 @@ public String execute(String filePath)
// Get all of the document text as one big string
String text = documentResponse.getText();

// Read the text recognition output from the processor
//System.out.println("The document contains the following paragraphs:");
Document.Page firstPage = documentResponse.getPages(0);
List<Document.Page.Paragraph> paragraphs = firstPage.getParagraphsList();

// Read the text recognition output from the processor
List<Document.Page> pages = documentResponse.getPagesList();
//System.out.printf("There are %s page(s) in this document.\n", pages.size());
System.out.printf("There are %s page(s) in this document.\n", pages.size());

for (Document.Page page : pages) {
System.out.printf("\n\n**** Page %d ****\n", page.getPageNumber());

List<Document.Page.Table> tables = page.getTablesList();

//영양성분표 없는 상황 추가해야함

System.out.printf("Found %d table(s):\n", tables.size());
for (Document.Page.Table table : tables) {
nutritionText += printTableInfo(table, text);
nutritionText += extractTableContents(table, documentResponse.getText());
}

List<Document.Page.FormField> formFields = page.getFormFieldsList();
for (Document.Page.FormField formField : formFields) {
String fieldName = getLayoutText(formField.getFieldName().getTextAnchor(), text);
String fieldValue = getLayoutText(formField.getFieldValue().getTextAnchor(), text);

System.out.printf(
" '%s': '%s'\n", removeNewlines(fieldName), removeNewlines(fieldValue));
nutritionText += String.format(" '%s': '%s'", removeNewlines(fieldName), removeNewlines(fieldValue));
}
// ... (Other code for additional processing, if needed)
}
}
return nutritionText;
}

private static byte[] readImageDataFromUrl(String imageUrl) throws IOException {
URL url = new URL(imageUrl);

try (InputStream inputStream = url.openStream()) {
// Dynamically adjust the buffer size based on the available data
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] tempBuffer = new byte[8192];
int bytesRead;
while ((bytesRead = inputStream.read(tempBuffer)) != -1) {
outputStream.write(tempBuffer, 0, bytesRead);
}
return outputStream.toByteArray();
}
return nutritionText;
}

private static String printTableInfo(Document.Page.Table table, String text) {

String tableText = "";
Document.Page.Table.TableRow firstBodyRow = table.getBodyRows(0);
int columnCount = firstBodyRow.getCellsCount();
/*System.out.printf(
" Table with %d columns and %d rows:\n", columnCount, table.getBodyRowsCount());*/
private static String extractTableContents(Document.Page.Table table, String text) {

String tableText="";
Document.Page.Table.TableRow headerRow = table.getHeaderRows(0);
// Extract and print header
StringBuilder headerRowText = new StringBuilder();
for (Document.Page.Table.TableCell cell : headerRow.getCellsList()) {
String columnName = getLayoutText(cell.getLayout().getTextAnchor(), text);
headerRowText.append(String.format("%s ", removeNewlines(columnName)));
headerRowText.append(String.format("%s | ", removeNewlines(columnName)));
}
headerRowText.setLength(headerRowText.length() - 3);
//System.out.printf(" Collumns: %s\n", headerRowText.toString());
System.out.printf("Columns: %s\n", headerRowText.toString());
tableText += headerRowText.toString();
for (Document.Page.Table.TableRow bodyRow : table.getBodyRowsList()) {
for (Document.Page.Table.TableCell cell : bodyRow.getCellsList()) {
String cellText = getLayoutText(cell.getLayout().getTextAnchor(), text);
System.out.printf("Table cell text: '%s'\n", removeNewlines(cellText));

StringBuilder firstRowText = new StringBuilder();
for (Document.Page.Table.TableCell cell : firstBodyRow.getCellsList()) {
String cellText = getLayoutText(cell.getLayout().getTextAnchor(), text);
firstRowText.append(String.format("%s ", removeNewlines(cellText)));
//tableText += String.format(" '%s' |", removeNewlines(cellText));
tableText += (removeNewlines(cellText)+" | ");
}
}
firstRowText.setLength(firstRowText.length() - 3);
//System.out.printf(" First row data: %s\n", firstRowText.toString());
tableText += firstRowText;

return tableText;
}

private static byte[] readImageDataFromUrl(String imageUrl) throws IOException {
URL url = new URL(imageUrl);

try (InputStream inputStream = url.openStream()) {
// Dynamically adjust the buffer size based on the available data
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] tempBuffer = new byte[8192];
int bytesRead;
while ((bytesRead = inputStream.read(tempBuffer)) != -1) {
outputStream.write(tempBuffer, 0, bytesRead);
}
return outputStream.toByteArray();
}
}
// Extract shards from the text field
private static String getLayoutText(Document.TextAnchor textAnchor, String text) {
if (textAnchor.getTextSegmentsList().size() > 0) {
/*if (textAnchor.getTextSegmentsList().size() > 0) {
int startIdx = (int) textAnchor.getTextSegments(0).getStartIndex();
int endIdx = (int) textAnchor.getTextSegments(0).getEndIndex();
return text.substring(startIdx, endIdx);
}
return "[NO TEXT]";
return "[NO TEXT]";*/

StringBuilder result = new StringBuilder();

for (Document.TextAnchor.TextSegment textSegment : textAnchor.getTextSegmentsList()) {
int startIdx = (int) textSegment.getStartIndex();
int endIdx = (int) textSegment.getEndIndex();
String segmentText = text.substring(startIdx, endIdx);
result.append(segmentText);
}

return result.toString();
}

private static String removeNewlines(String s) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.gdsc.hearo.domain.item.service;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.gdsc.hearo.domain.item.dto.ItemDto;
import com.gdsc.hearo.domain.item.dto.WishListResponseDto;
import com.gdsc.hearo.domain.item.dto.WishResponseDto;
Expand Down
Loading