@blog.justoneplanet.info

日々勉強

CakePHPのrenderメソッドでViewファイルを指定する

以下のように指定することも多々ある。

$this->render('/ajax/json');

Cake内部でファイルを探索する際、open_basedirの制限に引っかかることがあるので以下のように記述すると良い。

$this->render(null, null, VIEWS . 'ajax' . DS . 'json.ctp');

MeCabをインストールする

■インストール

以下のコマンドでインストールできる。

wget http://sourceforge.net/projects/mecab/files/mecab/0.98/mecab-0.98.tar.gz/download
tar xvzf mecab-0.98.tar.gz
cd mecab-0.98
./configure
make
make install

辞書

utf-8で使用したいので以下のconfigureオプションを使用する。

wget http://sourceforge.net/projects/mecab/files/mecab-ipadic/2.7.0-20070801/mecab-ipadic-2.7.0-20070801.tar.gz/download
tar xvzf mecab-ipadic-2.7.0-20070801.tar.gz
cd mecab-ipadic-2.7.0-20070801
./configure --with-charset=utf8
make
make install

libiconvが必要になる。

■実行

以下のようにして使用する。

mecab
にわにはにわにわとりがいる
に	助詞,格助詞,一般,*,*,*,に,ニ,ニ
わに	名詞,一般,*,*,*,*,わに,ワニ,ワニ
はにわ	名詞,一般,*,*,*,*,はにわ,ハニワ,ハニワ
にわとり	名詞,一般,*,*,*,*,にわとり,ニワトリ,ニワトリ
が	助詞,格助詞,一般,*,*,*,が,ガ,ガ
いる	動詞,自立,*,*,一段,基本形,いる,イル,イル
EOS

ちょっと意地悪すぎたので入力を漢字にする。

庭には二羽鶏がいる
庭	名詞,一般,*,*,*,*,庭,ニワ,ニワ
に	助詞,格助詞,一般,*,*,*,に,ニ,ニ
は	助詞,係助詞,*,*,*,*,は,ハ,ワ
二	名詞,数,*,*,*,*,二,ニ,ニ
羽	名詞,接尾,助数詞,*,*,*,羽,ワ,ワ
鶏	名詞,一般,*,*,*,*,鶏,ニワトリ,ニワトリ
が	助詞,格助詞,一般,*,*,*,が,ガ,ガ
いる	動詞,自立,*,*,一段,基本形,いる,イル,イル
EOS

正しく分類できた。

■Pythonから実行

PHP用のライブラリはあるようだが、久しぶりのPythonで楽しむことにする。

python-devel

セットアップスクリプトを実行するのに必要になる。

yum insatll python-devel

mecab-python

以下のコマンドでインストールできる。

wget http://sourceforge.net/projects/mecab/files/mecab-python/0.98/mecab-python-0.98.tar.gz/download
tar xvzf mecab-python-0.98.tar.gz
cd mecab-python-0.98
python setup.py build
python setup.py install

テストスクリプトの文字コードを指定する。

vi test.py

2行目に以下のコードを加える。

# -*- coding: utf-8 -*-

以下のコマンドで実行する。

python test.py

■参考

スクリプト言語のバインディング

UINavigationControllerを使う

新しい事をやりすぎて古いことを忘れそうなのでメモしておく。

■実装

早速実装する。

AppDelegate.h

#import <UIKit/UIKit.h>
#import "MainViewController.h"

@class MainViewController;

@interface SampleNavigationAppDelegate : NSObject <UIApplicationDelegate> {
@private
    UINavigationController *nav;
    MainViewController *mainViewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UINavigationController *nav;
@property (nonatomic, retain) MainViewController *mainViewController;
@end

AppDelegate.m

#import "SampleNavigationAppDelegate.h"

@implementation SampleNavigationAppDelegate
@synthesize nav;
@synthesize mainViewController;

@synthesize window=_window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    mainViewController = [[MainViewController alloc] init];
    nav = [[UINavigationController alloc] initWithRootViewController:mainViewController];
    nav.view.frame = [UIScreen mainScreen].applicationFrame;
    [self.window addSubview:nav.view];
    [self.window makeKeyAndVisible];
    return YES;
}

MainViewController.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"

@class SecondViewController;

@interface MainViewController : UIViewController {
    SecondViewController *secondViewController;
}
@property (nonatomic, retain) SecondViewController *secondViewController;
@end

MainViewController.m

@implementation MainViewController
@synthesize secondViewController;

-(void)onclick:(UIButton*)sender{
    secondViewController = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:secondViewController animated:YES];

}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setFrame:CGRectMake(0, 0, 320, 480)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    [label setText:@"first view"];
    [self.view addSubview:label];
    [label release];

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btn setFrame:CGRectMake(250, 0, 50, 30)];
    [btn addTarget:self action:@selector(onclick:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:btn];
}

画面は凄い適当だけど・・・