현재 저장되어있는 기본 데이터는 아래와 같다
우선 이 데이터 테이블에 칼럼을 하나씩 추가하며 전처리를 한다
clean_text로 영어가 아닌 글을 삭제하는 함수를 만들어서 적용하고, nltk의 word_tokenize도 적용한다
def clean_tweet(tweet):
'''
Utility function to clean tweet text by removing links, special characters
using simple regex statements.
'''
tweet = str(tweet).lower()
return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \\t])|(\\w+:\\/\\/\\S+)", " ", tweet).split())
query_df['clean_text'] = query_df['text'].apply(clean_tweet)
query_df['tokenized'] = query_df['clean_text'].apply(word_tokenize)
query_df = query_df[~query_df['key'].isnull()]
# s3에 데이터를 저장하며 형태가 바뀔 수 있다. int로 변환해준다
query_df['author_id'] = query_df['author_id'].astype(int)
query_df['id'] = query_df['id'].astype(int)
아래와 같은 테이블이 완성된다
이 단어들을 순서도 살려서 직접 사용하면 더 좋은 모델을 만들 수 있겠지만, 학습의 난이도를 위해 간단한 word count만 저장한다
더불어 author_id에 새로 글쓴 사람들의 수도 분석요소로 사용할 수 있을 것 같아 따로 저장한다
cnt_df_storage = []
author_df_storage = []
keys = set(query_df['key'])
for key in keys:
tdf = query_df[query_df['key']==key]
words = list(tdf['tokenized'])
flat_words = [num for sublist in words for num in sublist]
cnter = Counter(flat_words)
cnt_df = pd.DataFrame.from_dict(cnter, orient='index').reset_index()
cnt_df.columns=['word', 'count']
cnt_df['key']=key
cnt_df['date']=date
cnt_df_storage.append(cnt_df)
author_cnter = Counter(tdf['author_id'])
author_cnt_df = pd.DataFrame.from_dict(author_cnter, orient='index').reset_index()
author_cnt_df.columns=['author_id', 'cnt']
author_cnt_df['key']=key
author_cnt_df['date']=date
author_df_storage.append(author_cnt_df)
wc_df = pd.concat(cnt_df_storage)
author_df = pd.concat(author_df_storage)
아래와 같은 두개의 테이블이 만들어진다
우선, 여기까지 생성된 데이터를 premart 경로로 저장한다