MacOSX mecab試しに

mecabを使う機会がありそうだから試してみる OSX Appのサンプルコード。

// 「補足: mecabは自分でインストール」

//  brew install mecab mecab-ipadic

//

//  Macmecabをインストールした後

//

//  XcodeBuild Settings

//  – Search Paths

//    – Header Search Paths mecab-config —cflags」の値

//    – Library Search Pathsに「mecab-config —lstdc++」の値

//  – Linking

//    – Other Linker Flags -lmecab を追加

#import “AppDelegate.h”

#include <iostream>

#include <mecab.h>

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;

@property (weak) NSTextField *textField;

@property (weak) NSTextField *label;

@end

@implementation AppDelegate

– (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    

    NSTextField *f = [[NSTextField alloc] initWithFrame:NSMakeRect(20, 200, self.window.frame.size.width40, 130)];

    [self.window.contentView addSubview:f];

    self.textField = f;

    

    

    // initial string

    char input[1024] = “Macアプリで形態素解析を試してみる。;

    f.stringValue = [[NSString alloc] initWithUTF8String:input];

    

    

    NSButton *b = [[NSButton alloc] initWithFrame:NSMakeRect(20, 160, 50, 30)];

    b.title = @”解析;

    b.target = self;

    b.action = @selector(touch);

    [self.window.contentView addSubview:b];

    

    NSTextField *l = [[NSTextField alloc] initWithFrame:NSMakeRect(20, 20, self.window.frame.size.width40, 130)];

    [l setDrawsBackground:NO];

    [l setBezeled:NO];

    [self.window.contentView addSubview:l];

    self.label = l;

}

– (void)touch {

    MeCab::Tagger *tagger = MeCab::createTagger(“”);

    const char* str = tagger->parse([self.textField.stringValue UTF8String]);

    self.label.stringValue = [[NSString alloc] initWithUTF8String:str];

    delete tagger;

}

– (void)applicationWillTerminate:(NSNotification *)aNotification {

    // Insert code here to tear down your application

}

@end