Spaces:
Sleeping
Sleeping
Update keywords_processor.py
Browse files- keywords_processor.py +31 -2
keywords_processor.py
CHANGED
|
@@ -1,10 +1,39 @@
|
|
|
|
|
| 1 |
import re
|
| 2 |
from sklearn.feature_extraction.text import CountVectorizer
|
|
|
|
| 3 |
|
| 4 |
def process_keywords(text):
|
| 5 |
-
"""テキストからN-gramを生成してリストとして返す"""
|
| 6 |
-
text = re.sub(r"[,\
|
| 7 |
vectorizer = CountVectorizer(ngram_range=(1, 3))
|
| 8 |
X = vectorizer.fit_transform([text])
|
| 9 |
features = vectorizer.get_feature_names_out()
|
| 10 |
return features
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import re
|
| 3 |
from sklearn.feature_extraction.text import CountVectorizer
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
def process_keywords(text):
|
| 7 |
+
""" テキストからN-gramを生成してリストとして返す """
|
| 8 |
+
text = re.sub(r"[,\n]+", " ", text) # 文字列を正規化して、カンマと改行を空白に変換
|
| 9 |
vectorizer = CountVectorizer(ngram_range=(1, 3))
|
| 10 |
X = vectorizer.fit_transform([text])
|
| 11 |
features = vectorizer.get_feature_names_out()
|
| 12 |
return features
|
| 13 |
+
|
| 14 |
+
def save_keywords(keywords, filename="output1.txt"):
|
| 15 |
+
""" キーワードをファイルに保存 """
|
| 16 |
+
with open(filename, 'w', encoding='utf-8') as file:
|
| 17 |
+
for keyword in keywords:
|
| 18 |
+
file.write(keyword + "\n")
|
| 19 |
+
return f"Keywords saved to {filename}"
|
| 20 |
+
|
| 21 |
+
def process_and_save_keywords(text):
|
| 22 |
+
keywords = process_keywords(text)
|
| 23 |
+
save_result = save_keywords(keywords)
|
| 24 |
+
return ", ".join(keywords), save_result
|
| 25 |
+
|
| 26 |
+
with gr.Blocks() as demo:
|
| 27 |
+
gr.Markdown("### N-gram Generator and Saver")
|
| 28 |
+
text_input = gr.Textbox(label="Enter text")
|
| 29 |
+
output_keywords = gr.Textbox(label="Generated N-grams", readonly=True)
|
| 30 |
+
output_message = gr.Textbox(label="Output Message", readonly=True)
|
| 31 |
+
submit_button = gr.Button("Generate and Save N-grams")
|
| 32 |
+
|
| 33 |
+
submit_button.click(
|
| 34 |
+
fn=process_and_save_keywords,
|
| 35 |
+
inputs=text_input,
|
| 36 |
+
outputs=[output_keywords, output_message]
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
demo.launch()
|