ファイル保存時にディレクトリがなければ作成する(Python)
ファイル保存時にディレクトリがなければ作成する処理は以下のように書く。
def save_text(path, text): # ディレクトリが存在しなければ作成する path_dir = os.path.dirname(path) if not os.path.exists(path_dir): # makedirsは途中のディレクトがない場合作成します os.makedirs(path_dir) # ファイル保存(上書き) f = open(path,'w') f.write(text) f.close()
リンク
MacOSXにMecabインストール
fasttextを試すため、Mac OS X(Sierra)にMecabをインストールした。
pythonのバージョンは3.5.2
$ python --version Python 3.5.2 :: Anaconda 4.1.1 (x86_64)
手順
mecab-python3をインストール
pip install mecab-python3
mecab+ipadicをインストール
brew install mecab-ipadic
辞書(mecab-ipadic-NEologd)もインストールした。
多数のWeb上の言語資源から得た新語を追加することでカスタマイズした MeCab 用のシステム辞書
ネット上で流行した単語や慣用句やハッシュタグをエントリ化したデータ
実行
$ mecab -d /usr/local/lib/mecab/dic/mecab-ipadic-neologd ピコ太郎 ピコ太郎 名詞,固有名詞,人名,一般,*,*,ピコ太郎,ピコタロウ,ピコタロー
$ python Python 3.5.2 |Anaconda 4.1.1 (x86_64)| (default, Jul 2 2016, 17:52:12) [GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import MeCab >>> mecab = MeCab.Tagger() >>> print(mecab.parse("ピコ太郎")) ピコ太郎 名詞,固有名詞,人名,一般,*,*,ピコ太郎,ピコタロウ,ピコタロー EOS
Links
Visual Studio CodeでRubyデバッグを試してみました
Visual Studio CodeによるRubyのデバッグ | Developers.IOを参考にVisual Studio CodeでRubyのデバッグを試してみました。ウオッチ式、コールスタックが使えるので十分実用できそうです。
今まで、Rubyのデバッグ用にRubyMineを購入していたが、Visual Studio Codeで十分な気がしてきました。
Xcode8でコメント作成
Xcode8では、option + command + /で関数のコメントのひな形をを作成してくれます。便利。
リンク
UILabelのタップイベントを処理する
UILabelのタップイベントを処理して、ラベルに表示する値をアラートダイアログで入力する方法です。
UIラベルのisUserInteractionEnabledをtrueにして、touchesEndedで処理します。
class ViewController: UIViewController { let timerLabelTag = 10 @IBOutlet weak var timerLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() timerLabel.tag = timerLabelTag timerLabel.isUserInteractionEnabled = true } override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { for touch: UITouch in touches { let tag = touch.view!.tag if (tag == timerLabelTag) { // アラートダイアログで入力する let alert = UIAlertController(title: "タイマー時間セットアップ", message: "時間(秒)を入力してください", preferredStyle: .alert) let saveAction = UIAlertAction(title: "セット", style: .default) { (action:UIAlertAction!) -> Void in let textField = alert.textFields![0] as UITextField timerLabel = textField.text! } } let cancelAction = UIAlertAction(title: "キャンセル", style: .default) { (action:UIAlertAction!) -> Void in } // UIAlertControllerにtextFieldを追加 alert.addTextField { (textField:UITextField!) -> Void in } alert.addAction(saveAction) alert.addAction(cancelAction) present(alert, animated: true, completion: nil) } } } }
リンク
Excelを使って簡単にINSERT文を作る方法
今参加しているプロジェクトでは、データを挿入してテストすることがあるので、メモしておきます。 やっぱり、一つ一つInsert文を作成するより楽ですね。