获取 Twitter 字符数 [英] Getting Twitter characters count

查看:31
本文介绍了获取 Twitter 字符数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,它是 Twitter 推文的编辑器,它计算文本以使其少于 280 个字符作为 Twitter 限制.

I have a program, it is an editor for Twitter tweets, it's counting the text to make it less than 280 character as twitter restriction.

我像这样使用那个 utf8 属性:

I use for that utf8 property like this:

var str = "℞"
let r = str.utf8.count

结果 = 3

这个符号 (℞) 更像是它在 twitter 计数器中只需要 2 个字符,但这段代码的结果给了我 3,所以我不能给用户准确的字符数!

This symbol (℞) and more like it takes only 2 character in twitter counter but the result in this code gave me 3, so i can't give the user the exact character count!

我怎样才能得到正确的计数:2

How can I get the correct count: 2

推荐答案

计数字符

推文长度是通过 NFC 中的代码点数量来衡量的文本的规范化版本.

Tweet length is measured by the number of codepoints in the NFC normalized version of the text.

在Swift中,你可以通过precomposedStringWithCanonicalMapping获取NFC规范化形式,通过unicodeScalars.count获取codepoints的数量.

In Swift, you can get the NFC normalized form through precomposedStringWithCanonicalMapping, and the number of codepoints by unicodeScalars.count.

所以,Swift 中正确的代码应该是这样的:

So, the right code in Swift should be like this:

var str = "℞"
let r = str.precomposedStringWithCanonicalMapping.unicodeScalars.count
print(r) //->1

上面的代码与网络上的一些字符计数器显示出一致的结果,我不明白为什么你会得到 2 for .

The code above shows consistent result with some character counters on the web, I do not understand why you get 2 for .

(感谢 Rakesha Shastri.)我相信上面的代码正确地实现了我上面链接的文档中描述的规范.

(Thanks to Rakesha Shastri.) I believe the code above correctly implements the specification described in the documentation I linked above.

但据报道,实际的 Twitter 与文档中的不完全相同.(抱歉,我自己不会发推文.)我们可能需要猜测或寻找其他可靠来源以使其适合实际的 Twitter.

But it is reported that the actual Twitter does not work exactly as in the doc. (Sorry, I do not tweet myself.) We may need to guess or find another reliable source to make it fit for the actual Twitter.

我尝试了官方库 文本推文解析库,但它显示的结果与我的代码相同.

I tried the official library text Tweet parsing library, but it shows the same result as my code.

let len = TwitterText.tweetLength(str)
print(len) //->1

(不过,TwitterText.tweetLength(_:) 的代码要复杂得多,因为它处理 t.co 链接.因此,当文本中包含某些 URL 时,它会生成与我的代码不同的输出.)

(Though, the code of TwitterText.tweetLength(_:) is far more complex, as it handles t.co links. So, when some URLs are included in the text, it generates different output than my code.)

(更新)

我不确定所引用的 twitter 应用程序不是开源的,但我猜它们显示的是 text Tweet parsing library 页面中描述的 加权长度上面有链接.

I'm not sure as the referred twitter apps are not open-source, but I guess they are showing the weighted length described in the text Tweet parsing library page linked above.

您可能需要通过使用 pod 导入库来编写类似的内容.

You may need to write something like this with importing the library using pod.

let config = TwitterTextConfiguration(fromJSONResource: kTwitterTextParserConfigurationV2)
let parser = TwitterTextParser(configuration: config)
let result = parser.parseTweet(str)
print(result.weightedLength) //->2

这篇关于获取 Twitter 字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆