Skip to content

Commit 6339c8d

Browse files
authored
Merge pull request #38 from GoogleEyes-ewha/fix/#37
[Fix] documentAi 응답값 수정
2 parents 805e10d + ec61460 commit 6339c8d

File tree

3 files changed

+45
-57
lines changed

3 files changed

+45
-57
lines changed

src/main/java/com/gdsc/hearo/domain/item/controller/WishController.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.gdsc.hearo.domain.item.dto.WishRequestDto;
55
import com.gdsc.hearo.domain.item.dto.WishResponseDto;
66
import com.gdsc.hearo.domain.item.service.WishService;
7-
import com.gdsc.hearo.global.common.BaseException;
87
import com.gdsc.hearo.global.common.BaseResponse;
98
import com.gdsc.hearo.global.common.BaseResponseStatus;
109
import com.gdsc.hearo.global.security.CustomUserDetails;
@@ -35,9 +34,6 @@ public ResponseEntity<BaseResponse<WishResponseDto>> addToWishList(@Authenticati
3534
Long userId = userDetails.getMember().getMemberId();
3635
WishResponseDto result = wishService.addToWishList(userId, wishRequestDto.getItemId());
3736

38-
39-
40-
4137
response = new BaseResponse<>(BaseResponseStatus.SUCCESS, result);
4238
return ResponseEntity.ok(response);
4339

src/main/java/com/gdsc/hearo/domain/item/service/DocumentAiService.java

Lines changed: 45 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -77,92 +77,85 @@ public String execute(String filePath)
7777
// Get all of the document text as one big string
7878
String text = documentResponse.getText();
7979

80-
// Read the text recognition output from the processor
81-
//System.out.println("The document contains the following paragraphs:");
82-
Document.Page firstPage = documentResponse.getPages(0);
83-
List<Document.Page.Paragraph> paragraphs = firstPage.getParagraphsList();
84-
8580
// Read the text recognition output from the processor
8681
List<Document.Page> pages = documentResponse.getPagesList();
87-
//System.out.printf("There are %s page(s) in this document.\n", pages.size());
82+
System.out.printf("There are %s page(s) in this document.\n", pages.size());
8883

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

9187
List<Document.Page.Table> tables = page.getTablesList();
92-
93-
//영양성분표 없는 상황 추가해야함
94-
88+
System.out.printf("Found %d table(s):\n", tables.size());
9589
for (Document.Page.Table table : tables) {
96-
nutritionText += printTableInfo(table, text);
90+
nutritionText += extractTableContents(table, documentResponse.getText());
9791
}
9892

99-
List<Document.Page.FormField> formFields = page.getFormFieldsList();
100-
for (Document.Page.FormField formField : formFields) {
101-
String fieldName = getLayoutText(formField.getFieldName().getTextAnchor(), text);
102-
String fieldValue = getLayoutText(formField.getFieldValue().getTextAnchor(), text);
103-
104-
System.out.printf(
105-
" '%s': '%s'\n", removeNewlines(fieldName), removeNewlines(fieldValue));
106-
nutritionText += String.format(" '%s': '%s'", removeNewlines(fieldName), removeNewlines(fieldValue));
107-
}
93+
// ... (Other code for additional processing, if needed)
10894
}
109-
}
110-
return nutritionText;
111-
}
11295

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

116-
try (InputStream inputStream = url.openStream()) {
117-
// Dynamically adjust the buffer size based on the available data
118-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
119-
byte[] tempBuffer = new byte[8192];
120-
int bytesRead;
121-
while ((bytesRead = inputStream.read(tempBuffer)) != -1) {
122-
outputStream.write(tempBuffer, 0, bytesRead);
123-
}
124-
return outputStream.toByteArray();
12597
}
98+
return nutritionText;
12699
}
127100

128-
private static String printTableInfo(Document.Page.Table table, String text) {
129-
130-
String tableText = "";
131-
Document.Page.Table.TableRow firstBodyRow = table.getBodyRows(0);
132-
int columnCount = firstBodyRow.getCellsCount();
133-
/*System.out.printf(
134-
" Table with %d columns and %d rows:\n", columnCount, table.getBodyRowsCount());*/
101+
private static String extractTableContents(Document.Page.Table table, String text) {
135102

103+
String tableText="";
136104
Document.Page.Table.TableRow headerRow = table.getHeaderRows(0);
105+
// Extract and print header
137106
StringBuilder headerRowText = new StringBuilder();
138107
for (Document.Page.Table.TableCell cell : headerRow.getCellsList()) {
139108
String columnName = getLayoutText(cell.getLayout().getTextAnchor(), text);
140-
headerRowText.append(String.format("%s ", removeNewlines(columnName)));
109+
headerRowText.append(String.format("%s | ", removeNewlines(columnName)));
141110
}
142111
headerRowText.setLength(headerRowText.length() - 3);
143-
//System.out.printf(" Collumns: %s\n", headerRowText.toString());
112+
System.out.printf("Columns: %s\n", headerRowText.toString());
144113
tableText += headerRowText.toString();
114+
for (Document.Page.Table.TableRow bodyRow : table.getBodyRowsList()) {
115+
for (Document.Page.Table.TableCell cell : bodyRow.getCellsList()) {
116+
String cellText = getLayoutText(cell.getLayout().getTextAnchor(), text);
117+
System.out.printf("Table cell text: '%s'\n", removeNewlines(cellText));
145118

146-
StringBuilder firstRowText = new StringBuilder();
147-
for (Document.Page.Table.TableCell cell : firstBodyRow.getCellsList()) {
148-
String cellText = getLayoutText(cell.getLayout().getTextAnchor(), text);
149-
firstRowText.append(String.format("%s ", removeNewlines(cellText)));
119+
//tableText += String.format(" '%s' |", removeNewlines(cellText));
120+
tableText += (removeNewlines(cellText)+" | ");
121+
}
150122
}
151-
firstRowText.setLength(firstRowText.length() - 3);
152-
//System.out.printf(" First row data: %s\n", firstRowText.toString());
153-
tableText += firstRowText;
154-
155123
return tableText;
156124
}
157125

126+
private static byte[] readImageDataFromUrl(String imageUrl) throws IOException {
127+
URL url = new URL(imageUrl);
128+
129+
try (InputStream inputStream = url.openStream()) {
130+
// Dynamically adjust the buffer size based on the available data
131+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
132+
byte[] tempBuffer = new byte[8192];
133+
int bytesRead;
134+
while ((bytesRead = inputStream.read(tempBuffer)) != -1) {
135+
outputStream.write(tempBuffer, 0, bytesRead);
136+
}
137+
return outputStream.toByteArray();
138+
}
139+
}
158140
// Extract shards from the text field
159141
private static String getLayoutText(Document.TextAnchor textAnchor, String text) {
160-
if (textAnchor.getTextSegmentsList().size() > 0) {
142+
/*if (textAnchor.getTextSegmentsList().size() > 0) {
161143
int startIdx = (int) textAnchor.getTextSegments(0).getStartIndex();
162144
int endIdx = (int) textAnchor.getTextSegments(0).getEndIndex();
163145
return text.substring(startIdx, endIdx);
164146
}
165-
return "[NO TEXT]";
147+
return "[NO TEXT]";*/
148+
149+
StringBuilder result = new StringBuilder();
150+
151+
for (Document.TextAnchor.TextSegment textSegment : textAnchor.getTextSegmentsList()) {
152+
int startIdx = (int) textSegment.getStartIndex();
153+
int endIdx = (int) textSegment.getEndIndex();
154+
String segmentText = text.substring(startIdx, endIdx);
155+
result.append(segmentText);
156+
}
157+
158+
return result.toString();
166159
}
167160

168161
private static String removeNewlines(String s) {

src/main/java/com/gdsc/hearo/domain/item/service/WishService.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.gdsc.hearo.domain.item.service;
22

3-
import com.fasterxml.jackson.databind.ser.Serializers;
43
import com.gdsc.hearo.domain.item.dto.ItemDto;
54
import com.gdsc.hearo.domain.item.dto.WishListResponseDto;
65
import com.gdsc.hearo.domain.item.dto.WishResponseDto;

0 commit comments

Comments
 (0)