用于正确解析或验证twitter文本的库
twitter-text-parser的Python项目详细描述
功能
这个库根据the documentation from Twitter Developers计算tweet消息的长度, 这样您就可以验证tweet,而无需调用web api。 尽管计算字符看起来很简单,但实际上它非常复杂,尤其是当文本包含cjk字符、url或emojis时。
最初的twitter文本库也有hit hight highlighting和auto linking功能, 但是这个python端口还不支持它们。
用法
安装
$ pip install twitter-text-parser
示例
有关详细信息,请参见the API reference。
fromtwitter_textimportparse_tweet,extract_emojis_with_indices,extract_urls_with_indicestext='english text 日本語 ? https://example.com'assertparse_tweet(text).asdict()=={'weightedLength':46,'valid':True,'permillage':164,'validRangeStart':0,'validRangeEnd':38,'displayRangeStart':0,'displayRangeEnd':38}assertextract_urls_with_indices(text)==[{'url':'https://example.com','indices':[19,38]}]assertextract_emojis_with_indices(text)==[{'emoji':'?','indices':[17,18]}]