twitter-text-python (ttp) 模块 – Python
twitter-text-python
是Python的 Tweet 解析器和格式化程序。在许多事情中,这个模块可以执行的任务是:
- reply :回复推文的句柄的用户名。
- users :推文中提到的所有用户名。
- tags :推文中提到的所有主题标签。
- urls :推文中提到的所有 URL。
- html :向上述字段添加超链接。
示例 1:
# import the twitter-text-python module
from ttp import ttp
# the text to be parsed
tweet_text = ("@twitter Sample tweet containing different components." +
"# gfg # tweeple Visit : https://twitter.com @TwitterIndia")
# instantiating the Parser
p = ttp.Parser()
# parsing the text
result = p.parse(tweet_text)
# printing the username of the
# account being replied to
print("The username being replied to is : " + result.reply)
# printing all the usernames
# mentioned in the tweet
print("\nAll the usernames mentioned are : " + str(result.users))
# printing all the hashtags
# mentioned in the tweet
print("\nAll the hashtags mentioned are : " + str(result.tags))
# printing all the URLs
# mentioned in the tweet
print("\nAll the URLs mentioned are : " + str(result.urls))
# adding hyperlinks to usernames,
# hashtags and URLs
print(result.html)
输出 :
The username being replied to is : twitter
All the usernames mentioned are : [‘twitter’, ‘TwitterIndia’]
All the hashtags mentioned are : [‘gfg’, ‘tweeple’]
All the URLs mentioned are : [‘https://twitter.com’]
@twitter Sample tweet containing different components.#gfg #tweeple Visit : https://twitter.com @TwitterIndia
示例 2:我们还可以通过include_spans = True
找到字符串(POS) 的位置。
# import the twitter-text-python module
from ttp import ttp
# the text to be parsed
tweet_text = ("@twitter Sample tweet containing different components." +
"# gfg # tweeple Visit : https://twitter.com @TwitterIndia")
# instantiating the Parser
# with spans
p = ttp.Parser(include_spans = True)
# parsing the text
result = p.parse(tweet_text)
# printing all the usernames
# mentioned in the tweet with POS
print("All the usernames mentioned are : " + str(result.users))
# printing all the hashtags
# mentioned in the tweet with POS
print("\nAll the hashtags mentioned are : " + str(result.tags))
# printing all the URLs
# mentioned in the tweet with POS
print("\nAll the URLs mentioned are : " + str(result.urls))
输出 :
All the usernames mentioned are : [(‘twitter’, (0, 8)), (‘TwitterIndia’, (130, 143))]
All the hashtags mentioned are : [(‘gfg’, (96, 100)), (‘tweeple’, (101, 109))]
All the URLs mentioned are : [(‘https://twitter.com’, (76, 95))]
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。