我想减少我的推特语料库的功能。出于这个原因,我打算用一个等价类标记替换用户名。用户名的特征是以@开头。我尝试使用re.sub(),但它不按预期工作。它取代了句子中的名字,而不是在句子的开头。哪里不对?用“USERNAME”替换twitter用户名(@ ...) - 如何?
#usernames (e.g. @max) are replaced with An equivalence class token
import re
with open('outfilename2.csv',"r", encoding="utf-8") as oldfile1, open('outfilename3.csv', 'w',encoding="utf-8") as newfile1:
for line in oldfile1:
line=re.sub(r"(\s)@\w+", r" USERNAME", line)
newfile1.write(line)
newfile1.close()
tweets中的用户名不在括号内,但是它们是以@开头的正常文本。 –
示例推文:“我去购物@sarah。” 目标:“我去购物USERNAME”。 –
Username.upper()使其成为大写 – IsaBostan