Download app.py from Kims12/mecab: direct link, hf CLI and curl.
- Browser
- Download file 2.87 kB
-
https://huggingface.co/spaces/Kims12/mecab/resolve/main/app.py
- Command line
-
hf download hf://spaces/Kims12/mecab/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Kims12/mecab/resolve/main/app.py
2.87 kB
| import re | |
| import logging | |
| import tempfile | |
| import pandas as pd | |
| import gradio as gr | |
| import mecab # pythonโmecabโko ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ | |
| # ๋๋ฒ๊น ์ ์ํ ๋ก๊น ์ค์ | |
| logging.basicConfig(level=logging.DEBUG) | |
| logger = logging.getLogger(__name__) | |
| def analyze_text(text: str): | |
| logger.debug("์๋ณธ ํ ์คํธ: %s", text) | |
| # 1. ํ๊ตญ์ด๋ง ๋จ๊ธฐ๊ธฐ (๊ณต๋ฐฑ, ์์ด, ๊ธฐํธ ๋ฑ ์ ๊ฑฐ) | |
| filtered_text = re.sub(r'[^๊ฐ-ํฃ]', '', text) | |
| logger.debug("ํํฐ๋ง๋ ํ ์คํธ (ํ๊ตญ์ด๋ง, ๊ณต๋ฐฑ ์ ๊ฑฐ): %s", filtered_text) | |
| if not filtered_text: | |
| logger.debug("์ ํจํ ํ๊ตญ์ด ํ ์คํธ๊ฐ ์์.") | |
| # ๋น DataFrame๊ณผ ๋น ๋ฌธ์์ด ๋ฐํ (Excel ํ์ผ ๊ฒฝ๋ก) | |
| return pd.DataFrame(columns=["๋จ์ด", "๋น๋์"]), "" | |
| # 2. Mecab์ ์ด์ฉํ ํํ์ ๋ถ์ (๋ช ์ฌ์ ๋ณตํฉ๋ช ์ฌ๋ง ์ถ์ถ) | |
| mecab_instance = mecab.MeCab() # ์์ : ์ง์ ์ธ์คํด์ค ์์ฑ | |
| tokens = mecab_instance.pos(filtered_text) | |
| logger.debug("ํํ์ ๋ถ์ ๊ฒฐ๊ณผ: %s", tokens) | |
| freq = {} | |
| for word, pos in tokens: | |
| # ๋น ๋ฌธ์์ด ์ฌ๋ถ ๋ฐ ์ ํจ์ฑ ๊ฒ์ฌ | |
| if word and word.strip(): | |
| # Mecab์์ ๋ช ์ฌ์ ๊ฒฝ์ฐ ์ผ๋ฐ์ ์ผ๋ก 'NN'์ผ๋ก ์์ (์: NNG, NNP ๋ฑ) | |
| if pos.startswith("NN"): | |
| freq[word] = freq.get(word, 0) + 1 | |
| logger.debug("๋จ์ด: %s, ํ์ฌ: %s, ํ์ฌ ๋น๋: %d", word, pos, freq[word]) | |
| # 3. ๋น๋์๋ฅผ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ | |
| sorted_freq = sorted(freq.items(), key=lambda x: x[1], reverse=True) | |
| logger.debug("๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ๋ ๋จ์ด ๋น๋: %s", sorted_freq) | |
| # 4. ๊ฒฐ๊ณผ DataFrame ์์ฑ (ํ๋ก ์ถ๋ ฅํ๊ธฐ ์ํจ) | |
| df = pd.DataFrame(sorted_freq, columns=["๋จ์ด", "๋น๋์"]) | |
| logger.debug("๊ฒฐ๊ณผ DataFrame ์์ฑ๋จ, shape: %s", df.shape) | |
| # 5. Excel ํ์ผ ์์ฑ (์์ ํ์ผ์ ์ ์ฅํ์ฌ ๋ค์ด๋ก๋ ์ ๊ณต) | |
| temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") | |
| df.to_excel(temp_file.name, index=False, engine='openpyxl') | |
| temp_file.close() | |
| logger.debug("Excel ํ์ผ ์์ฑ๋จ: %s", temp_file.name) | |
| return df, temp_file.name | |
| # Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# ํํ์ ๋ถ์ ์คํ์ด์ค") | |
| with gr.Row(): | |
| text_input = gr.Textbox(label="ํ ์คํธ ์ ๋ ฅ", lines=5, placeholder="๋ถ์ํ ํ ์คํธ๋ฅผ ์ ๋ ฅํ์ธ์.") | |
| with gr.Row(): | |
| analyze_button = gr.Button("๋ถ์ ์คํ") | |
| with gr.Row(): | |
| output_table = gr.Dataframe(label="๋ถ์ ๊ฒฐ๊ณผ (๋จ์ด ๋ฐ ๋น๋์)") | |
| with gr.Row(): | |
| output_file = gr.File(label="Excel ๋ค์ด๋ก๋") | |
| analyze_button.click(fn=analyze_text, inputs=text_input, outputs=[output_table, output_file]) | |
| if __name__ == "__main__": | |
| demo.launch() | |