简体   繁体   English

python unicodedata库的C ++实现

[英]C++ implementation of python unicodedata library

New user here, please be gentle. 新用户在这里,请保持柔和。

we are looking to implement a piece of python code in c++, but it involves some intricate unicode library called unicodedata, in particular this function 我们正在寻找在c ++中实现一段python代码的方法,但是它涉及一些称为unicodedata的复杂unicode库,尤其是此函数

unicodedata.category('A')  # 'L'etter, 'u'ppercase
'Lu'

Any chance that this can be readily achieved in c++? 在C ++中可以很容易地实现这一点吗? Would embedding compiled python code in c++ be worthwhile, assuming we want to do this in the context of online tensorflow model serving? 假设我们要在在线tensorflow模型服务的上下文中这样做,将编译后的python代码嵌入c ++是否值得? Thanks! 谢谢!

Just stick the output of this Python code into a C++ source file: 只需将此Python代码的输出粘贴到C ++源文件中:

import unicodedata

print('typedef enum {Cn, Cc, Cf, Co, Cs, Ll, Lm, Lo, Lt, Lu, Mc, Me, Mn, Nd, Nl, No, Pc, Pd, Pe, Pf, Pi, Po, Ps, Sc, Sk, Sm, So, Zl, Zp, Zs} CATEGORY_e;')
print('const CATEGORY_e CHAR_CATEGORIES[] = {%s};' % ', '.join(unicodedata.category(chr(codepoint)) for codepoint in range(0x110000)))

(If you are still using Python 2.x instead of 3.x, replace chr with unichr .) (如果您仍在使用Python 2.x而不是3.x,请用unichr替换chr 。)

You now have a convenient lookup table of Unicode character categories to use in your C++ programs. 现在,您可以在C ++程序中使用一个方便的Unicode字符类别查找表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM