#import "UntitledAppDelegate.h"
@implementation UntitledAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
mecab = mecab_new2("-d /usr/lib/mecab/dic/apple/ja/LE");
if (mecab == NULL) {
fprintf(stderr, "error in mecab_new2: %s\n", mecab_strerror(NULL));
}
}
- (void)dealloc
{
mecab_destroy(mecab);
[super dealloc];
}
- (NSString *)inputString
{
return [[_inputString retain] autorelease];
}
- (void)setInputString:(NSString *)inputString
{
[_inputString release];
_inputString= [inputString retain];
[self setMecabResult:[self mecabResultWithString:_inputString]];
}
- (NSArray *)mecabResult
{
return [[_mecabResult retain] autorelease];
}
- (void)setMecabResult:(NSArray *)mecabResult
{
[_mecabResult release];
_mecabResult= [mecabResult retain];
}
- (NSArray *)mecabResultWithString:(NSString *)input
{
NSMutableArray *ret= [NSMutableArray array];
if (input==nil || [input length]==0)
return ret;
const char *buf= [input cStringUsingEncoding:NSUTF16LittleEndianStringEncoding];
const mecab_node_t *node = mecab_sparse_tonode(mecab, buf);
NSUInteger l= [input lengthOfBytesUsingEncoding:NSUTF16LittleEndianStringEncoding];
const mecab_node_t *node = mecab_sparse_tonode2(mecab, buf, l);
if (node == NULL) {
return ret;
}
node = node->next;
for (; node->next != NULL; node = node->next) {
NSString *w = [[NSString alloc] initWithBytes:node->surface
length:node->length
encoding:NSUTF16LittleEndianStringEncoding];
[ret addObject:w];
[w release];
}
return ret;
}
@end
0 件のコメント:
コメントを投稿