所以我想用“HAPPY”取代所有快乐的表情符号,反之亦然“SAD”用于文本文件的悲伤表情符号。但代码不能正常工作。虽然它检测到表情符号(截至目前:-)),但在下面的例子中它没有用文本替换表情符号,它只是附加文本而且它也附加了两次,原因我似乎不理解。
dict_sad={":-(":"SAD", ":(":"SAD", ":-|":"SAD", ";-(":"SAD", ";-<":"SAD", "|-{":"SAD"}
dict_happy={":-)":"HAPPY",":)":"HAPPY", ":o)":"HAPPY",":-}":"HAPPY",";-}":"HAPPY",":->":"HAPPY",";-)":"HAPPY"}
#THE INPUT TEXT#
a="guys beautifully done :-)"
for i in a.split():
for j in dict_happy.keys():
if set(j).issubset(set(i)):
print "HAPPY"
continue
for k in dict_sad.keys():
if set(k).issubset(set(i)):
print "SAD"
continue
if str(i)==i.decode('utf-8','replace'):
print i
输入文本
a="guys beautifully done :-)"
OUTPUT(“HAPPY”即将播出两次,表情符号也不会消失)
guys
-
beautifully
done
HAPPY
HAPPY
:-)
预期输出
guys
beautifully
done
HAPPY
答案 0 :(得分:5)
您正在将每个单词和每个表情符号转换为一个集合;这意味着您正在寻找单个字符的重叠。您可能希望最多使用完全匹配:
for i in a.split():
for j in dict_happy:
if j == i:
print "HAPPY"
continue
for k in dict_sad:
if k == i:
print "SAD"
continue
您可以直接迭代字典 ,无需在那里调用.keys()
。你实际上似乎没有使用字典值;你可以这么做:
for word in a.split():
if word in dict_happy:
print "HAPPY"
if word in dict_sad:
print "SAD"
然后可能使用集而不是字典。然后可以将其简化为:
words = set(a.split())
if dict_happy.viewkeys() & words:
print "HAPPY"
if dict_sad.viewkeys() & words:
print "SAD"
使用键上的dictionary view作为一组。尽管如此,使用套装仍然会更好:
sad_emoticons = {":-(", ":(", ":-|", ";-(", ";-<", "|-{"}
happy_emoticons = {":-)", ":)", ":o)", ":-}", ";-}", ":->", ";-)"}
words = set(a.split())
if sad_emoticons & words:
print "HAPPY"
if happy_emoticons & words:
print "SAD"
如果您想从文字中删除表情符号,则必须过滤单词:
for word in a.split():
if word in dict_happy:
print "HAPPY"
elif word in dict_sad:
print "SAD"
else:
print word
或者更好的是,结合两个词典并使用dict.get()
:
emoticons = {
":-(": "SAD", ":(": "SAD", ":-|": "SAD",
";-(": "SAD", ";-<": "SAD", "|-{": "SAD",
":-)": "HAPPY",":)": "HAPPY", ":o)": "HAPPY",
":-}": "HAPPY", ";-}": "HAPPY", ":->": "HAPPY",
";-)": "HAPPY"
}
for word in a.split():
print emoticons.get(word, word)
这里我将当前单词作为查找键和默认值传递;如果当前单词不是表情符号,则会打印单词本身,否则会打印单词SAD
或HAPPY
。
答案 1 :(得分:1)
我没有使用词典,而是使用了列表。使代码更简单:
list_sad = [":(", ":-("]
list_happy = [":)", ":-)"]
a = "guys beautifully done :-)"
for i in a.split():
if i in list_sad:
print ("SAD")
elif i in list_happy:
print ("HAPPY")
else:
print (i)