Я пытаюсь обработать большой текстовый файл, используя NLTK и регулярное выражение. После нескольких кодов я обнаружил, что между некоторыми словами отсутствует пробел. Ниже приведены мои коды и примеры этих слов, сгруппированных вместе из-за отсутствующего пробела. Я был бы очень признателен, если бы вы помогли мне понять, какая часть кода повторного вызова вызывает проблему.
Данные, с которых я начал:
news 99 ¢ Only Stores_6-9- 2008 99 ¢ Только магазины (r) Выпускает предупредительные отзывы ... APP Pharmaceuticals_3-26-2011 APP Pharmaceuticals выпускает общенациональный объем ... Abbott Diabetes Care_12-22-2010 Abbott Diabetes Care объявляет об отзыве Certa ... Abbott Nutrition_1-19 -2009 Abbott Nutrition объявляет о добровольном отзыве ... Abbott_5-25-2007 Abbott объявляет о добровольном общенациональном отзыве o ...
import pandas as pd
pd.set_option('max_colwidth',50)
data_df = pd.DataFrame.from_dict(data_combined).transpose()
data_df.columns = ['news']
data_df = data_df.sort_index()
def clean_text_round1 (text):
text=text.split()
re_punc = re.compile('[%s]' % re.escape(string.punctuation))
text = [re_punc.sub('', w) for w in text]
text = [word for word in text if word.isalpha()]
#lem = WordNetLemmatizer()
#text = [lem.lemmatize(word) for word in text]
porter = PorterStemmer()
text = [porter.stem(word) for word in text]
text = [word for word in text if len(word) > 3]
return text
round1 = lambda x: clean_text_round1(x)
data_clean = pd.DataFrame(data_df.news.apply(round1))
def clean_text_round2(data_clean):
data_clean = re.sub('^[A-Z]\w*', '', data_clean)
data_clean = re.sub('[%s]' % re.escape(string.punctuation), '', data_clean)
data_clean = re.sub('[‘’“”…]', '', data_clean)
data_clean = re.sub('(\n|¢)', '', data_clean)
data_clean = re.sub('\[.*?\]', '', data_clean)
data_clean = re.sub('\w*\d\w*', '', data_clean)
data_clean = data_clean.lower().strip()
return data_clean
round2 = lambda x: clean_text_round2(x)
data_clean = pd.DataFrame(data_df.news.apply(round2))
Некоторые примеры неправильного вывода:
abbottabbott, abbottdocument , alexandraspmcommunicationscom, americacustomers