Skip to content

Instantly share code, notes, and snippets.

@obafgkm44
Last active January 12, 2022 10:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obafgkm44/7c7e1c66495e7f9c12bca9ec5e20990c to your computer and use it in GitHub Desktop.
Save obafgkm44/7c7e1c66495e7f9c12bca9ec5e20990c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "71ebb275",
"metadata": {},
"outputs": [],
"source": [
"import PySimpleGUI as sg\n",
"import sys\n",
"import unicodedata\n",
"import random\n",
"\n",
"# 画面のレイアウト指定\n",
"layout = [\n",
" [sg.Text('テキストファイルを選択してください')],\n",
" [sg.Text(\"ファイル\"),sg.InputText(), sg.FileBrowse(key=\"file1\",file_types=((\"Text Files\", \"*.txt\"),))], # filetype の指定\n",
" [sg.Button(\"生成\",pad=(200,50))],\n",
" [sg.Text('---\\n---',key='info',size=(50,2),pad=(20,20))]\n",
"]\n",
"\n",
"sg.theme('LightGreen1')\n",
"\n",
"# ウィンドウの作成\n",
"window = sg.Window('単語生成', layout)\n",
"\n",
"# text処理:削除する部分\n",
"replace_words = [\"\t\t\t\t\t\t\",\"\t\t\t\t\t\",\"\t\t\t\t\",\"\t\t\t\",\"\t\t\"]\n",
"\n",
"while True:\n",
" event,val = window.read()\n",
" \n",
" if event in ('Exit','Quit',None): break\n",
" \n",
" # ファイルの中身から単語を取り出す\n",
" if event == '生成':\n",
" try:\n",
" with open(val['file1'],mode='r',encoding='shift_jis') as f:\n",
" word = f.read()\n",
" except FileNotFoundError: \n",
" val = sg.popup_scrolled('やり直してください',title='エラー')\n",
" break\n",
" except Exception:\n",
" with open(val['file1'],mode='r',encoding='utf-8') as f:\n",
" word = f.read()\n",
" \n",
" # テキストのインデント等整形\n",
" word = unicodedata.normalize('NFKC',word)\n",
" word =''.join(word) \n",
" for i in replace_words:\n",
" word = word.replace(i,'').strip()\n",
" word = word.replace('\t','\\n').strip()\n",
" word = word.replace('、','\\n')\n",
" word = word.split('\\n')\n",
" word = [word for word in word if word != '']\n",
" \n",
" # 単語選ぶ\n",
" words = []\n",
" for i in word:\n",
" words.append(i)\n",
" ld = random.choice(words)\n",
" ld2 = random.choice(words)\n",
" \n",
" # 同じ単語が選ばれてしまった時は選び直す\n",
" while ld == ld2:\n",
" ld2 = random.choice(words)\n",
" break\n",
" \n",
" s = str(ld) +'\\n'+str(ld2)\n",
" window['info'].update(s)\n",
"\n",
"# ウィンドウを閉じる\n",
"window.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b84bf372",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment