Update app.py
Browse files22 unmodified lines2324252627282930313233345 unmodified lines404142434445464748495051525354555657585960613 unmodified lines656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118255 unmodified lines23823924024124224324424522 unmodified linesdef extract_titles(text, debug=False):"""ํ ์คํธ์์ ์ ํ ์ ๋ชฉ์ ์ถ์ถํ๋ ํจ์๊ฐ์ ์ฌํญ:1. ์ ํ ํค์๋ ๋ชฉ๋ก ํ์ฅ2. ์ ์ฌ๋ ๊ณ์ฐ ์ ๊ฑฐ (๊ฐ ์ํ์ ๊ณ ์ ํ๊ฒ ์ฒ๋ฆฌ)3. ๊ด๊ณ ํํฐ๋ง ๋ก์ง ์์ 4. ์ ๋ชฉ ์ถ์ถ ํจํด ๋ค์ํ"""# ์๋ณธ ์ค ์ ์ฅ (** ํจํด ๋ณด์กด)original_lines = text.split('\n')5 unmodified linestitles = []seen_titles = set()# ์ ํ ๊ด๋ จ ํค์๋ ํ์ฅ (์ํ, ๋ฐ์น๋ฐ์ค ๋ฑ ์ถ๊ฐ)key_product_words = ['๋์๋ฝ', '๋์๋ฝํต', '๋ณด์จ', '์คํ ', '๋ฐ์น', '๋ฐํ๋ฉ', '์ฃฝํต','๋ฌด์นจ', '๋ฐ์ฐฌ', '์ง๋ฏธ', '์ค์ง์ด', '๊นป์', '์ง๋ฏธ์ฑ', '๋ฌด์นจํ','์ง์ฅ์ธ', '์ ์น์', '์๋ฌ๋', '์ด์ ์', '๋ณด์จํต', '๋ฐฅํต', '๋จ','์ํ', '์๊ธฐ', '์ธํธ', '๋ฐ์น๋ฐ์ค', '๋ฐํ๋ ', '์ฐฌํฉ', '์ํ','๊ฐ์', '๊ฐ๋ฐฉ']if debug:print("=== ๋๋ฒ๊น ๋ชจ๋ ํ์ฑํ ===")# ํจํด 1: ** ์ฌ์ด์ ์๋ ํ ์คํธ๋ฅผ ๊ธฐ๋ณธ ์ ๋ชฉ ํ๋ณด๋ก ๊ฐ์ฃผfor i, original_line in enumerate(original_lines):# ๊ด๊ณ ๋ฌธ๊ตฌ ์์ฒด๋ ๊ฑด๋๋ฐ๋, ๊ด๊ณ ๊ฐ ํฌํจ๋ ์ ํ ์ ์ฒด๋ฅผ ๋ฌด์ํ์ง ์์if '๊ด๊ณ ' in lines[i] and len(lines[i].strip()) < 20: # ์งง์ ๊ด๊ณ ๋ฌธ๊ตฌ๋ง ๊ฑด๋๋ฐ๊ธฐif debug:print(f"๊ด๊ณ ๋ฌธ๊ตฌ ๋ฐ๊ฒฌ (๊ฑด๋๋): {lines[i]}")continue# ** ํจํด ์ฐพ๊ธฐ (์ ๋ชฉ์ผ ๊ฐ๋ฅ์ฑ์ด ๋์)3 unmodified linesfor match in matches:clean_match = preprocess_text(match)# ์ ๋ชฉ์ผ๋ก ์ ํฉํ์ง ํ์ธ (๊ธธ์ด ๊ฒ์ฌ ์ํ)if len(clean_match) > 5 and \not any(exclude in clean_match for exclude in ['์ํ๋ง ๋ณด๊ธฐ', '๊ตฌ๋งค์ ๋ณด']):# ์ ํ๋ช ์ผ๋ก ๊ฐ์ฃผํ ์ ์๋ ํค์๋๊ฐ ํฌํจ๋์ด ์๋์ง ํ์ธhas_product_keyword = any(keyword in clean_match.lower() for keyword in key_product_words)if has_product_keyword and clean_match not in seen_titles:if debug:print(f"ํจํด1 (** ํจํด) ์ ๋ชฉ ํ๋ณด: {clean_match}")titles.append(clean_match)seen_titles.add(clean_match)# ํจํด 2: ์ฐํ๊ธฐ ๋ค์ ์ค๋ ํ ์คํธ๋ฅผ ์ ๋ชฉ์ผ๋ก ๊ฐ์ฃผfor i in range(len(lines)):current_line = lines[i]# ์งง์ ๊ด๊ณ ๋ฌธ๊ตฌ๋ ๊ฑด๋๋ฐ๊ธฐif '๊ด๊ณ ' in current_line and len(current_line.strip()) < 20:continue# ์ฐํ๊ธฐ ํจํด ํ์ธ (์ฐํ๊ธฐ + ์ซ์)if '์ฐํ๊ธฐ' in current_line:# ํ์ฌ ์ค์์ ์ ๋ชฉ ์ฐพ๊ธฐ (์ฐํ๊ธฐ ๋ค์ ์์ ์ ์์)parts = current_line.split('์ฐํ๊ธฐ')if len(parts) > 1 and len(parts[1].strip()) > 5:# ํญ์ด๋ ์คํ์ด์ค๋ก ๊ตฌ๋ถ๋ ๋ถ๋ถ ์ฒ๋ฆฌpotential_title = parts[1].strip().split('\t')[0].strip()# ์ซ์๋ก๋ง ๋์ด์์ง ์์์ง ํ์ธif not re.match(r'^\d+$', potential_title) and len(potential_title) > 5:if any(keyword in potential_title.lower() for keyword in key_product_words) and potential_title not in seen_titles:if debug:print(f"ํจํด2-1 (์ฐํ๊ธฐ ๋ค ํ ์คํธ) ์ ๋ชฉ ํ๋ณด: {potential_title}")titles.append(potential_title)seen_titles.add(potential_title)# ์ฐํ๊ธฐ ๋ค์ ์ค ํ์ธ - ์ด ๋ถ๋ถ์ด ์ค์if i + 1 < len(lines):next_line = lines[i + 1]if len(next_line) > 5 and \not any(exclude in next_line for exclude in ['๋ฐฐ์ก๋น', '์ํ๋ง ๋ณด๊ธฐ', '์๋ฐฐ์ก๋น']):# ์ซ์๋ก๋ง ๋์ด์์ง ์์์ง ํ์ธif not re.match(r'^\d+$', next_line):# ์ ํ๋ช ์ผ๋ก ๊ฐ์ฃผํ ์ ์๋ ํค์๋๊ฐ ํฌํจ๋์ด ์๋์ง ํ์ธhas_product_keyword = any(keyword in next_line.lower() for keyword in key_product_words)if has_product_keyword and next_line not in seen_titles:if debug:print(f"ํจํด2-2 (์ฐํ๊ธฐ ๋ค์ ์ค) ์ ๋ชฉ ํ๋ณด: {next_line}")titles.append(next_line)seen_titles.add(next_line)# ํจํด 3: ๊ฐ๊ฒฉ ์ ๋ณด ๋ฐ๋ก ์ ์ค ํ์ธ (์ค์ํ ์ ๋ชฉ ์ถ์ถ ํจํด)if i + 1 < len(lines) and \(re.search(r'\d+,?\d*์', lines[i + 1]) or '์๋ฐฐ์ก๋น' in lines[i + 1]):if len(current_line) > 5 and \'์ฐํ๊ธฐ' not in current_line and \'์ํ๋ง ๋ณด๊ธฐ' not in current_line:# ์ ํ๋ช ์ผ๋ก ๊ฐ์ฃผํ ์ ์๋ ํค์๋๊ฐ ํฌํจ๋์ด ์๋์ง ํ์ธhas_product_keyword = any(keyword in current_line.lower() for keyword in key_product_words)if has_product_keyword and current_line not in seen_titles:if debug:print(f"ํจํด3 (๊ฐ๊ฒฉ ์ ๋ณด ์ ์ค) ์ ๋ชฉ ํ๋ณด: {current_line}")titles.append(current_line)seen_titles.add(current_line)# ํจํด 4: ๋ฐ๋ณต๋๋ ์ ํ๋ช ํจํด ์ฐพ๊ธฐ (์์๋๋ก ๋น์ทํ ํ ์คํธ๊ฐ ์๋ ๊ฒฝ์ฐ)for i in range(len(lines)):current_line = lines[i]# ์งง์ ๊ด๊ณ ๋ฌธ๊ตฌ๋ ๊ฑด๋๋ฐ๊ธฐif '๊ด๊ณ ' in current_line and len(current_line.strip()) < 20:continueif len(current_line) > 5 and \not any(exclude in current_line for exclude in ['์๋ฐฐ์ก๋น', '์ํ๋ง ๋ณด๊ธฐ', '๊ตฌ๋งค์ ๋ณด','์ํ/๊ฑด๊ฐ', 'ํฌ์ธํธ ์ต๋', '์ ๋ฆฝ์ฟ ํฐ','์ฌ์ง :', '๋ณ์ ', '๋ฆฌ๋ทฐ']):# ์ค๋ณต ํ ์คํธ ์ฐพ๊ธฐfound_duplicate = Falsefor j in range(max(0, i - 3), min(len(lines), i + 4)):if i != j and lines[i] == lines[j]:found_duplicate = Truebreak# ์ ํ ํค์๋๊ฐ ์๋์ง ํ์ธhas_product_keyword = any(keyword in current_line.lower() for keyword in key_product_words)# ์ค๋ณต์ด ์๊ฑฐ๋ ํค์๋๊ฐ ์์ผ๋ฉด ์ ๋ชฉ์ผ๋ก ๊ฐ์ฃผif (found_duplicate or has_product_keyword) and current_line not in seen_titles:# ์ซ์๋ก๋ง ๋ ํ ์คํธ๋ ์ ์ธif not re.match(r'^[\d\s]+$', current_line):if debug:print(f"ํจํด4 (๋ฐ๋ณต/ํค์๋ ํจํด) ์ ๋ชฉ ํ๋ณด: {current_line}")titles.append(current_line)seen_titles.add(current_line)# ์ค๋ณต ์ ๊ฑฐ ๋ฐ ์ ๋ชฉ ์ ์ (์ ์ฌ๋ ๊ณ์ฐ ์์ด ๋จ์ ์ค๋ณต๋ง ์ ๊ฑฐ)final_titles = []processed_titles = set()for title in titles:# ์ ๋ชฉ์ผ๋ก ์ ํฉํ์ง ์์ ํญ๋ชฉ ํํฐ๋งif len(title) > 5 and not re.match(r'^[0-9.]+$', title):# ๋จ์ ์ค๋ณต ๊ฒ์ฌ๋ง ์ํif title not in processed_titles:final_titles.append(title)processed_titles.add(title)55 unmodified lines### ์ ์ฉ๋๋ ๊ท์น:1. "*" ๋ฌธ์ ์ ๊ฑฐ2. ์ค๋ณต ๊ณต๋ฐฑ์ ๋จ์ผ ๊ณต๋ฐฑ์ผ๋ก ๋ณ๊ฒฝ3. ์ ํ ๊ด๋ จ ํค์๋๊ฐ ํฌํจ๋ ํ ์คํธ๋ฅผ ์ ๋ชฉ์ผ๋ก ์ฐ์ ๊ฐ์ฃผ4. ์ค๋ณต ์ ๋ชฉ์ ์ ๊ฑฐํ๊ณ ์์๋๋ก ๋ฒํธ ๋ถ์ฌ""")with gr.Row():22 unmodified lines232425262728293031323334353637385 unmodified lines444546474849505152535455565758596061626364656667686970717273743 unmodified lines7879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420555 unmodified lines26126226326426526626726826922 unmodified linesdef extract_titles(text, debug=False):"""ํ ์คํธ์์ ์ ํ ์ ๋ชฉ์ ์ถ์ถํ๋ ํจ์๊ท์น:1. "*"๋ฌธ์ ๊ฐ ๋์ค๋ฉด ์ ๊ฑฐ2. " "์ด๋ฉด " "๋ก ๋ณ๊ฒฝ3. ์คํ์ด์ค๊ฐ 1๊ฐ์ด์์ด๋ฉด ๋ฌด์กฑ๊ฑด 2๊ฐ๋ฉด 1๊ฐ๋ก ์์ 4. ์์๋๋ก 3์ค์ ๊ธฐ์ค์ผ๋ก ๋์ผํ ๋ด์ฉ์ด ์์ผ๋ฉด ์ ๋ชฉ์ผ๋ก ๊ฐ์ฃผ5. ์์๋๋ก 5์ค๊ธฐ์ค์ผ๋ก "๊ด๊ณ "๊ณ ๋ฌธ๊ตฌ๊ฐ ์๋ค๋ฉด ์ ๋ชฉ์์ ์ ์ธ6. ์ฒ์๋ถํฐ ์ ์ผ ์๋ถํฐ 1๋ก ์ ํ๊ณ ์์๋๋ก ์๋ฒ์ ์ ๋ฆฌ7. ๋์ผํ ์ซ์๊ฐ ๋์จ๋ค๊ณ ํด์ ๊ทธ ์ซ์๋ ์ ๋ชฉ์ด ์๋๋ค๊ฐ์ : ์ ํ ํค์๋ ์์กด๋๋ฅผ ๋ฎ์ถ๊ณ ์์น์ ํจํด ๊ธฐ๋ฐ ์ถ์ถ ๊ฐํ"""# ์๋ณธ ์ค ์ ์ฅ (** ํจํด ๋ณด์กด)original_lines = text.split('\n')5 unmodified linestitles = []seen_titles = set()# ์ ๋ชฉ์ผ๋ก ์ ํฉํ์ง ์์ ๋จ์ด๋ค (ํ๋งค์, ์๋น์ค, ๋ถ๊ฐ ์ ๋ณด ๋ฑ)exclude_words = ['์ํ๋ง ๋ณด๊ธฐ', '๊ตฌ๋งค์ ๋ณด', 'ํฌ์ธํธ ์ต๋', '๋ฐฐ์ก๋น', '์๋ฐฐ์ก๋น','์ํ/๊ฑด๊ฐ', '์ ๋ฆฝ์ฟ ํฐ', '์ฌ์ง :', '๋ณ์ ', '๋ฆฌ๋ทฐ', '์ผํ๋ชฐ๋ณ','ํ์', '์๋น์ค', '๋น ํ์', '๊ตฟ์๋น์ค', '๋ฌด๋ฃ๊ตํ๋ฐํ', '์ค๋์ถ๋ฐ','๋ค์ด๋ฒํ๋ฌ์ค', '๋ฉค๋ฒ์ญ', '์ ๊ณ ํ๊ธฐ', '๋ฑ๋ก์ผ', 'ํกํก']# ๊ด๊ณ ์์น ํ์ ๋ฐฐ์ด (5์ค ๋ฒ์ ๋ด ๊ด๊ณ ์์ผ๋ฉด true)has_ad_nearby = [False] * len(lines)# ๊ด๊ณ ์์น ๋ฏธ๋ฆฌ ํ์ ํ๊ธฐfor i in range(len(lines)):if '๊ด๊ณ ' in lines[i]:# ํด๋น ์ค์์ ์์๋๋ก 5์ค๊น์ง ๊ด๊ณ ๊ทผ์ฒ๋ก ํ์for j in range(max(0, i-5), min(len(lines), i+6)):has_ad_nearby[j] = Trueif debug:print(f"์ด {len(lines)}์ค ์ค {sum(has_ad_nearby)}์ค์ด ๊ด๊ณ ๊ทผ์ฒ๋ก ํ์๋จ")# ํจํด 1: ** ์ฌ์ด์ ์๋ ํ ์คํธ๋ฅผ ๊ธฐ๋ณธ ์ ๋ชฉ ํ๋ณด๋ก ๊ฐ์ฃผfor i, original_line in enumerate(original_lines):# ๊ด๊ณ ๊ทผ์ฒ๋ฉด ๊ฑด๋๋ฐ๊ธฐif has_ad_nearby[i]:continue# ** ํจํด ์ฐพ๊ธฐ (์ ๋ชฉ์ผ ๊ฐ๋ฅ์ฑ์ด ๋์)3 unmodified linesfor match in matches:clean_match = preprocess_text(match)# ์ ๋ชฉ์ผ๋ก ์ ํฉํ์ง ํ์ธif len(clean_match) > 10 and \not any(exclude in clean_match for exclude in exclude_words) and \not re.match(r'^[\d,.\s]+$', clean_match):if clean_match not in seen_titles:titles.append(clean_match)seen_titles.add(clean_match)# ํจํด 2: ๊ฐ๊ฒฉ ์ ๋ณด ์์ ์ค์ด ์ ๋ชฉ์ผ ๊ฐ๋ฅ์ฑ์ด ๋์for i in range(len(lines) - 1):# ๊ด๊ณ ๊ทผ์ฒ๋ฉด ๊ฑด๋๋ฐ๊ธฐif has_ad_nearby[i]:continuecurrent_line = lines[i]next_line = lines[i + 1]# ๊ฐ๊ฒฉ ํจํด ํ์ธ (์ซ์+์, ์๋ฐฐ์ก๋น)price_pattern = re.search(r'\d+,?\d*์', next_line) or '์๋ฐฐ์ก๋น' in next_lineif price_pattern and len(current_line) > 10:# ์ ๋ชฉ์ผ๋ก ์ ํฉํ์ง ์์ ๋ด์ฉ ๋ฐ ์ซ์๋ง์ผ๋ก ๊ตฌ์ฑ๋ ์ค ์ ์ธif not any(exclude in current_line for exclude in exclude_words) and \not re.match(r'^[\d,.\s]+$', current_line) and \'์ฐํ๊ธฐ' not in current_line:if current_line not in seen_titles:titles.append(current_line)seen_titles.add(current_line)# ํจํด 3: ์ฐํ๊ธฐ ๋ค์ ์ค ํ์ธfor i in range(len(lines) - 1):# ๊ด๊ณ ๊ทผ์ฒ๋ฉด ๊ฑด๋๋ฐ๊ธฐif has_ad_nearby[i]:continuecurrent_line = lines[i]# ์ฐํ๊ธฐ ํจํด ํ์ธif '์ฐํ๊ธฐ' in current_line and i + 1 < len(lines):next_line = lines[i + 1]# ์ ๋ชฉ์ผ๋ก ์ ํฉํ์ง ์์ ๋ด์ฉ ๋ฐ ์ซ์๋ง์ผ๋ก ๊ตฌ์ฑ๋ ์ค ์ ์ธif len(next_line) > 10 and \not any(exclude in next_line for exclude in exclude_words) and \not re.match(r'^[\d,.\s]+$', next_line):if next_line not in seen_titles:titles.append(next_line)seen_titles.add(next_line)# ํจํด 4: ๋ฐ๋ณต๋๋ ํ ์คํธ ์ฐพ๊ธฐ (์กฐ๊ฑด 4: ์์๋๋ก 3์ค ๊ธฐ์ค์ผ๋ก ๋์ผํ ๋ด์ฉ)for i in range(len(lines)):# ๊ด๊ณ ๊ทผ์ฒ๋ฉด ๊ฑด๋๋ฐ๊ธฐif has_ad_nearby[i]:continuecurrent_line = lines[i]# ์ ๋ชฉ์ผ๋ก ์ ํฉํ์ง ์์ ๋ด์ฉ ๋ฐ ์งง์ ์ค ์ ์ธif len(current_line) <= 10 or \any(exclude in current_line for exclude in exclude_words) or \re.match(r'^[\d,.\s]+$', current_line):continue# ์์๋๋ก 3์ค ๋ฒ์ ๋ด์ ๋์ผํ ํ ์คํธ๊ฐ ์๋์ง ํ์ธfor j in range(max(0, i - 3), min(len(lines), i + 4)):if i != j and not has_ad_nearby[j] and lines[i] == lines[j]:if current_line not in seen_titles:titles.append(current_line)seen_titles.add(current_line)break# ํจํด 5: ์์น ๊ธฐ๋ฐ ์ถ์ถ (๊ฐ ์ ํ ๋ธ๋ก์ ์ฃผ์ ์์น์ ์ ๋ชฉ์ด ์์ ๊ฐ๋ฅ์ฑ์ด ๋์)block_start_indices = []# ์ ํ ๋ธ๋ก์ ์์์ ์ฐพ๊ธฐ (์ฐํ๊ธฐ ์ค์ด ์๋ก์ด ์ ํ์ ์์์ ์ผ ๊ฐ๋ฅ์ฑ์ด ๋์)for i in range(len(lines)):if '์ฐํ๊ธฐ' in lines[i] and not has_ad_nearby[i]:block_start_indices.append(i)# ๊ฐ ๋ธ๋ก์ ์ฒซ ๋ฒ์งธ ์๋ฏธ ์๋ ์ค์ ์ ๋ชฉ์ผ๋ก ๊ฐ์ฃผfor start_idx in block_start_indices:# ๋ธ๋ก ์์ ํ 5์ค ์ด๋ด์์ ์๋ฏธ ์๋ ํ ์คํธ ์ฐพ๊ธฐfor i in range(start_idx + 1, min(start_idx + 6, len(lines))):if has_ad_nearby[i]:continuecurrent_line = lines[i]# ์ ๋ชฉ์ผ๋ก ์ ํฉํ์ง ํ์ธif len(current_line) > 10 and \not any(exclude in current_line for exclude in exclude_words) and \not re.match(r'^[\d,.\s]+$', current_line):if current_line not in seen_titles:titles.append(current_line)seen_titles.add(current_line)break# ์ค๋ณต ์ ๊ฑฐ ๋ฐ ์ ๋ชฉ ์ ์ final_titles = []processed_titles = set()for title in titles:# ์ ๋ชฉ์ผ๋ก ์ ํฉํ์ง ์์ ํญ๋ชฉ ํํฐ๋งif len(title) > 10 and not re.match(r'^[\d,.\s]+$', title):# ์ ์ธํ ๋จ์ด๊ฐ ํฌํจ๋ ๊ฒฝ์ฐ ๊ฑด๋๋ฐ๊ธฐif any(exclude in title for exclude in exclude_words):continueif title not in processed_titles:# "์ฐํ๊ธฐ" ๋ฌธ๊ตฌ๊ฐ ํฌํจ๋ ๊ฒฝ์ฐ ์ ๊ฑฐif '์ฐํ๊ธฐ' in title:parts = title.split('์ฐํ๊ธฐ')if len(parts) > 1:# ์ซ์ ๋ค์์ ์ค๋ ํญ์ด๋ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถclean_title = re.sub(r'์ฐํ๊ธฐ\d+[,\d]*\s*[\t ]+', '', title).strip()if len(clean_title) > 10:title = clean_titlefinal_titles.append(title)processed_titles.add(title)55 unmodified lines### ์ ์ฉ๋๋ ๊ท์น:1. "*" ๋ฌธ์ ์ ๊ฑฐ2. ์ค๋ณต ๊ณต๋ฐฑ์ ๋จ์ผ ๊ณต๋ฐฑ์ผ๋ก ๋ณ๊ฒฝ3. ์์๋๋ก 3์ค ๊ธฐ์ค์ผ๋ก ๋์ผํ ๋ด์ฉ์ด ์์ผ๋ฉด ์ ๋ชฉ์ผ๋ก ๊ฐ์ฃผ4. ์์๋๋ก 5์ค ๊ธฐ์ค์ผ๋ก "๊ด๊ณ " ๋ฌธ๊ตฌ๊ฐ ์๋ ์ ํ์ ์ ์ธ5. ์ค๋ณต ์ ๋ชฉ์ ์ ๊ฑฐํ๊ณ ์์๋๋ก ๋ฒํธ ๋ถ์ฌ""")with gr.Row():
app.py+128-104