Skip to content

Commit

Permalink
Merge pull request #352 from softeerbootcamp-2nd/dev
Browse files Browse the repository at this point in the history
Python 연동 버그 오류 해결 후 Merge
  • Loading branch information
tank3a authored Aug 20, 2023
2 parents 850c037 + c62552b commit 5db8dfa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.idea
.DS_Store
.env
20 changes: 13 additions & 7 deletions backend-recommend/recommend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import time
import os
from dotenv import load_dotenv
from flask import jsonify

load_dotenv(verbose=True)

Expand Down Expand Up @@ -44,20 +45,25 @@ def recByApriori(body):
df = pd.DataFrame(te_ary, columns=te.columns_)
frequent_itemsets = fpgrowth(df, min_support=0.05, use_colnames=True)

result_itemsets = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.05)
result_itemsets = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.3)
matching_itemsets = {}

consequent_results = set()

for idx, row in result_itemsets.iterrows():
confidence = row.confidence
antecedents = set(row.antecedents)
consequents = set(row.consequents)

if set(row.antecedents).issubset(input) and len(row.consequents) <= 2 and not set(row.antecedents).union(set(row.consequents)).issubset(input):
key = tuple(row.consequents)
if antecedents.issubset(input) and len(consequents) <= 2 and not antecedents.union(consequents).issubset(input):
key = tuple(consequents)
if key not in matching_itemsets or confidence > matching_itemsets[key]:
matching_itemsets[key] = confidence

sorted_items = sorted(matching_itemsets.items(), key=lambda x: x[1], reverse=True)
top_items = sorted_items[:4]
top_consequents = [consequent for consequent, _ in top_items]
return top_consequents

response = []
for item in top_items:
consequent = item[0]
response.append({"salesOptions": ",".join(consequent)})

return jsonify(response)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import autoever2.cartag.service.RecommendService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -14,7 +15,7 @@ public class RecommendController {
private final RecommendService recommendService;

@PostMapping("/list")
public String getRecommendedList() {
return recommendService.getList();
public ResponseEntity<String> getRecommendedList() {
return ResponseEntity.ok(recommendService.getList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
@Service
public class RecommendService {

@Value("${python.url")
@Value("${python.url}")
private String requestURL;

//TODO: 응답 존재 안할 시 예외처리
public String getList() {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(requestURL))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(getJsonFromEstimate())).build();

try {
Expand All @@ -43,9 +44,10 @@ public String getJsonFromEstimate() {

JSONArray jsonArray = new JSONArray();
JSONObject subOption = new JSONObject();
subOption.put("subOptionId", 69);
subOption.put("subOptionId", "69");
jsonArray.add(subOption);
subOption.put("subOptionId", 70);
subOption = new JSONObject();
subOption.put("subOptionId", "70");
jsonArray.add(subOption);

jsonObject.put("options", jsonArray);
Expand Down

0 comments on commit 5db8dfa

Please sign in to comment.