как удалить некоторые знаки препинания из фрейма данных с помощью регулярного выражения - PullRequest
0 голосов
/ 09 июля 2020

У меня есть сценарий python, который очищает текст с помощью регулярного выражения, чтобы получить значимую строку с удалением всех ненужных символов, таких как стоп-слова, гиперссылки, знаки препинания. для печати наиболее существующего слова отображается следующая пунктуация.

("''", 25), (',', 23), ('-', 22), ('' ', 20) ,

это мой код:

import re
from collections import Counter
from string import punctuation

def processTweet(tweet):
    '''
    parameters:
    ====================
    - tweets: list of text 
   
    functions:
    ====================
    - Remove HTML special entities (e.g. &)
    - Convert @username to AT_USER
    - Remove tickers
    - convert to lowercase
    - Remove hyperlinks
    - Remove hashtags
    - Remove Punctuation and split 's, 't, 've with a space for filter
    - Remove stopwords
    - Remove words with 2 or fewer letters
    - Remove whitespace (including new line characters)
    - Remove single space remaining at the front of the tweet.
    
    '''
    # Remove HTML special entities (e.g. &)
    tweet = re.sub(r'\&\w*;', '', tweet)
    #Convert @username to AT_USER
    tweet = re.sub('@[^\s]+','',tweet)
    # Remove tickers
    tweet = re.sub(r'\$\w*', '', tweet)
    # To lowercase
    tweet = tweet.lower()
    # Remove hyperlinks
    tweet = re.sub(r'https?:\/\/.*\/\w*', '', tweet)
    # Remove hashtags
    tweet = re.sub(r'#\w*', '', tweet)
    # Remove Punctuation and split 's, 't, 've with a space for filter
    tweet = re.sub(r'[' + punctuation.replace('@', '') + ']+', ' ', tweet)
    # Remove Punctuation and ellipses like 3 dots at the end of the line.
    tweet = tweet.replace('\u2026','')
    # Remove stopwords
    tweet = [word for word in tweet.lower().split() if word.lower() not in stopwords.words("english")]
    tweet = " ".join(tweet)
    # Remove words with 2 or fewer letters
    tweet = re.sub(r'\b\w{1,2}\b', '', tweet)
    # Remove whitespace (including new line characters)
    tweet = re.sub(r'\s\s+', ' ', tweet)
    # Remove single space remaining at the front of the tweet.
    tweet = tweet.lstrip(' ') 
    return tweet
...