Parsing a tweet to extract hashtags into an array

前端 未结 9 733
旧时难觅i
旧时难觅i 2020-12-03 05:33

I am having a heck of a time taking the information in a tweet including hashtags, and pulling each hashtag into an array using Python. I am embarrassed to even put what I

9条回答
  •  春和景丽
    2020-12-03 06:00

    Suppose that you have to retrieve your #Hashtags from a sentence full of punctuation symbols. Let's say that #stackoverflow #people and #helpfulare terminated with different symbols, you want to retrieve them from text but you may want to avoid repetitions:

    >>> text = "I love #stackoverflow, because #people... are very #helpful! Are they really #helpful??? Yes #people in #stackoverflow are really really #helpful!!!"
    

    if you try with set([i for i in text.split() if i.startswith("#")]) alone, you will get:

    >>> set(['#helpful???',
     '#people',
     '#stackoverflow,',
     '#stackoverflow',
     '#helpful!!!',
     '#helpful!',
     '#people...'])
    

    which in my mind is redundant. Better solution using RE with module re:

    >>> import re
    >>> set([re.sub(r"(\W+)$", "", j) for j in set([i for i in text.split() if i.startswith("#")])])
    >>> set(['#people', '#helpful', '#stackoverflow'])
    

    Now it's ok for me.

    EDIT: UNICODE #Hashtags

    Add the re.UNICODE flag if you want to delete punctuations, but still preserving letters with accents, apostrophes and other unicode-encoded stuff which may be important if the #Hashtags may be expected not to be only in english... maybe this is only an italian guy nightmare, maybe not! ;-)

    For example:

    >>> text = u"I love #stackoverflòw, because #peoplè... are very #helpfùl! Are they really #helpfùl??? Yes #peoplè in #stackoverflòw are really really #helpfùl!!!"
    

    will be unicode-encoded as:

    >>> u'I love #stackoverfl\xf2w, because #peopl\xe8... are very #helpf\xf9l! Are they really #helpf\xf9l??? Yes #peopl\xe8 in #stackoverfl\xf2w are really really #helpf\xf9l!!!'
    

    and you can retrieve your (correctly encoded) #Hashtags in this way:

    >>> set([re.sub(r"(\W+)$", "", j, flags = re.UNICODE) for j in set([i for i in text.split() if i.startswith("#")])])
    >>> set([u'#stackoverfl\xf2w', u'#peopl\xe8', u'#helpf\xf9l'])
    

    EDITx2: UNICODE #Hashtags and control for # repetitions

    If you want to control for multiple repetitions of the # symbol, as in (forgive me if the text example has become almost unreadable):

    >>> text = u"I love ###stackoverflòw, because ##################peoplè... are very ####helpfùl! Are they really ##helpfùl??? Yes ###peoplè in ######stackoverflòw are really really ######helpfùl!!!"
    >>> u'I love ###stackoverfl\xf2w, because ##################peopl\xe8... are very ####helpf\xf9l! Are they really ##helpf\xf9l??? Yes ###peopl\xe8 in ######stackoverfl\xf2w are really really ######helpf\xf9l!!!'
    

    then you should substitute these multiple occurrences with a unique #. A possible solution is to introduce another nested implicit set() definition with the sub() function replacing occurrences of more-than-1 # with a single #:

    >>> set([re.sub(r"#+", "#", k) for k in set([re.sub(r"(\W+)$", "", j, flags = re.UNICODE) for j in set([i for i in text.split() if i.startswith("#")])])])
    >>> set([u'#stackoverfl\xf2w', u'#peopl\xe8', u'#helpf\xf9l']) 
    

提交回复
热议问题