๊ฐ•๋ฏผ๊ท  commited on
Commit
aa1a383
ยท
1 Parent(s): 96a4d11

Add stopword filtering to recipe ingredients

Browse files
Files changed (1) hide show
  1. src/api.py +23 -3
src/api.py CHANGED
@@ -48,6 +48,26 @@ def startup_event():
48
  # ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์Šค๋ ˆ๋“œ์—์„œ ๋ชจ๋ธ ๋กœ๋”ฉ ์‹œ์ž‘ (์•ฑ ์‹œ์ž‘์„ ๋ง‰์ง€ ์•Š์Œ)
49
  threading.Thread(target=logic.load_resources).start()
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  # --- Endpoints ---
52
 
53
  @app.get("/")
@@ -70,7 +90,7 @@ def list_recipes(limit: int = 50, offset: int = 0):
70
  output.append({
71
  "id": int(row['๋ ˆ์‹œํ”ผ์ผ๋ จ๋ฒˆํ˜ธ']),
72
  "name": row['์š”๋ฆฌ๋ช…'],
73
- "ingredients": row['์žฌ๋ฃŒํ† ํฐ'],
74
  "method": row['์š”๋ฆฌ๋ฐฉ๋ฒ•๋ณ„๋ช…'],
75
  "category": row['์š”๋ฆฌ์ข…๋ฅ˜๋ณ„๋ช…_์„ธ๋ถ„ํ™”']
76
  })
@@ -93,7 +113,7 @@ def search_recipes(q: str):
93
  output.append({
94
  "id": int(row['๋ ˆ์‹œํ”ผ์ผ๋ จ๋ฒˆํ˜ธ']),
95
  "name": row['์š”๋ฆฌ๋ช…'],
96
- "ingredients": row['์žฌ๋ฃŒํ† ํฐ']
97
  })
98
  return output
99
  except Exception as e:
@@ -115,7 +135,7 @@ def get_recipe_detail(recipe_id: int):
115
  "name": row['์š”๋ฆฌ๋ช…'],
116
  "method": row['์š”๋ฆฌ๋ฐฉ๋ฒ•๋ณ„๋ช…'],
117
  "category": row['์š”๋ฆฌ์ข…๋ฅ˜๋ณ„๋ช…_์„ธ๋ถ„ํ™”'],
118
- "ingredients": row['์žฌ๋ฃŒํ† ํฐ']
119
  }
120
  except Exception as e:
121
  raise HTTPException(status_code=500, detail=str(e))
 
48
  # ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์Šค๋ ˆ๋“œ์—์„œ ๋ชจ๋ธ ๋กœ๋”ฉ ์‹œ์ž‘ (์•ฑ ์‹œ์ž‘์„ ๋ง‰์ง€ ์•Š์Œ)
49
  threading.Thread(target=logic.load_resources).start()
50
 
51
+ # --- ๋ถˆ์šฉ์–ด ์บ์‹œ ๋ฐ ํ•„ํ„ฐ๋ง ---
52
+ _stopwords_cache = None
53
+
54
+ def get_stopwords():
55
+ """๋ถˆ์šฉ์–ด ๋ชฉ๋ก์„ ์บ์‹œํ•˜์—ฌ ๋ฐ˜ํ™˜"""
56
+ global _stopwords_cache
57
+ if _stopwords_cache is None:
58
+ try:
59
+ _stopwords_cache = set(logic.load_global_stopwords())
60
+ except:
61
+ _stopwords_cache = set()
62
+ return _stopwords_cache
63
+
64
+ def filter_ingredients(ingredients_list):
65
+ """์žฌ๋ฃŒ ๋ชฉ๋ก์—์„œ ๋ถˆ์šฉ์–ด ์ œ๊ฑฐ"""
66
+ stopwords = get_stopwords()
67
+ if not stopwords:
68
+ return ingredients_list
69
+ return [ing for ing in ingredients_list if ing not in stopwords]
70
+
71
  # --- Endpoints ---
72
 
73
  @app.get("/")
 
90
  output.append({
91
  "id": int(row['๋ ˆ์‹œํ”ผ์ผ๋ จ๋ฒˆํ˜ธ']),
92
  "name": row['์š”๋ฆฌ๋ช…'],
93
+ "ingredients": filter_ingredients(row['์žฌ๋ฃŒํ† ํฐ']),
94
  "method": row['์š”๋ฆฌ๋ฐฉ๋ฒ•๋ณ„๋ช…'],
95
  "category": row['์š”๋ฆฌ์ข…๋ฅ˜๋ณ„๋ช…_์„ธ๋ถ„ํ™”']
96
  })
 
113
  output.append({
114
  "id": int(row['๋ ˆ์‹œํ”ผ์ผ๋ จ๋ฒˆํ˜ธ']),
115
  "name": row['์š”๋ฆฌ๋ช…'],
116
+ "ingredients": filter_ingredients(row['์žฌ๋ฃŒํ† ํฐ'])
117
  })
118
  return output
119
  except Exception as e:
 
135
  "name": row['์š”๋ฆฌ๋ช…'],
136
  "method": row['์š”๋ฆฌ๋ฐฉ๋ฒ•๋ณ„๋ช…'],
137
  "category": row['์š”๋ฆฌ์ข…๋ฅ˜๋ณ„๋ช…_์„ธ๋ถ„ํ™”'],
138
+ "ingredients": filter_ingredients(row['์žฌ๋ฃŒํ† ํฐ'])
139
  }
140
  except Exception as e:
141
  raise HTTPException(status_code=500, detail=str(e))