Ich übe mich auf NLTK zu verwenden, um bestimmte Features aus rohen Tweets zu entfernen und in der Hoffnung, Tweets zu entfernen, die (irrelevant) sind (z. B. leere Tweets oder einzelne Wort Tweets). Es scheint jedoch, dass einige der einzelnen Wort Tweets nicht entfernt werden. Ich habe auch ein Problem damit, dass ich keine Stoppwörter entfernen kann, die entweder am Anfang oder am Ende eines Satzes stehen.Text Vorverarbeitung mit NLTK
Irgendwelche Ratschläge? Im Moment hoffe ich, einen Satz als eine Ausgabe statt als eine Liste von in Token zerlegten Wörtern zurück zu geben.
Jeder andere Kommentar zur Verbesserung des Codes (Bearbeitungszeit, Eleganz) sind willkommen.
import string
import numpy as np
import nltk
from nltk.corpus import stopwords
cache_english_stopwords=stopwords.words('english')
cache_en_tweet_stopwords=stopwords.words('english_tweet')
# For clarity, df is a pandas dataframe with a column['text'] together with other headers.
def tweet_clean(df):
temp_df = df.copy()
# Remove hyperlinks
temp_df.loc[:, "text"] = temp_df.loc[:, "text"].replace('https?:\/\/.*\/\w*', '', regex=True)
# Remove hashtags
# temp_df.loc[:,"text"]=temp_df.loc[:,"text"].replace('#\w*', '', regex=True)
temp_df.loc[:, "text"] = temp_df.loc[:, "text"].replace('#', ' ', regex=True)
# Remove citations
temp_df.loc[:, "text"] = temp_df.loc[:, "text"].replace('\@\w*', '', regex=True)
# Remove tickers
temp_df.loc[:, "text"] = temp_df.loc[:, "text"].replace('\$\w*', '', regex=True)
# Remove punctuation
temp_df.loc[:, "text"] = temp_df.loc[:, "text"].replace('[' + string.punctuation + ']+', '', regex=True)
# Remove stopwords
for tweet in temp_df.loc[:,"text"]:
tweet_tokenized=nltk.word_tokenize(tweet)
for w in tweet_tokenized:
if (w.lower() in cache_english_stopwords) | (w.lower() in cache_en_tweet_stopwords):
temp_df.loc[:, "text"] = temp_df.loc[:, "text"].replace('[\W*\s?\n?]'+w+'[\W*\s?]', ' ', regex=True)
#print("w in stopword")
# Remove quotes
temp_df.loc[:, "text"] = temp_df.loc[:, "text"].replace('\&*[amp]*\;|gt+', '', regex=True)
# Remove RT
temp_df.loc[:, "text"] = temp_df.loc[:, "text"].replace('\s+rt\s+', '', regex=True)
# Remove linebreak, tab, return
temp_df.loc[:, "text"] = temp_df.loc[:, "text"].replace('[\n\t\r]+', ' ', regex=True)
# Remove via with blank
temp_df.loc[:, "text"] = temp_df.loc[:, "text"].replace('via+\s', '', regex=True)
# Remove multiple whitespace
temp_df.loc[:, "text"] = temp_df.loc[:, "text"].replace('\s+\s+', ' ', regex=True)
# Remove single word sentence
for tweet_sw in temp_df.loc[:, "text"]:
tweet_sw_tokenized = nltk.word_tokenize(tweet_sw)
if len(tweet_sw_tokenized) <= 1:
temp_df.loc["text"] = np.nan
# Remove empty rows
temp_df.loc[(temp_df["text"] == '') | (temp_df['text'] == ' ')] = np.nan
temp_df = temp_df.dropna()
return temp_df
Bitte bearbeiten ** Ihre Frage ** zu erklären, dass ' df' ist ein Pandas Datenrahmen (wie ich aus deinen Kommentaren erfahren habe). Idealerweise sollten Sie ein paar Codezeilen hinzufügen, damit ein vollständiges Snippet erstellt wird, das ausgeführt werden kann, wenn Ihnen jemand eine bessere Antwort geben möchte. Und wenn Sie entscheiden, dass Ihre eigene Selbst-Antwort Ihr Problem gelöst hat, sollten Sie es schließlich als "akzeptiert" markieren. (Aber zuerst würde ich deine Frage klären und auf bessere Antworten warten). – alexis