Skip to content

Commit 0f817e4

Browse files
committed
fix kookmin-sw#1 - fix type
fix id type 포스트인 경우는 long이기 떄문에 이를 변경
1 parent d5584fb commit 0f817e4

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

main.py

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class DataModel(BaseModel):
4444
user: UserCategory
4545
post: PostCategory
4646

47-
DATABASE_URL = "postgresql://cheesecrust:0810jack@mydatabase.c3kmc4wcyz81.ap-northeast-2.rds.amazonaws.com/maru"
48-
# DATABASE_URL = "postgresql://localhost:5432/maru"
47+
# DATABASE_URL = "postgresql://cheesecrust:0810jack@mydatabase.c3kmc4wcyz81.ap-northeast-2.rds.amazonaws.com/maru"
48+
DATABASE_URL = "postgresql://localhost:5432/maru"
4949
database = Database(DATABASE_URL)
5050

5151

@@ -90,7 +90,16 @@ def extract_features(data):
9090

9191
if options_value is not None:
9292
options_array = ast.literal_eval(options_value)
93-
93+
94+
result_array = []
95+
for value in options_array:
96+
if isinstance(value, list):
97+
result_array.append(str(value))
98+
# elif isinstance(value, int):
99+
# print("int value : ", value)
100+
# result_array.extend(str(value))
101+
else:
102+
result_array.append(value)
94103
# 추가할 키의 값들 추출
95104
smoking_value = parsed_data['smoking']
96105
mate_age_value = None
@@ -99,10 +108,11 @@ def extract_features(data):
99108
room_sharing_option_value = parsed_data['room_sharing_option']
100109

101110
# 추가할 값들을 배열에 추가
102-
options_array.extend([smoking_value, room_sharing_option_value])
111+
result_array.extend([smoking_value, room_sharing_option_value])
103112
if mate_age_value is not None:
104-
options_array.append(mate_age_value)
105-
return options_array
113+
result_array.append(mate_age_value)
114+
115+
return result_array
106116

107117
def generate_df_data(data):
108118
df = pd.DataFrame(data)
@@ -118,7 +128,6 @@ def generate_df_data(data):
118128
.reset_index(level=1, drop=True)
119129
.to_frame("features")
120130
)
121-
122131
dummies = (
123132
pd.get_dummies(features, prefix="", prefix_sep="").groupby(level=0).sum()
124133
)
@@ -127,7 +136,7 @@ def generate_df_data(data):
127136
dummies.drop("[]", axis=1, inplace=True)
128137
if "null" in dummies:
129138
dummies.drop("null", axis=1, inplace=True)
130-
139+
131140
df = pd.concat([df, dummies], axis=1).drop("features", axis=1)
132141

133142
return df
@@ -197,16 +206,16 @@ async def clustering(user_male_cards, user_female_cards, post_male_cards, post_f
197206
male_cards = [*user_male_cards, *post_male_cards]
198207

199208
if male_cards == []:
200-
male_cards = [{'id': 'default', 'features': None, 'gender': 'MALE', 'card_type': 'my', 'birth_year': '1999'},
201-
{'id': 'default', 'features': None, 'gender': 'MALE', 'card_type': 'mate', 'birth_year': '1999'}]
209+
male_cards = [{'id': 'male_default', 'features': None, 'gender': 'MALE', 'card_type': 'my', 'birth_year': '1999'},
210+
{'id': 'male_default', 'features': None, 'gender': 'MALE', 'card_type': 'mate', 'birth_year': '1999'}]
202211

203212
male_df = generate_df_data(male_cards)
204213

205214
female_cards = [*user_female_cards, *post_female_cards]
206215

207216
if female_cards == []:
208-
female_cards = [{'id': 'default', 'features': None, 'gender': 'MALE', 'card_type': 'my', 'birth_year': '1999'},
209-
{'id': 'default', 'features': None, 'gender': 'MALE', 'card_type': 'mate', 'birth_year': '1999'}]
217+
female_cards = [{'id': 'female_default', 'features': None, 'gender': 'MALE', 'card_type': 'my', 'birth_year': '1999'},
218+
{'id': 'female_default', 'features': None, 'gender': 'MALE', 'card_type': 'mate', 'birth_year': '1999'}]
210219

211220
female_df = generate_df_data(female_cards)
212221

@@ -262,8 +271,8 @@ async def clustering(user_male_cards, user_female_cards, post_male_cards, post_f
262271
for j, other_card in enumerate(cluster_item):
263272
if (
264273
i == j
265-
# or card_type == other_card["card_type"]
266-
# or user_id == other_card["id"]
274+
or card_type == other_card["card_type"]
275+
or user_id == other_card["id"]
267276
):
268277
continue
269278

@@ -298,8 +307,8 @@ async def clustering(user_male_cards, user_female_cards, post_male_cards, post_f
298307
for j, other_card in enumerate(cluster_item):
299308
if (
300309
i == j
301-
# or card_type == other_card["card_type"]
302-
# or user_id == other_card["id"]
310+
or card_type == other_card["card_type"]
311+
or user_id == other_card["id"]
303312
):
304313
continue
305314

@@ -323,15 +332,17 @@ async def clustering(user_male_cards, user_female_cards, post_male_cards, post_f
323332

324333
# print("male key : ", male_recommendation_result.keys())
325334
# print("male result : ", male_recommendation_result.values())
326-
327335
"""
328336
user_id <-> id, user_card_type, score, id_type
329337
"""
330338

331339
await database.execute("truncate table recommend")
340+
# print(male_recommendation_result)
341+
# print(female_recommendation_result)
342+
332343
for recommendation_result in (male_recommendation_result, female_recommendation_result):
333-
for user_id in recommendation_result:
334344

345+
for user_id in recommendation_result:
335346
query = """
336347
insert into recommend (user_id, card_type, recommendation_id, recommendation_card_type, score)
337348
values (:user_id, :card_type, :id, :recommendation_card_type, :score)
@@ -342,14 +353,14 @@ async def clustering(user_male_cards, user_female_cards, post_male_cards, post_f
342353

343354
for result in my_card_result:
344355
id = result["id"]
345-
score = result["score"]
356+
score = result["score"] * 100
346357
card_type = result["cardType"]
347358

348359
await database.execute(query, {"user_id": user_id, "card_type": 'my', "id": id, "score": score, "recommendation_card_type": card_type})
349360

350361
for result in mate_card_result:
351362
id = result["id"]
352-
score = result["score"]
363+
score = result["score"] * 100
353364
card_type = result["cardType"]
354365

355366
await database.execute(query, {"user_id": user_id, "card_type": 'mate', "id": id, "score": score, "recommendation_card_type": card_type})
@@ -359,17 +370,17 @@ async def clustering(user_male_cards, user_female_cards, post_male_cards, post_f
359370

360371
for result in post_my_card_result:
361372
id = result["id"]
362-
score = result["score"]
373+
score = result["score"] * 100
363374
card_type = result["cardType"]
364375

365-
await database.execute(query, {"user_id": user_id, "card_type": 'my', "id": id, "score": score, "recommendation_card_type": card_type})
376+
await database.execute(query, {"user_id": user_id, "card_type": 'my', "id": str(id), "score": score, "recommendation_card_type": card_type})
366377

367378
for result in post_mate_card_result:
368379
id = result["id"]
369-
score = result["score"]
380+
score = result["score"] * 100
370381
card_type = result["cardType"]
371382

372-
await database.execute(query, {"user_id": user_id, "card_type": 'mate', "id": id, "score": score, "recommendation_card_type": card_type})
383+
await database.execute(query, {"user_id": user_id, "card_type": 'mate', "id": str(id), "score": score, "recommendation_card_type": card_type})
373384

374385
# 여기에 insert
375386

@@ -396,8 +407,8 @@ async def fetch():
396407
await fetch_data()
397408
)
398409

399-
print(user_male_cards)
400-
print(generate_df_data(user_male_cards))
410+
# print(user_male_cards)
411+
print(generate_df_data(post_female_cards))
401412

402413
@app.get("/insert")
403414
async def insert():

0 commit comments

Comments
 (0)