Life and Tech Talk

[파이썬] 빈출 단어 찾기 코드
import pandas as pd
import re
from tqdm import tqdm

def preprocess_text(texts):
    stopword = ['은','는','이','가','하','아','것','들','의','있','되','수','보','주','등','한']
    texts_cleaning = []
    for text in tqdm(texts):
        text_cleaning = re.sub('[^가-힣ㄱ-ㅎㅏ-ㅣ\\s]', '', text)
        # 공백 문자 기준으로 토큰화
        text_token = text_cleaning.split()
        text_token_stop = [token for token in text_token if not token in stopword]
        texts_cleaning.append(text_token_stop)
    return texts_cleaning

def top_words(texts):
    train_texts = preprocess_text(texts)
    print('불용어 처리 후 :\n', train_texts[0])    
    # 전체 데이터셋에서 각 단어가 등장하는 횟수를 구합니다.
    word_cnt = {}
    for text in train_texts:
        for token in text:
            word_cnt.setdefault(token, 0)
            word_cnt[token] += 1
    # 10회 이상 등장한 단어만 솎아냅니다.
    filtered_word_cnt={k: v for k, v in word_cnt.items() if v >= 2}
    filtered_word_cnt=dict(sorted(filtered_word_cnt.items(), key=lambda x: x[1], reverse=True))
    print("filtered_word_cnt: ", filtered_word_cnt)
    keywords=list(filtered_word_cnt.keys())
    return keywords
올린날: 2024년 1월 14일
주제: 코드 관련 포스팅 클릭!!

* 올린이: 마이클

* VIEW: 24       0           위키홈     게시판     수정