Skip to content

Commit

Permalink
get_emoticons function should return dict instead of set
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillarAnand committed Dec 29, 2014
1 parent c8280da commit 13a83e5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
5 changes: 3 additions & 2 deletions nidaba/features/_util/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def get_emoticons(text):
"""
Return a list of emoticons in the given text.
:param text: String.
:return: List of emoticons.
:return: Dictionary - emoticons as keys and their count as values.
"""
emoticons = (":-) :) :D :o) :] :3 :c) :> =] 8) =) :} :^) :っ) :-D 8-D 8D"
"x-D xD X-D XD =-D =D =-3 =3 B^D :-)) >:[ :-( :( :-c :c :-<"
Expand All @@ -174,4 +174,5 @@ def get_emoticons(text):
":-###.. :###.. <:-| ಠ_ಠ <*)))-{ ><(((*> ><> \o/ *\0/*"
"@}-;-'--- @>-->-- ~(_8^(I) 5:-) ~:-\ //0-0\\ *<|:-) =:o]"
",:-) 7:^] <3 </3").split()
return frozenset(emoticons).intersection(text.split())
return dict((emoticon, text.count(emoticon)) for emoticon in emoticons

This comment has been minimized.

Copy link
@Ffisegydd

Ffisegydd Dec 29, 2014

Member

This will fail some of the time. Consider the text "This is my code :] x = text[2:]". This will return a count of 2 for the smiley :] even though one of the smileys is part of some code.

What about something like

from collections import Counter
Counter(word for word in text.split() if word in emoticons)

You could also accept strings or lists as an argument, and if it's a list then you don't need to split it. Something to think about as we have Post.text.words which is a tokenised list of words.

if emoticon in text.split())
8 changes: 4 additions & 4 deletions nidaba/features/test/test_questions_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def test_get_emoticons():
Test the get_emoticons() _util function.
:return: None
"""
text = 'these are emoticons :) :D :-) '
assert question.get_emoticons(text) == {':)', ':D', ':-)'}
code = 'this is code x = [:D], y = [0:3], ED8 = 12'
assert question.get_emoticons(code) != {':D', '0:3', 'D8'}
text = 'these are emoticons :) :D :)'
assert question.get_emoticons(text) == {':)': 2, ':D': 1}
code = 'this is code x = [:D], y = [0:3], ED8 = [:D]'
assert question.get_emoticons(code) != {':D': 2, '0:3': 1}

0 comments on commit 13a83e5

Please # to comment.