<commit_before>//
//  Mecab.swift
//  MeCab
//
//  Created by <NAME> on 12/25/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

#if os(Linux)
    import Glibc
#endif

import CMeCab

public enum MecabError: ErrorType {
    case MecabInitializeError
    case NodeParseError
}

public protocol Tokenzier {
    associatedtype T: TokenNode
    func tokenize(str: String) throws -> [T]
}

public class Mecab: Tokenzier {
    
    let mecab: COpaquePointer
    let mutex: UnsafeMutablePointer<pthread_mutex_t> = UnsafeMutablePointer.alloc(sizeof(pthread_mutex_t))
    public init() throws {
        self.mecab = mecab_new(0, nil)
        if mecab == nil {
            throw MecabError.MecabInitializeError
        }
        pthread_mutex_init(mutex, nil)
    }
    
    public func tokenize(str: String) throws -> [Node] {
        var nodes: [Node] = []
        
        pthread_mutex_lock(mutex)
        defer {
            pthread_mutex_unlock(mutex)
        }
        
        
        var node = mecab_sparse_tonode(mecab, str.withCString{ $0 })
        while true {
            if node == nil {
                break
            }
            nodes.append(try Node(node))
            node = UnsafePointer(node.memory.next)
        }
        return nodes
    }
    
    deinit {
        mecab_destroy(mecab)
        pthread_mutex_destroy(mutex)
    }
}<commit_msg>fix error related source string buffer in tokenizer<commit_after>//
//  Mecab.swift
//  MeCab
//
//  Created by <NAME> on 12/25/15.
//  Copyright © 2015 <NAME>. All rights reserved.
//

#if os(Linux)
    import Glibc
#endif

import CMeCab

public enum MecabError: ErrorType {
    case MecabInitializeError
    case NodeParseError
}

public protocol Tokenzier {
    associatedtype T: TokenNode
    func tokenize(str: String) throws -> [T]
}

public class Mecab: Tokenzier {
    
    let mecab: COpaquePointer
    let mutex: UnsafeMutablePointer<pthread_mutex_t> = UnsafeMutablePointer.alloc(sizeof(pthread_mutex_t))
    public init() throws {
        self.mecab = mecab_new(0, nil)
        if mecab == nil {
            throw MecabError.MecabInitializeError
        }
        pthread_mutex_init(mutex, nil)
    }
    
    public func tokenize(str: String) throws -> [Node] {
        var nodes: [Node] = []
        
        pthread_mutex_lock(mutex)
        defer {
            pthread_mutex_unlock(mutex)
        }
        
        return try str.withCString{ buf in
            var nodes: [Node] = []
            var node = mecab_sparse_tonode(mecab, buf)
            while true {
                if node == nil {
                    break
                }
                nodes.append(try Node(node))
                node = UnsafePointer(node.memory.next)
            }
            return nodes
        }
    }
    
    deinit {
        mecab_destroy(mecab)
        pthread_mutex_destroy(mutex)
    }
}
{'fraction_non_alphanumeric': 0.08945353289244692, 'fraction_numerical': 0.008005569091541943, 'mean_word_length': 1.8682634730538923, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '874479', 'n_tokens_mistral': 949, 'n_tokens_neox': 915, 'n_words': 208}