как удалить хештег, @user, ссылку твита с помощью регулярного выражения - PullRequest
12 голосов
/ 04 декабря 2011

Мне нужно предварительно обработать твиты с помощью Python. Теперь мне интересно, каким было бы регулярное выражение для удаления всех хэштегов, @user и ссылок твита соответственно?

например,

  1. original tweet: @peter I really love that shirt at #Macy. http://bet.ly//WjdiW4
    • обработанный твит: I really love that shirt at Macy
  2. оригинальный твит: @shawn Titanic tragedy could have been prevented Economic Times: Telegraph.co.ukTitanic tragedy could have been preve... http://bet.ly/tuN2wx
    • обработанный твит: Titanic tragedy could have been prevented Economic Times Telegraph co ukTitanic tragedy could have been preve
  3. оригинальный твит: I am at Starbucks http://4sh.com/samqUI (7419 3rd ave, at 75th, Brooklyn)
    • обработанный твит: I am at Starbucks 7419 3rd ave at 75th Brooklyn

Мне просто нужны осмысленные слова в каждом твите. Мне не нужно имя пользователя, ссылки или знаки препинания.

Ответы [ 4 ]

23 голосов
/ 04 декабря 2011

Следующий пример является близким приближением. К сожалению, нет правильного способа сделать это только с помощью регулярного выражения. Следующее регулярное выражение просто обрезает URL (не только http), любые знаки препинания, имена пользователей или любые не алфавитно-цифровые символы. Это также отделяет слово с одним пробелом. Если вы хотите проанализировать твит, как вы намереваетесь, вам нужно больше интеллекта в системе. Некоторый алгоритм предварительного самообучения, учитывающий отсутствие стандартного формата твита.

Вот что я предлагаю.

' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())

и вот результат на ваших примерах

>>> x="@peter I really love that shirt at #Macy. http://bit.ly//WjdiW4"
>>> ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
'I really love that shirt at Macy'
>>> x="@shawn Titanic tragedy could have been prevented Economic Times: Telegraph.co.ukTitanic tragedy could have been preve... http://bit.ly/tuN2wx"
>>> ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
'Titanic tragedy could have been prevented Economic Times Telegraph co ukTitanic tragedy could have been preve'
>>> x="I am at Starbucks http://4sq.com/samqUI (7419 3rd ave, at 75th, Brooklyn) "
>>> ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
'I am at Starbucks 7419 3rd ave at 75th Brooklyn'
>>> 

и вот несколько примеров, где оно не идеально

>>> x="I c RT @iamFink: @SamanthaSpice that's my excited face and my regular face. The expression never changes."
>>> ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
'I c RT that s my excited face and my regular face The expression never changes'
>>> x="RT @AstrologyForYou: #Gemini recharges through regular contact with people of like mind, and social involvement that allows expression of their ideas"
>>> ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
'RT Gemini recharges through regular contact with people of like mind and social involvement that allows expression of their ideas'
>>> # Though after you add # to the regex expression filter, results become a bit better
>>> ' '.join(re.sub("([@#][A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
'RT recharges through regular contact with people of like mind and social involvement that allows expression of their ideas'
>>> x="New comment by diego.bosca: Re: Re: wrong regular expression? http://t.co/4KOb94ua"
>>> ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
'New comment by diego bosca Re Re wrong regular expression'
>>> #See how miserably it performed?
>>> 
5 голосов
/ 30 июня 2016

Немного поздно, но это решение предотвращает ошибки пунктуации, такие как # hashtag1, # hashtag2 (без пробелов), и реализация очень проста

import re,string

def strip_links(text):
    link_regex    = re.compile('((https?):((//)|(\\\\))+([\w\d:#@%/;$()~_?\+-=\\\.&](#!)?)*)', re.DOTALL)
    links         = re.findall(link_regex, text)
    for link in links:
        text = text.replace(link[0], ', ')    
    return text

def strip_all_entities(text):
    entity_prefixes = ['@','#']
    for separator in  string.punctuation:
        if separator not in entity_prefixes :
            text = text.replace(separator,' ')
    words = []
    for word in text.split():
        word = word.strip()
        if word:
            if word[0] not in entity_prefixes:
                words.append(word)
    return ' '.join(words)


tests = [
    "@peter I really love that shirt at #Macy. http://bet.ly//WjdiW4",
    "@shawn Titanic tragedy could have been prevented Economic Times: Telegraph.co.ukTitanic tragedy could have been preve... http://bet.ly/tuN2wx",
    "I am at Starbucks http://4sh.com/samqUI (7419 3rd ave, at 75th, Brooklyn)",
]
for t in tests:
    strip_all_entities(strip_links(t))


#'I really love that shirt at'
#'Titanic tragedy could have been prevented Economic Times Telegraph co ukTitanic tragedy could have been preve'
#'I am at Starbucks 7419 3rd ave at 75th Brooklyn'
3 голосов
/ 04 декабря 2011

Это будет работать с вашими примерами. Если у вас есть ссылки внутри ваших твитов, он потерпит неудачу, жалко .

result = re.sub(r"(?:@\S*|#\S*|http(?=.*://)\S*)", "", subject)

Edit:

работает и с внутренними ссылками, если они разделены пробелом.

Просто зайдите с API. Зачем изобретать велосипед?

0 голосов
/ 04 декабря 2011

Я знаю, что это не регулярное выражение, но:

>>>
>>> import urlparse
>>> string = '@peter I really love that shirt at #Macy. http://bit.ly//WjdiW#'
>>> new_string = ''
>>> for i in string.split():
...     s, n, p, pa, q, f = urlparse.urlparse(i)
...     if s and n:
...         pass
...     elif i[:1] == '@':
...         pass
...     elif i[:1] == '#':
...         new_string = new_string.strip() + ' ' + i[1:]
...     else:
...         new_string = new_string.strip() + ' ' + i
...
>>> new_string
'I really love that shirt at Macy.'
>>>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...