文件处理常用函数
import re
import emoji
# 写入文件
def write_file(path,content):
file = open(path,'w',encoding="utf-8")
file.write(content)
file.close()
# 读取文件
def read_file(path):
file = open(path,'r',encoding="utf-8")
content = file.readlines()
file.close()
return content
# 去除字符串中的emoji(表情)
def remove_emoji(text):
"""去除字符串中的表情符号"""
text_without_emoji = re.sub(emoji.get_emoji_regexp(), '', text)
return text_without_emoji
# numpy计算余弦相似度
def cosine_similarity(a,b):
return np.dot(a, b) / (np.linalg.norm(a, ord=2) * np.linalg.norm(b, ord=2))