1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::{
    ffi::{CStr, CString},
    mem::forget,
    os::raw::c_char,
    path::Path,
    ptr,
};

#[repr(C)]
#[derive(PartialEq, Clone, Debug)]
pub struct TextRange {
    pub s: usize,
    pub e: usize,
}

#[repr(C)]
#[derive(PartialEq, Clone, Debug)]
pub struct Wordcut {}

fn wordcut_new_with_dict_path(path: &Path) -> *mut Wordcut {
    match wordcut_engine::load_dict(path) {
        Ok(dict) => {
            let wordcut = wordcut_engine::Wordcut::new(dict);
            let boxed_wordcut = Box::new(wordcut);
            Box::into_raw(boxed_wordcut) as *mut Wordcut
        }
        Err(e) => {
            eprintln!("{}", e);
            return ptr::null::<Wordcut>() as *mut Wordcut;
        }
    }
}

#[no_mangle]
pub extern "C" fn wordcut_new_with_dict(path: *const c_char) -> *mut Wordcut {
    let path = unsafe { CStr::from_ptr(path) }.to_str().unwrap();
    let path = Path::new(path);
    wordcut_new_with_dict_path(path)
}

#[no_mangle]
pub extern "C" fn wordcut_new_with_dict_from_default_dir(path: *const c_char) -> *mut Wordcut {
    let path = unsafe { CStr::from_ptr(path) }.to_str().unwrap();
    let path = chamkho::cargo_dir().join(path);
    wordcut_new_with_dict_path(&path)
}

#[no_mangle]
pub extern "C" fn delete_wordcut(wordcut: *mut Wordcut) {
    unsafe {
        Box::from_raw(wordcut as *mut wordcut_engine::Wordcut);
    }
}

#[no_mangle]
pub extern "C" fn delete_text_ranges(text_ranges: *mut TextRange, range_count: usize) {
    unsafe { Vec::from_raw_parts(text_ranges, range_count, range_count) };
}

#[no_mangle]
pub extern "C" fn wordcut_into_text_ranges(
    wordcut: *const Wordcut,
    text: *const c_char,
    range_count: *mut usize,
) -> *mut TextRange {
    let wordcut: *const wordcut_engine::Wordcut = wordcut as *const wordcut_engine::Wordcut;
    let text = unsafe { CStr::from_ptr(text) }.to_str().unwrap();
    let text_ranges = unsafe { (*wordcut).segment(text) };
    let mut text_ranges: Vec<TextRange> = text_ranges
        .into_iter()
        .map(|r| TextRange { s: r.s, e: r.e })
        .collect();
    unsafe {
        *range_count = text_ranges.len();
    };
    let p = text_ranges.as_mut_ptr();
    forget(text_ranges);
    return p;
}

#[no_mangle]
pub extern "C" fn wordcut_into_strings(
    wordcut: *const Wordcut,
    text: *const c_char,
    string_count: *mut usize,
) -> *mut *mut c_char {
    let wordcut: *const wordcut_engine::Wordcut = wordcut as *const wordcut_engine::Wordcut;
    let text = unsafe { CStr::from_ptr(text) }.to_str().unwrap();
    let strings = unsafe { (*wordcut).segment_into_strings(text) };
    let mut strings: Vec<*mut c_char> = strings
        .into_iter()
        .map(|s| CString::new(s).unwrap().into_raw())
        .collect();
    unsafe {
        *string_count = strings.len();
    };
    let p = strings.as_mut_ptr();
    forget(strings);
    return p;
}

#[no_mangle]
pub extern "C" fn delete_strings(strings: *mut *mut c_char, string_count: usize) {
    unsafe {
        let raw_strings = Vec::from_raw_parts(strings, string_count, string_count);
        raw_strings.into_iter().for_each(drop);
    };
}

#[no_mangle]
pub extern "C" fn wordcut_put_delimiters(
    wordcut: *const Wordcut,
    text: *const c_char,
    delim: *const c_char,
) -> *mut c_char {
    let wordcut: *const wordcut_engine::Wordcut = wordcut as *const wordcut_engine::Wordcut;
    let text = unsafe { CStr::from_ptr(text) }.to_str().unwrap();
    let delim = unsafe { CStr::from_ptr(delim) }.to_str().unwrap();
    let segmented_text = unsafe { (*wordcut).put_delimiters(text, delim) };
    let p = CString::new(segmented_text).unwrap().into_raw();
    return p;
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::ffi::CString;

    #[test]
    fn test_wordcut_into_text_ranges() {
        let text = CString::new("ลากา").unwrap().into_raw();
        let wordcut = wordcut_new_with_dict_from_default_dir(
            CString::new("data/thai.txt").unwrap().into_raw(),
        );
        let mut range_count = 0;
        let text_ranges = wordcut_into_text_ranges(wordcut, text, &mut range_count);
        assert_eq!(range_count, 2);
        unsafe {
            assert_eq!(*text_ranges, TextRange { s: 0, e: 2 });
            assert_eq!(*text_ranges.offset(1), TextRange { s: 2, e: 4 });
        }
        delete_text_ranges(text_ranges, range_count);
        delete_wordcut(wordcut);
    }

    #[test]
    fn test_wordcut_into_strings() {
        let text = CString::new("ลากา").unwrap().into_raw();
        let wordcut = wordcut_new_with_dict_from_default_dir(
            CString::new("data/thai.txt").unwrap().into_raw(),
        );
        let mut string_count = 0;
        let segmented_strings = wordcut_into_strings(wordcut, text, &mut string_count);
        assert_eq!(string_count, 2);
        unsafe {
            let s0 = CStr::from_ptr(*segmented_strings).to_str().unwrap();
            let s1 = CStr::from_ptr(*segmented_strings.offset(1))
                .to_str()
                .unwrap();
            assert_eq!(s0, "ลา");
            assert_eq!(s1, "กา");
        }
        delete_strings(segmented_strings, string_count);
        delete_wordcut(wordcut);
    }

    #[test]
    fn test_wordcut_put_delimiters() {
        let text = CString::new("ลากา").unwrap().into_raw();
        let delim = CString::new("---").unwrap().into_raw();
        let wordcut = wordcut_new_with_dict_from_default_dir(
            CString::new("data/thai.txt").unwrap().into_raw(),
        );
        let segmented_text = wordcut_put_delimiters(wordcut, text, delim);
        unsafe {
            let s = CStr::from_ptr(segmented_text).to_str().unwrap();
            assert_eq!(s, "ลา---กา");
        }
        delete_wordcut(wordcut);
    }
}