-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[#125] ๊ฐ์ ์ด ๋ถ์ API ์ถ๊ฐ
- Loading branch information
Showing
7 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/com/hobak/happinessql/domain/record/application/SentimentAnalyzeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.hobak.happinessql.domain.record.application; | ||
|
||
import com.google.gson.JsonObject; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.*; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
import org.springframework.web.client.HttpServerErrorException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
@Service | ||
public class SentimentAnalyzeService { | ||
@Value("${sentiment.clientId}") | ||
private String clientId; | ||
|
||
@Value("${sentiment.clientSecret}") | ||
private String clientSecret; | ||
|
||
public Map<String, List<String>> getAnalyzeResult(String content) { | ||
String url = "https://naveropenapi.apigw.ntruss.com/sentiment-analysis/v1/analyze"; | ||
|
||
try { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
headers.add("X-NCP-APIGW-API-KEY-ID", clientId); | ||
headers.add("X-NCP-APIGW-API-KEY", clientSecret); | ||
|
||
JsonObject jsonObject = new JsonObject(); | ||
jsonObject.addProperty("content", content); | ||
|
||
HttpEntity<String> entity = new HttpEntity<>(jsonObject.toString(), headers); | ||
|
||
ResponseEntity<Map<String, Object>> response = restTemplate.exchange( | ||
url, | ||
HttpMethod.POST, | ||
entity, | ||
new ParameterizedTypeReference<Map<String, Object>>() {} | ||
); | ||
|
||
Map<String, Object> responseBody = response.getBody(); | ||
if (responseBody != null && responseBody.containsKey("sentences")) { | ||
List<Map<String, Object>> sentences = (List<Map<String, Object>>) responseBody.get("sentences"); | ||
List<Map<String, String>> emotionText = sentences.stream() | ||
.map(sentence -> Map.of( | ||
"content", (String) sentence.get("content"), | ||
"sentiment", (String) sentence.get("sentiment") | ||
)) | ||
.collect(Collectors.toList()); | ||
|
||
return separateSentiment(emotionText); | ||
} else { | ||
log.warn("Response does not contain 'sentences' key"); | ||
return Map.of("positive", List.of(), "negative", List.of()); | ||
} | ||
} catch (HttpClientErrorException | HttpServerErrorException e) { | ||
log.error("HTTP error: " + e.toString()); | ||
return Map.of("positive", List.of(), "negative", List.of()); | ||
} catch (Exception e) { | ||
log.error("Unexpected error: " + e.toString()); | ||
return Map.of("positive", List.of(), "negative", List.of()); | ||
} | ||
} | ||
|
||
private Map<String, List<String>> separateSentiment(List<Map<String, String>> emotionText) { | ||
Map<String, List<String>> groupedTexts = emotionText.stream() | ||
.collect(Collectors.groupingBy( | ||
map -> map.get("sentiment"), | ||
Collectors.mapping(map -> map.get("content"), Collectors.toList()) | ||
)); | ||
|
||
List<String> positive = groupedTexts.getOrDefault("positive", List.of()); | ||
List<String> negative = groupedTexts.getOrDefault("negative", List.of()); | ||
|
||
return Map.of("positive", positive, "negative", negative); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/hobak/happinessql/domain/record/domain/Analysis.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.hobak.happinessql.domain.record.domain; | ||
|
||
import com.hobak.happinessql.global.infra.database.BaseTimeEntity; | ||
import jakarta.persistence.*; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Getter | ||
@Entity | ||
@Table(name = "analysis") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Analysis extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "analysis_id") | ||
private Long analysisId; | ||
@OneToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "record_id") | ||
private Record record; | ||
|
||
@ElementCollection | ||
@CollectionTable(name = "positive_sentiments", joinColumns = @JoinColumn(name = "analysis_id")) | ||
@Column(name = "content") | ||
private List<String> positiveSentiments = new ArrayList<>(); | ||
|
||
@ElementCollection | ||
@CollectionTable(name = "negative_sentiments", joinColumns = @JoinColumn(name = "analysis_id")) | ||
@Column(name = "content") | ||
private List<String> negativeSentiments = new ArrayList<>(); | ||
|
||
@Builder | ||
public Analysis(Record record, List<String> positiveSentiments, List<String> negativeSentiments) { | ||
this.record = record; | ||
this.positiveSentiments = positiveSentiments; | ||
this.negativeSentiments = negativeSentiments; | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/hobak/happinessql/domain/record/repository/AnalysisRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.hobak.happinessql.domain.record.repository; | ||
|
||
import com.hobak.happinessql.domain.record.domain.Analysis; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface AnalysisRepository extends JpaRepository<Analysis, Long> { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters