Advertisement
Guest User

Untitled

a guest
May 25th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. --[[
  2. Lua wrapper for OpenCC.
  3.  
  4. --]]
  5.  
  6. local ffi = require("ffi")
  7. local opencc = ffi.load("/usr/lib64/libopencc.so.2")
  8.  
  9. -- Define C API bindings
  10. ffi.cdef[[
  11. typedef void* opencc_t;
  12. opencc_t opencc_open(const char* configFileName);
  13. opencc_t opencc_open_w(const wchar_t* configFileName);
  14. int opencc_close(opencc_t opencc);
  15. size_t opencc_convert_utf8_to_buffer(opencc_t opencc, const char* input, size_t length, char* output);
  16. char* opencc_convert_utf8(opencc_t opencc, const char* input, size_t length);
  17. void opencc_convert_utf8_free(char* str);
  18. const char* opencc_error(void);
  19. ]]
  20.  
  21. local _M = {}
  22.  
  23. --[[
  24. Function to convert input string according to given configuration.
  25.  
  26. Parameters:
  27. @input: input string, cannot be null
  28. @config: opencc configuration file, can be "t2s.json", "s2t.json" or custom file path.
  29.  
  30. Returns:
  31. the converted string
  32. --]]
  33. function _M.convert(input, config)
  34. local opencc_instance = opencc.opencc_open(config)
  35. local cd = opencc.opencc_convert_utf8(opencc_instance, input, -1)
  36. local ret = ffi.string(cd)
  37. opencc.opencc_convert_utf8_free(cd)
  38. opencc.opencc_close(opencc_instance)
  39. return ret
  40. end
  41.  
  42. return _M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement