Esempio n. 1
0
    def generate_from_text(self, text):
        """Generate wordcloud from text.

        The input "text" is expected to be a natural text. If you pass a sorted
        list of words, words will appear in your output twice. To remove this
        duplication, set ``collocations=False``.

        Calls process_text and generate_from_frequencies.

        ..versionchanged:: 1.2.2
            Argument of generate_from_frequencies() is not return of
            process_text() any more.

        Returns
        -------
        self
        """

        if self.korean:
            try:
                from eunjeon import Mecab
            except ImportError:
                raise Exception("Please install pyeunjeon properly. https://github.com/koshort/pyeunjeon")
            tagger = Mecab()
            words = Counter(tagger.nouns(text))
        else:
            words = self.process_text(text)

        self.generate_from_frequencies(words)
        return self
Esempio n. 2
0
def mecabFreqToDataFrame(text):
    mecab = Mecab()

    # kkma.nouns(text)라는 함수가 있지만 결과가 좋지 못하므로 조건을 달리함
    nouns = mecab.nouns(text)

    # 각 명사의 숫자 세기
    count = Counter(nouns)
    tag_count = []
    tags = []

    # 가장 많이 카운팅된 명사를 차례로 tags와 tag_count리스트에 추가
    for n, c in count.most_common(100):
        dics = {'tag': n, 'count': c}
        # 글자 수 조건 2~49자
        if len(dics['tag']) >= 2 and len(tags) <= 49:
            tag_count.append(dics['count'])
            tags.append(dics['tag'])

    # 어떤 텍스트가 형태소 분리됬는지 디버깅
    joined_text = " ".join(tags)
    print("형태소 : ", joined_text)
    print("언어 감지됨 : ", detect_langs(joined_text))

    # 본문 토큰화 시킨 것을 데이터프레임으로 변환
    return pd.DataFrame({"Word": list(tags), "Frequency": list(tag_count)})
def dataPrePrcs(contents):
    no_kor_num = 0
    logger = __get_logger()

    try:
        logger.info('mecab 형태소 분석기를 실행합니다.')
        tagger = Mecab()
    except Exception as e:
        trace_back = traceback.format_exc()
        message = str(e)  #+"\n"+ str(trace_back)
        logger.error('mecab형태소 분석기 실행에 실패하였습니다. %s', message)
        #sys.exit()

    try:
        logger.info('한글 외의 글자를 삭제합니다.')
        hangul = re.compile('[^ ㄱ-ㅣ가-힣]+')
        for j in range(len(contents)):
            if re.match('[^ ㄱ-ㅣ가-힣]+', str(contents[j])):
                no_kor_num += 1
        contents = [
            hangul.sub('', str(contents[cn])) for cn in range(len(contents))
        ]
        logger.info('한글 외의 글자를 가진', no_kor_num, '개의 문서 삭제를 완료했습니다.')
    except Exception as e:
        trace_back = traceback.format_exc()
        message = str(e)  #+"\n"+ str(trace_back)
        logger.error('한글 외의 글자 삭제에 실패하였습니다.. %s', message)
        #sys.exit()

    try:
        logger.info('각 문서의 명사를 추출합니다.')
        tokenized_doc = []
        for cnt in tqdm(range(len(contents))):
            nouns = tagger.nouns(contents[cnt])
            tokenized_doc.append(nouns)
        logger.info('각 문서의 명사추출을 완료했습니다.')
    except Exception as e:
        trace_back = traceback.format_exc()
        message = str(e)  #+"\n"+ str(trace_back)
        logger.error('각 문서의 명사를 추출에 실패하였습니다.. %s', message)
        #sys.exit()

    # 한글자 단어들 지우기!
    try:
        logger.info('한 글자 단어를 삭제합니다.')
        num_doc = len(tokenized_doc)
        one_word = 0

        for i in range(num_doc):
            tokenized_doc[i] = [
                word for word in tokenized_doc[i] if len(word) > 1
            ]

        logger.info("한 글자 단어를 삭제를 완료했습니다.")
    except Exception as e:
        trace_back = traceback.format_exc()
        message = str(e)  #+"\n"+ str(trace_back)
        logger.error('한 글자 단어를 삭제에 실패하였습니다.. %s', message)
        #sys.exit()
    return tokenized_doc
Esempio n. 4
0
def ajax_news_analysis(request):
    news_data = json.loads(request.POST.get('news_data', ''))
    mecab = Mecab()

    def sentiment_predict(new_sentence):
        max_len = 30
        stopwords = [
            '의', '가', '이', '은', '들', '는', '좀', '잘', '걍', '과', '도', '를', '으로',
            '자', '에', '와', '한', '하다'
        ]
        new_sentence = re.sub(r'[^ㄱ-ㅎㅏ-ㅣ가-힣 ]', '', new_sentence)
        new_sentence = mecab.morphs(new_sentence)  # 토큰화
        new_sentence = [
            word for word in new_sentence if not word in stopwords
        ]  # 불용어 제거
        #tokenizer = Tokenizer()
        tokenizer.fit_on_texts([new_sentence])
        encoded = tokenizer.texts_to_sequences([new_sentence])  # 정수 인코딩
        pad_new = pad_sequences(encoded, maxlen=max_len)  # 패딩
        score = float(loaded_model.predict(pad_new))  # 예측
        return score * 100

    score = []
    words_list = []
    for i in range(len(news_data['items'])):
        title = news_data['items'][i]['title']
        words_list.extend(mecab.nouns(title))
        s = sentiment_predict(title)
        if s > 10 and s < 90:
            score.append(s)
    score_avg = sum(score) / len(score)
    counter = collections.Counter(words_list)
    data = {'LSTM_sent': score_avg, 'words_list': counter.most_common(30)}
    return JsonResponse(data, safe=False)
Esempio n. 5
0
 def nounExt(self, sentlist):
     m = Mecab()
     # m = Mecab(dicpath='C:/mecab/mecab-ko-dic') # (사용불가능, 비활성)
     # m = Mecab(dicpath='/usr/local/lib/mecab/dic/mecab-ko-dic') # (사용불가능, 비활성)
     out = []
     for i in sentlist:
         out.append(m.nouns(i))
     return out
Esempio n. 6
0
 def lematization(self, texts):  #['NOUN', 'ADJ', 'VERB', 'ADV']
     print(' ...Make lematization...')
     texts_out = []
     mecab = Mecab()
     for sent in tqdm(texts):
         sent = ' '.join(sent)
         sent = mecab.nouns(sent)
         texts_out.append(sent)
     return texts_out
Esempio n. 7
0
 def go(self, url=gurl, depth=0, count_search=0):
     print(url, 'depth:', depth)
     reurl = req.Request(url, data=None, headers=self.gheader)
     f = req.urlopen(reurl)
     text = f.read().decode('utf-8')
     soup = BeautifulSoup(text, 'html.parser')
     refs = soup.find_all(href=re.compile("news/news_view"))
     try:
         tagger = Mecab()
         res = soup.select_one('div.sub_view_cont')
         ptext = res.get_text()
         lltext = tagger.nouns(ptext)
         print(tagger.nouns(ptext))
         for i in lltext:
             try:
                 self.dict_text[i] += 1
             except:
                 self.dict_text[i] = 1
     except AttributeError:
         pass
     if (depth >= self.MAX_DEPTH): return count_search
     for r in refs:
         suburl = r['href']
         result = parse.urlparse(suburl)
         result2 = parse.parse_qs(result.query)
         theval = False
         try:
             theval = self.visited[result2['artice_id'][0]]
         except KeyError:
             pass
         if not theval:
             self.visited[result2['artice_id'][0]] = True
             if 'http' in suburl:
                 count_search += self.go(suburl, depth + 1)
             else:
                 count_search += self.go(self.gurl + suburl, depth + 1)
             count_search += 1
     return count_search
Esempio n. 8
0
def textRank():
    import json
    from gensim.summarization import keywords
    DIR_FE = "../Front_KUBIC/src/assets/special_first/ctgRNNResult.json"

    with open(DIF_FE, 'r', encoding='utf-8') as fp:
        data = json.load(fp)

    corpus = data[0]["doc"]

    isTokened = True

    if isTokened == True:
        tokenized_doc = []
        for doc in corpus:
            tokenized_doc.append(doc["words"])
    else:
        contents = []
        for doc in corpus:
            contents.append(doc["contents"])

        tagger = Mecab()
        print("데이터 전처리 중... It may takes few hours...")
        tokenized_doc = [
            tagger.nouns(contents[cnt]) for cnt in range(len(contents))
        ]

    # texts = """
    # □ 평화번영을 위한 남북관계 발전방안「6.15공동선언」 이후 남북관계는 빠른 속도로 발전하고 있습니다. 그러나 장기간에 걸친 분단으로 인해 냉전적 사고와 논리, 불신이 남과 북의 관계 진전에 발목을 잡고 있는 것도 사실입니다.공존공영의 남북관계를 정립하기 위해서는 무엇보다 상호신뢰를 쌓기 위한 노력과, 이를 뒷받침할 수 있는 통일교육 지원 등의 국내적 기반 조성도 필요합니다.우리 민주평화통일자문회의 위원들은 한반도의 평화번영을 위한 남북관계의 발전을 위해 다음의 건의를 드립니다.첫 째, 남과 북이 서로 만날 수 있는 기회를 많이 만들어내야 합니다.대구 유니버시아드대회를 통해 우리는 북한 주민들이 우리와 얼마나 다른 사고와 행동을 하는지 생생히 보았습니다.남과 북이 서로를 이해하고 화합하기 위해서는 무엇보다 먼저 많이 만나는 것이 필요합니다.둘 째, 지방자치단체와 시민사회단체 등의 남북교류가 활성화되어야 합니다.남북한 관계가 탄탄한 뿌리를 내리기 위해서는 남과 북을 잇는 다양한 네트워크가 필요합니다. 먼저 지방자치단체간 자매결연을 통한 협력체제를 구축하고, 시민사회단체간에도 연대를 맺는 활동이 필요합니다.이들 상호간의 남북교류는 안정적인 남북관계를 뒷받침하는 든든한 버팀목이 될 수 있을 것입니다.셋 째, 인도주의적 대북지원이 적극적으로 확대되어야 합니다.인도주의 정신에 입각한 대북지원은 정치적 논리를 초월하여 실천해야 할 매우 중요한 과제입니다. 일회성 지원이나 이벤트성 행사보다는 지속적인 지원이 절실히 필요합니다.범국민적 차원에서 폭넓게 펼칠 수 있는 북한에 나무심기 운동, 북한 어린이 돕기운동 등이 좋은 예가 될 수 있을 것입니다.넷 째, 제2차 남북정상회담이 빠른 시일 내에 열리기를 기대합니다.북핵문제 해결 등 한반도의 평화체제 구축을 위한 역사적 전기 마련이 필요한 시점입니다.제2차 정상회담을 통해 보다 밝은 남북관계를 재정립하고, 동북아의 안정에도 기여할 수 있을 것입니다.□ 국민통합과 한민족 통일역량 결집방안한반도에서의 평화와 번영의 성취는 동북아뿐만 아니라 전세계의 평화와 번영에도 긴요하다는 점에서 하루빨리 이루어야 할 민족적 과제입니다. 따라서 7천만 국내외 동포의 통일역량의 결집은 장차 한반도 통일에 결정적 담보가 될 것 입니다.해외동포는 통일을 위한 가교의 역할로, 남북한 주민은 서로를 이해하는 넓은 아량으로 그리고 우리 사회 내부는 겨레가 함께 살아갈 수 있는 의지를 모아 새로운 시대에 새로운 한민족 공동체를 창조하는 데 적극적으로 참여하여야 합니다.우리 민주평화통일자문회의 위원들은 국민대통합과 한민족의 통일역량 결집을 위해 다음의 건의를 드립니다.첫 째, 분단의 결과로 생긴 민족내부의 차이와 갈등을 폭넓게 수용하는 정책이 필요합니다.냉전적 사고에 사로잡힌 불신과 대결로는 민족의 통일역량을 결집할 수 없습니다. 다양한 의견과 차이를 인정하고 이를 공존의 기초로 받아들이는 포용의 자세가 필요합니다.국내는 물론 현지사회에 동화된 한민족의 일원도 차이와 갈등을 넓게 아우르는 민족대통합의 정책이 필요합니다.둘 째, 한민족 경제·문화 공동체를 만들어 민족상생의 시대를 열어야 합니다.세계화 시대에 각 나라에 거주하는 해외동포들이 경제·사회적으로 주류에 서서 한민족의 정체성을 유지할 수 있도록, 그리고 현지 국민들과 융화하면서 자긍심을 유지·발전시킬 수 있도록 지원해야 합니다.한민족 경제·문화공동체를 형성하기 위해 현지 한국어 교육의 강화, 청년세대의 상호 교류, 지역사회내의 다양한 네트워크 형성으로 한민족의 정체성을 강화하여야 합니다.또한 현지 국민들과의 융화를 위해 이미 설립되어 있거나 설립해야 할 「한국 문화센터」에 지원을 아끼지 말아야 합니다.셋 째, 민주평통 해외자문위원의 역할을 강화할 필요가 있습니다.동북아 중심국가로 성장하는 통일한국의 건설에는 국민대통합과 함께 해외동포의 역량결집이 대단히 중요합니다. 특히 한민족의 10% 이상인 7백만이 해외에 살고 있다는 점은 큰 자산이라 할 수 있습니다.해외동포의 통일역량을 효과적으로 결집하는 데 민주평통 해외위원의 참여와 역할이 대단히 중요한 비중을 차지하고 있습니다. 이를 위한 제도적 장치가 시급히 마련되어야 합니다.
    # """

    # with open("krWl.txt", "r" ,encoding='utf-8') as f:
    #     texts = f.read()
    # print(f.read())
    # tokenized_doc = tagger.nouns(texts)
    # tokenized_doc = ' '.join(tokenized_doc)
    # print("형태소 분석 이후 단어 토큰의 개수",len(tokenized_doc))

    # result = keywords(tokenized_doc, words = 15 , scores=True)
    result = keywords(tokenized_doc)

    # with open(DIR_FE, 'w', -1, "utf-8") as f:
    with open("./wrCul.json", 'w', -1, "utf-8") as f:
        json.dump(result, f, ensure_ascii=False)

    return json.dumps(result, ensure_ascii=False)
Esempio n. 9
0
def dataPrePrcs(contents):
    # 형태소 분석기 instance
    # okt = Okt()
    # tokenized_doc = [okt.nouns(contents[cnt]) for cnt in range(len(contents))]

    #mecab test
    tagger = Mecab()
    print("데이터 전처리 중... It may takes few hours...")
    tokenized_doc = [
        tagger.nouns(contents[cnt]) for cnt in range(len(contents))
    ]

    print("형태소 분석 완료!")
    print("투입된 문서의 수 : %d" % (NUM_DOC))
    showTime()

    # 한글자 단어들 지우기!
    num_doc = len(tokenized_doc)
    for i in range(num_doc):
        tokenized_doc[i] = [word for word in tokenized_doc[i] if len(word) > 1]

    print("데이터 전처리 완료!")
    return tokenized_doc
from eunjeon import Mecab

tagger = Mecab()

sentence = '아무문장이다.'
print(tagger.morphs(sentence))
print(tagger.nouns(sentence))

#아나콘다 환경
#python 3.6
#대소 비교가 가능한 숫자(뭐 ~km) 같은거 2차언 배열로 순위 매기기
#대소 비교 불가능한 건 0 으로 처리하고 1순위부터 시작해서 2차원 배열 생성

# '/usr/local/lib/mecab/dic/mecab-ko-dic'
Esempio n. 11
0
def overview_recommend(movie_title):

    from sklearn.feature_extraction.text import TfidfVectorizer
    from sklearn.metrics.pairwise import cosine_similarity
    from eunjeon import Mecab
    import pandas as pd
    import numpy as np
    import json
    import warnings
    warnings.filterwarnings('ignore')

    # 데이터 불러오기
    movies = pd.read_json("../../finalpjt/CBF/CBFmovies.json")

    # 사용 할 데이터만 저장
    movies_df = movies[['id', 'title', 'overview', 'popularity', 'vote_count',
                        'vote_average', 'release_date', 'poster_path', 'adult', 'genre_ids']]

    mecab = Mecab()

    # 명사 기반
    movies_df['overview_token'] = movies_df['overview'].apply(
        lambda x: mecab.nouns(x))

    # movies_df['overview_token'] = movies_df['overview'].apply(
    #     lambda x: x.split())
    # 형태소 기반

    # 줄거리가 없는 영화 제거

    movies_df['overview_literal'] = movies_df['overview_token'].apply(
        lambda x: (' ').join(x))

    # CountVectorizer로 학습시켰더니 4803개 영화에 대한 276개 장르의 '장르 매트릭스'가 생성되었다.
    # min_df: 단어장에 들어갈 최소빈도, ngram_range: 1 <= n <= 2
    count_vect = TfidfVectorizer(min_df=0, ngram_range=(1, 2))
    genre_mat = count_vect.fit_transform(movies_df['overview_literal'])

    # 코사인 유사도에 의해 4803개 영화 각각 유사한 영화들이 계산됨
    genre_sim = cosine_similarity(genre_mat, genre_mat)

    # 자료를 정렬하는 것이 아니라 순서만 알고 싶다면 argsort
    # 유사도가 높은 영화를 앞에서부터 순서대로 보여줌
    # 0번째 영화의 경우 유사도 순서 : 0번, 3494번, 813번, ..., 2401 순서
    genre_sim_sorted_ind = genre_sim.argsort()[:, ::-1]  # 전체를 -1칸 간격으로

    # 가중 평점
    # 상위 60%에 해당하는 vote_count를 최소 투표 횟수인 m으로 지정
    C = movies_df['vote_average'].mean()
    m = movies_df['vote_count'].quantile(0.6)

    # 가중 평점 계산

    def weighted_vote_average(record):
        v = record['vote_count']
        R = record['vote_average']

        return ((v/(v+m)) * R) + ((m/(m+v)) * C)

    # 기존 데이터에 가중평점 칼럼 추가
    movies_df['weighted_vote'] = movies_df.apply(weighted_vote_average, axis=1)

    def find_sim_movie_ver2(df, sorted_ind, title_name, top_n=10):
        title_movie = df[df['title'] == title_name]
        title_index = title_movie.index.values

        # top_n의 2배에 해당하는 쟝르 유사성이 높은 index 추출
        similar_indexes = sorted_ind[title_index, :(top_n*2)]
        similar_indexes = similar_indexes.reshape(-1)

        # 기준 영화 index는 제외
        similar_indexes = similar_indexes[similar_indexes != title_index]

        # top_n의 2배에 해당하는 후보군에서 weighted_vote 높은 순으로 top_n 만큼 추출
        # return df.iloc[similar_indexes].sort_values('weighted_vote', ascending=False)[:top_n][['title', 'overview', 'popularity', 'vote_count', 'vote_average', 'release_date', 'poster_path', 'genre_ids']].to_json(orient='records', indent=4, lines=True, force_ascii=False)
        return df.iloc[similar_indexes].sort_values('weighted_vote', ascending=False)[:top_n][['title', 'overview', 'popularity', 'vote_count', 'vote_average', 'release_date', 'poster_path', 'genre_ids']].to_json(orient='records', force_ascii=False)

    similar_movies = find_sim_movie_ver2(
        movies_df, genre_sim_sorted_ind, '{}'.format(movie_title), 3)

    parsed = json.loads(similar_movies)
    return parsed
Esempio n. 12
0
def make_X3input(input_list):
    if input_list:
        mecab = Mecab()
        result = str()
        for i in input_list.split():
            if len(i) > 1 and i.isalpha():
                i = i.replace('설령', '가사').replace('고려', '감안').replace(
                    '생각', '감안').replace('참작', '감안')
                i = i.replace('거치지', '걷히지').replace('걷잡을', '겉잡을').replace(
                    '매각', '경락').replace('매수인', '경락인')
                i = i.replace('마쳐졌다', '경료되었다').replace('돈', '금원').replace(
                    '금액', '금원').replace('이미', '기')
                i = i.replace('속여',
                              '기망하여').replace('빌미로',
                                              '기화로').replace('까다롭다', '까닭스럽다')
                i = i.replace('까다롭다',
                              '까탈스럽다').replace('난이도',
                                               '난도').replace('이해', '납득')
                i = i.replace('명세', '내역').replace('내비게이션', '네비게이션').replace(
                    '많은 금액', '다액').replace('많은 돈', '다액').replace('큰돈', '다액')
                i = i.replace('큰 액수', '다액').replace('해당', '당해').replace(
                    '넘겨', '도과하여').replace('경과한 것으로서', '도과한 것으로서')
                i = i.replace('튀통수치다', '뒤꼭지치다').replace('개인지도', '레슨').replace(
                    '렌터카', '렌트카').replace('맞히다', '맞추다')
                i = i.replace('실어내기', '반출하기').replace('발송하다', '발하다').replace(
                    '발표하다', '발하다').replace('함께',
                                           '병합하여').replace('보도블록', '보도블럭')
                i = i.replace('일반인', '보통인').replace(
                    '알 수 없는 방법으로',
                    '불상의 방법으로').replace('포함하다', '산입').replace('타당하다', '상당하다')
                i = i.replace('알맞다',
                              '상당하다').replace('잃게',
                                              '상실하게').replace('남아', '상존하고')
                i = i.replace('성립여부', '성부').replace('이른바', '소위').replace(
                    '받는', '송금받는').replace('수수한', '수수받은')
                i = i.replace('여러차례', '수회').replace('잠금장치', '시건장치').replace(
                    '잠긴 문', '시정된 문').replace('일정한 비율에 따라 나누어', '안분하여')
                i = i.replace('한지 그렇지 않은지',
                              '여부').replace('두려움을 느낀', '외포된').replace(
                                  '배서하여', '이서하여').replace('넘겨', '이첩')
                i = i.replace('한때', '일시적').replace(
                    '담보권 실행을 위한 경매', '임의경매실행').replace('인정', '자인').replace(
                        '옮겨', '전원하여').replace('저해',
                                              '조해').replace('존재 여부', '존부')
                i = i.replace('근거한', '터 잡은').replace('피시방', '피씨방').replace(
                    '반드시',
                    '필요적으로').replace('휴대전화',
                                     '휴대폰').replace('휴대전화',
                                                    '핸드폰').replace('부족', '흠결')
                i = i.replace('모자람', '흠결').replace('흠', '흠결').replace(
                    '빈집터', '나대지').replace('피고에게', '피고에 대하여').replace(
                        '쌓아놓다', '적치하다').replace('음주운전', '주취운전')

                if mecab.nouns(i):
                    result = result + " " + i if result else i

        max_features = 30000
        sequence_length = 256

        r_list = list()
        r_list.append(result)
        tokenizer = tf.keras.preprocessing.text.Tokenizer(
            num_words=max_features, split=' ', oov_token='<unw>')
        tokenizer.fit_on_texts(r_list)
        r_list = tokenizer.texts_to_sequences(r_list)
        r_list = tf.keras.preprocessing.sequence.pad_sequences(
            r_list, sequence_length)
        return r_list
    else:
        return []
Esempio n. 13
0
# Made by Mose Gu 'http://cpslab.skku.edu/people-Moses-Gu.php'
#this code is to extract nouns from text file

from konlpy.tag import Hannanum
from eunjeon import Mecab
hannanum = Hannanum()
mecab = Mecab()

filepath = ("./경로.확장자")  #directory of word or text file
f = open(filepath, 'r', encoding='utf-8')
rd = f.read()
y = mecab.nouns(rd)
# print(rd)

f = open("./경로2.확장자", 'w', encoding='utf-8')  #save path
frequency = {}
for i in y:
    print(i)

f.writelines(str(y))  #메모장에 콘엘피와이 단어다찍어내기
print(str(y))
Esempio n. 14
0
class Exam(QWidget, form_window):
    def __init__(self):
        """객체 초기화"""
        super().__init__()  # 부모의 생성자 실행
        self.path = None
        self.setupUi(self)
        self.set_font(self.InputText, "Yummo,나눔바른펜")
        self.InputText.textChanged.connect(self.init_textedit)
        # 그림자 생성
        self.set_shadow(self.InputText)
        self.set_shadow(self.Result)
        self.set_shadow(self.History)
        # 파일 로드
        self.load_pickles()

        # 위 사각형 클릭 시 textedit 초기화
        self.reset_1.clicked.connect(self.clear_text)
        self.reset_2.clicked.connect(self.clear_text)
        # 버튼 클릭 시 예측
        self.Predict.clicked.connect(self.predict)

    def set_shadow(self, object):
        """해당 객체에 그림자 생성"""
        shadow = QGraphicsDropShadowEffect()
        shadow.setOffset(QPointF(5.0, 6.0))
        shadow.setColor(QColor(155, 155, 155, 155))
        shadow.setBlurRadius(33)
        object.setGraphicsEffect(shadow)

    def init_textedit(self):
        self.InputText.setFontFamily("Yummo,나눔바른펜")
        self.InputText.setTextColor(QColor(52, 52, 52, 255))

    def load_pickles(self):
        """모델 및 예측에 필요한 파일들 로드"""
        # 모델 로드
        self.model = load_model("./models/books_classification_0.9008.h5")
        # 한글 분석기 로드
        self.mecab = Mecab()
        self.max = 612
        # one-hot encoder 로드
        with open(
                "./data/nouns_category_onehot_encoder.pickle",
                "rb",
        ) as f:
            encoder = pickle.load(f)
        self.category = encoder.categories_[0]
        self.counts = {cat: 0 for cat in self.category}
        # Tokenizer 로드
        with open("./data/nouns_token.pickle", "rb") as f:
            self.token = pickle.load(f)
        # stopword 로드
        self.stopwords = pd.read_csv("./data/stopwords.csv", index_col=0)

    def set_font(self, object, string):
        font = object.font()
        font.setFamily(string)
        print(type(font))
        object.setFont(font)

    def clear_text(self):
        """textedit 초기화"""
        self.InputText.clear()

    def delete_stopwords(self, lst):
        """해당 list의 stopword 제거한 string 반환"""
        words = [
            word for word in lst
            if word not in list(self.stopwords["stopword"]) and len(word) > 1
        ]
        return " ".join(words)

    def predict(self):
        """textedit의 글자를 통해 예측"""
        # textedit의 글자를 가져와 명사 추출
        target_raw = self.InputText.toPlainText()
        target_nouns = self.mecab.nouns(target_raw)
        n = self.delete_stopwords(target_nouns)
        nouns = []
        nouns.append(n)

        # 해당 글자 토큰화
        nouns_tokened = self.token.texts_to_sequences(nouns)
        nouns_pad = pad_sequences(nouns_tokened, self.max)

        # 결과 예측 및 출력
        predict = self.model.predict(nouns_pad)
        if predict.max() >= 0.5:
            target = self.category[np.argmax(predict)]
            self.counts[target] += 1
            result = f"It's book about {target}.\nThank you! The kids will be happy."
            self.Result.setText(result)
        else:
            self.Result.setText("Please give me a little more details")
            self.set_font(self.Result, "Yummo,나눔바른펜")
        history = {
            f"{key}": f"{self.counts[key]}"
            for key in self.counts if self.counts[key] > 0
        }
        print(history)
        self.History.setText("\n".join(history.keys()))
        self.HistoryNumber.setText("\n".join(history.values()))
Esempio n. 15
0
# Made by Mose Gu 'http://cpslab.skku.edu/people-Moses-Gu.php'
#this code is using after mecab.py
#this code is to extract frequency of nouns

from konlpy.tag import Hannanum
from eunjeon import Mecab
hannanum = Hannanum()
mecab = Mecab()
from collections import Counter

morphs = []

filepath = "./경로.확장자"  #open text or word file
f = open(filepath, 'r', encoding='utf-8')
lists = f.read()
y = mecab.nouns(lists)
# print(lists)

# 명사분리후 frequency 분석
count = Counter(y)
words = dict(count.most_common())
morphs.append(words)
print(morphs)

#frequency 분석후 다 찍어내기
f1 = open("./경로.확장자", 'w', encoding='utf-8')  #save file directory
for morpy in morphs:
    f1.write(str(morpy))
    f1.write('\n')
f1.write(morphs)
Esempio n. 16
0
    def __init__(self, inputPath, index=3, words=2, standard=0.3):

        # CSV, TXT 파일, 또는 기사 원문에서 복합단어를 추출
        # 파이썬 버전 3.6
        # 설치할 패키지: kss, eunjeon, pandas
        # 차후 eunjeon에서 konlpy로 이전 예정

        # 리눅스 환경 mecab-ko-dic 설치과정
        # wget -c https://bitbucket.org/eunjeon/mecab-ko-dic/downloads/최신버전-mecab-ko-dic.tar.gz
        # tar zxfv  최신버전-mecab-ko-dic.tar.gz
        # cd 최신버전-mecab-ko-dic
        # ./configure
        # make
        # make check
        # sudo make install
        # 위 과정을 거치면 /usr/local/lib/mecab/dic/mecab-ko-dic 경로에 mecab-ko-dic 설치

        # 입력변수
        # inputPath: CSV 또는 TXT 파일의 위치 (너무 길 경우 원문 스트링으로 인식하여 분석)
        # outputPath: 출력할 텍스트 파일의 위치
        # index: CSV 테이블에서 불러올 텍스트의 행 번호
        # words: 복합단어를 이루는 단어 갯수 (기본:2)
        # standards: 요구사항을 충족하는 TR+PMI 점수의 최소치 (임시:0.3)

        # inputPath가 길 경우 원문으로 인식
        if len(inputPath) > 50: self.data = inputPath
        else: self.data = self.extractText(inputPath)

        # 파일을 호출해서 행 번호(index)에 있는 값을 TXT에 저장
        txt = self.clean_str(self.readValue(self.data, index))

        # target = "문서전체내용"
        self.target = self.clean_str(txt)
        m = Mecab()
        # m = Mecab(dicpath='C:/mecab/mecab-ko-dic') # (사용불가능, 비활성)
        # m = Mecab(dicpath='/usr/local/lib/mecab/dic/mecab-ko-dic') # (사용불가능, 비활성)

        # wTotal = (int)문서 내 명사 갯수
        # fList = [["문장1명사1", "문장1명사2", ...], ["문장2명사1", "문장2명사2", ...], ...]
        # mList = ["문장1명사1", "문장1명사2", ..., "문장2명사1", "문장2명사2" ...] 중복 제거
        # lList = [["문장1형태소1", "문장1형태소2", ...], ["문장2형태소1", "문장2형태소2", ...], ...]
        self.wTotal = len(m.nouns(self.target))
        self.fList = self.nounExt(kss.split_sentences(self.target))
        self.mList = list(
            dict.fromkeys([item for sublist in self.fList
                           for item in sublist]))
        self.lList = []
        for i in kss.split_sentences(self.target):
            l = []
            for j in m.morphs(i):
                l.append(j)
            self.lList.append(l)

        # N그램 변수 (형태소의 갯수, 또는 문자수. 문자수 기반 N그램 현재 사용불가능)
        self.ngram = 8
        # 복합단어를 이룰 단어의 갯수
        self.nOfWords = words
        # 제동변수
        self.df = 0.85
        # 텍스트랭크 반복횟수 (임시:16)
        self.defIteration = 16

        # allCW = [["단어1", "단어2", ...], ["단어a", "단어b", ...], ...] 복합단어의 가능성이 있는 모든 명사 리스트의 리스트
        self.allCW = []
        for i in range(len(self.fList)):
            n = self.genCW(self.fList[i])
            for j in n:
                # 문서를 검색하는 방식
                # if self.complexSearch(j, self.target) > 1 and j not in self.allCW: # 띄어쓰기 경우의 수를 모두 검색 (사용가능, 비활성)
                if self.searchSpaceless(
                        j, self.target
                ) > 1 and j not in self.allCW:  # 본문 그대로 검색 (활성)
                    self.allCW.append(j)
        # 일부분 중복되는 복합단어를 탐지한 뒤 추가
        # self.allCW += self.detectRedundant(self.allCW) # (사용가능, 비활성)

        # trdic = {"단어1": TR1, "단어2": TR2, ...} (기존방식) (활성))
        self.trdic = self.calculateTROld(self.mList, self.fList,
                                         self.defIteration)

        # trdic = {"단어1": TR1, "단어2": TR2, ...} (N그램 방식) (사용가능, 비활성)
        # self.trdic = self.calculateTR(self.mList, self.lList, self.ngram, self.defIteration)

        # pmiList = [PMI1, PMI2, ...] allCW의 복합단어의 PMI 점수 리스트
        pmiList = []
        for i in self.allCW:
            pmiList.append(self.getPMI(i, self.wTotal, self.target))

        # trmpiList = [TRPMI1, TRPMI2, ...] 복합단어를 구성하는 TR의 기하평균 곱하기 복합단어의 PMI
        trpmiList = []
        for i in range(len(self.allCW)):
            k = self.allCW[i]
            key = 1
            for j in k:
                key *= self.trdic[j]
            key **= (1 / len(k))
            key *= pmiList[i]
            trpmiList.append(key)

        #gluedCW = ["복합단어1", "복합단어2", ...] allCW의 단어 구성 리스트를 합친 스트링 리스트
        gluedCW = []
        for i in self.allCW:
            gluedCW.append(''.join(i))

        # compDict = {"복합단어1": 1.11, "복합단어2": 2.22, ...}
        # 중복된 복합단어가 없는 경우
        if len(self.detectDuplicates(gluedCW)) == 0:
            self.compDict = dict(zip(gluedCW, trpmiList))
        # 중복된 복합단어가 있는 경우
        else:
            self.compDict = self.eliminateDuplicates(gluedCW, trpmiList)

        self.out = []
        for i in self.compDict.items():
            if i[1] > standard:
                self.out.append(i[0])
Esempio n. 17
0
# ['열심히', '코딩', '하', 'ㄴ', '당신', ',', '연휴', '에', '는', '여행', '을', '가보', '아요']
print(kkma.pos("열심히 코딩한 당신, 연휴에는 여행을 가봐요"))
# [('열심히','MAG'), ('코딩', 'NNG'), ('하', 'XSV'), ('ㄴ', 'ETD'), ('당신', 'NP'), (',', 'SP'), ('연휴', 'NNG'), ('에', 'JKM'), ('는', 'JX'), ('여행', 'NNG'), ('을', 'JKO'), ('가보', 'VV'), ('아요', 'EFN')]
print(kkma.nouns("열심히 코딩한 당신, 연휴에는 여행을 가봐요"))
# ['코딩', '당신', '연휴', '여행']
'''
한글 형태소 분석기 중에 가장 속도가 빠른 Mecab은 konlpy 엔진에 포함되어 있지 않다.
아래는 eunjeon 패키지를 이용하여 python에서 mecab을 활용하는 예시이다.
'''
from eunjeon import Mecab  # KoNLPy style mecab wrapper
tagger = Mecab()
print(tagger.morphs("열심히 코딩한 당신, 연휴에는 여행을 가봐요"))
# ['열심히', '코딩', '한', '당신', ',', '연휴', '에', '는', '여행', '을', '가', '봐요']
print(tagger.pos("열심히 코딩한 당신, 연휴에는 여행을 가봐요"))
# [('열심히', 'MAG'), ('코딩', 'NNG'), ('한', 'XSA+ETM'), ('당신', 'NP'), (',', 'SC'), ('연휴', 'NNG'), ('에', 'JKB'), ('는', 'JX'), ('여행', 'NNG'), ('을', 'JKO'), ('가', 'VV'), ('봐요', 'EC+VX+EC')]
print(tagger.nouns("열심히 코딩한 당신, 연휴에는 여행을 가봐요"))
# ['코딩', '당신', '연휴', '여행']
'''
2) 정제(Normalization) - https://wikidocs.net/21693
1. 규칙에 기반한 표기가 다른 단어들의 통합
2. 대, 소문자 통합
3. 정규 표현식(Regular Expression)
'''
'''
3) 어간 추출(Stemming) and 표제어 추출(Lemmatization) - https://wikidocs.net/21707
1. 표제어 추출(Lemmatization)
2. 어간 추출(Stemming)
'''

# WordNetLemmatizer
import nltk
# In[16]:

# 상위 빈도 100개 단어

top_words = Counter(tokenized_data)

top_words.most_common(100)[:10]

# ## 4.3 명사 추출

# In[17]:

nouns = []
for sentence in movie_data['new_text']:
    for noun in mecab.nouns(sentence):
        nouns.append(noun)

nouns[:10]

# In[18]:

# 상위 빈도 100개 명사

stop_words = '졸ㄹ ㅋㅋ 까지 다는 너무 네요 해서 아니 지만 재밌 는데 에서 였습니다 면서 그냥 으로 진짜 물론 워낙 수록 인지 용아 습니다 다시 입니다 ㅜ긴장감의 스러운 해도 졸라 근데 ㅜㅠ 나오 위한 세요 스러운 느꼈 겨를 드니 그래서 봐야 봐도 어서 하나 다기 안감 원다이해갖될듯 몰라도 아위 돌이 던데 이기 다면 볼지 해져서 단지 라고 이랑 대체 장리 솔직히 려고 여서 아닌 이거 다가 으면 해놨 언제 이게 이걸 이건 뭔가'
stop_words = stop_words.split(' ')

nouns = [word for word in nouns if word not in stop_words]
nouns = [word for word in nouns if len(word) >= 2]

top_nouns_not_stopword = Counter(nouns)
class Predict:
    def __init__(self,
                 pred_config: Pred_config,
                 task_id=None,
                 keyword=None,
                 channel=None):
        self.pred_config = pred_config
        self.engine = create_engine(
            ("mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8mb4").format(
                'root', 'robot369', '10.96.5.179', 3306, 'datacast2'))
        self.args = self.pred_config.get_args()

        ##쿠다, cpu 중 사용할 디바이스 설정
        self.device = self.pred_config.get_device()

        ##배치사이즈 설정(모델이 한번에 처리할 input 사이즈 크기)
        self.batch_size = self.pred_config.batch_size

        ##모델 가져오기
        self.model = self.pred_config.load_model(self.args, self.device)

        ##토크나이저 가져오기
        self.tokenizer = self.pred_config.load_tokenizer()
        self.nlp = Mecab()
        self.task_id = task_id
        self.keyword = keyword
        self.channel = channel

    def verbs(self, phrase):
        """Verbs extractor."""
        verbs = ['VV']
        tagged = self.nlp.pos(phrase)
        return [s for s, t in tagged if t in verbs]

    def adjs(self, phrase):
        """Adjs extractor."""
        adjs = ['VA', 'IC']
        tagged = self.nlp.pos(phrase)
        return [s for s, t in tagged if t in adjs]

    def read(self):
        # conn = pymysql.connect(host='1.221.75.76', user='******', password='******', database='datacast')
        # curs = conn.cursor(pymysql.cursors.DictCursor)
        # sql_select_sentence = 'select * from analysis_sentence'
        # curs.execute(sql_select_sentence)
        # rows = curs.fetchall()
        ##pandas datatable 형태로 sentece 테이블 읽어들이기

        print(
            'sql:',
            "SELECT ct.task_id,ct.channel,cc.contents_id,cc.text,cc.url from crawl_task as ct join crawl_contents as cc on ct.task_id=cc.task_id WHERE ct.task_id=%s and ct.keyword=\'%s\'"
            % (self.task_id, self.keyword))
        df_sentence_rows = pd.read_sql(
            "SELECT ct.task_id,ct.channel,cc.contents_id,cc.text,cc.url from crawl_task as ct join crawl_contents as cc on ct.task_id=cc.task_id WHERE ct.task_id=%s and ct.keyword=\'%s\'"
            % (self.task_id, self.keyword), self.engine)
        df_sentence_rows = df_sentence_rows[df_sentence_rows['text'].apply(
            lambda x: len(x) < 5000)]
        print('read finish')
        return df_sentence_rows

    def convert_input_sentence_to_tensor_dataset(self,
                                                 df_sentence_rows,
                                                 cls_token_segment_id=0,
                                                 pad_token_segment_id=0,
                                                 sequence_a_segment_id=0,
                                                 mask_padding_with_zero=True):
        tokenizer = self.tokenizer
        args = self.args

        cls_token = tokenizer.cls_token
        sep_token = tokenizer.sep_token
        pad_token_id = tokenizer.pad_token_id

        all_input_ids = []
        all_attention_mask = []
        all_token_type_ids = []

        ###input file 읽어들이기
        ###input file 읽어서 tensordata type 으로 변환
        for index in df_sentence_rows.index:
            sentence = df_sentence_rows.at[index, 'text']

            tokens = tokenizer.tokenize(sentence)

            # Account for [CLS] and [SEP]
            special_tokens_count = 2
            #문장의 최대길이 보다 큰 문장에 대해서 길이 조정을 해준다.
            if len(tokens) > args.max_seq_len - special_tokens_count:
                tokens = tokens[:(args.max_seq_len - special_tokens_count)]

            # Add [SEP] token
            tokens += [sep_token]
            token_type_ids = [sequence_a_segment_id] * len(tokens)

            # Add [CLS] token
            tokens = [cls_token] + tokens
            token_type_ids = [cls_token_segment_id] + token_type_ids
            input_ids = tokenizer.convert_tokens_to_ids(tokens)

            # The mask has 1 real tokens and 0 for padding tokens. Only real tokens are attended to.
            attention_mask = [1 if mask_padding_with_zero else 0
                              ] * len(input_ids)
            # Zero-pad up to the sequence length.
            padding_length = args.max_seq_len - len(input_ids)
            input_ids = input_ids + ([pad_token_id] * padding_length)
            attention_mask = attention_mask + (
                [0 if mask_padding_with_zero else 1] * padding_length)
            token_type_ids = token_type_ids + ([pad_token_segment_id] *
                                               padding_length)

            all_input_ids.append(input_ids)
            all_attention_mask.append(attention_mask)
            all_token_type_ids.append(token_type_ids)

        # Change to Tensor
        all_input_ids = torch.tensor(all_input_ids, dtype=torch.long)
        all_attention_mask = torch.tensor(all_attention_mask, dtype=torch.long)
        all_token_type_ids = torch.tensor(all_token_type_ids, dtype=torch.long)
        dataset = TensorDataset(all_input_ids, all_attention_mask,
                                all_token_type_ids)
        return dataset

    def predict(self):
        ##tuning 시 파라미터 정보가 들어있는 파일(training_args.bin)
        args = self.args

        ##쿠다, cpu 중 사용할 디바이스 설정
        device = self.device

        ##배치사이즈 설정(모델이 한번에 처리할 input 사이즈 크기)
        batch_size = self.batch_size

        ##모델 가져오기
        model = self.model
        logger.info(args)

        ##감성분석할 데이터 가져오기
        chunck_size = 100
        df_cdata_rows = self.read()
        for idx, i in enumerate(
                tqdm(range(0, len(df_cdata_rows), chunck_size),
                     desc="Nlp&Prediction")):
            try:
                df_cdata_chuncked_rows = df_cdata_rows[idx * chunck_size +
                                                       1:(idx + 1) *
                                                       chunck_size]
                ## crawl_contents 를 sentence 로 쪼개고 crawl_sentence 에 넣는 작업
                df_sentence_rows = pd.DataFrame()
                for index in df_cdata_chuncked_rows.index:
                    df_sentence_row = pd.DataFrame()
                    crawl_contents_id = df_cdata_chuncked_rows.at[
                        index, 'contents_id']
                    text = df_cdata_chuncked_rows.at[index, 'text']
                    sentences = kss.split_sentences(text)
                    seq = [i for i in range(0, len(sentences))]
                    df_sentence_row['contents_id'] = [crawl_contents_id
                                                      ] * len(sentences)
                    df_sentence_row['text'] = sentences
                    df_sentence_row['seq'] = seq
                    df_sentence_rows = df_sentence_rows.append(
                        df_sentence_row, ignore_index=True)
                # chunk_size = 10000
                # list_df_sentence_rows = [df_sentence_rows[i:i+chunk_size] for i in range(0,df_sentence_rows.shape[0],chunk_size)]
                # for df_sentence_rows_to_read in list_df_sentence_rows:
                ##모델이 읽을 수 있도록 데이터 형변환(to TensorDataset)
                dataset = self.convert_input_sentence_to_tensor_dataset(
                    df_sentence_rows)

                # dataset 을 model 을 이용하여 output 도출
                # Predict
                sampler = SequentialSampler(dataset)
                data_loader = DataLoader(dataset,
                                         sampler=sampler,
                                         batch_size=batch_size)
                preds = None
                probs = None
                print(type(data_loader), len(data_loader))
                for index, batch in enumerate(data_loader):
                    batch = tuple(t.to(device) for t in batch)
                    with torch.no_grad():
                        inputs = {
                            "input_ids": batch[0],
                            "attention_mask": batch[1],
                            "labels": None
                        }
                        if args.model_type != "distilkobert":
                            inputs["token_type_ids"] = batch[2]
                        outputs = model(**inputs)
                        logits = outputs[0]

                        if preds is None:
                            preds = logits.detach().cpu().numpy()
                            probs = np.exp(logits.detach().cpu().numpy()) / (
                                1 + np.exp(logits.detach().cpu().numpy()))
                        else:
                            preds = np.append(preds,
                                              logits.detach().cpu().numpy(),
                                              axis=0)
                            probs = np.append(
                                probs,
                                np.exp(logits.detach().cpu().numpy()) /
                                (1 + np.exp(logits.detach().cpu().numpy())),
                                axis=0)
                print(len(preds), len(probs))
                preds = np.argmax(preds, axis=1).tolist()
                prob_max_index = np.argmax(probs, axis=-1)
                maximum_probs = probs[np.arange(probs.shape[0]),
                                      prob_max_index]
                # maximum_probs = maximum_probs.tolist()
                # maximum_probs = list([round(maximum_prob,2) if pred==1 else round(maximum_prob,2)*(-1) for pred,maximum_prob in zip(preds,maximum_probs)])
                for idx in df_sentence_rows.index:
                    sentence = df_sentence_rows.at[idx, 'text']
                    nouns = list(set(self.nlp.nouns(sentence)))
                    nouns = json.dumps(nouns, ensure_ascii=False)

                    verbs = list(set(self.verbs(sentence)))
                    verbs = json.dumps(verbs, ensure_ascii=False)

                    adjs = list(set(self.adjs(sentence)))
                    adjs = json.dumps(adjs, ensure_ascii=False)

                    df_sentence_rows.at[idx, 'nouns'] = nouns
                    df_sentence_rows.at[idx, 'verbs'] = verbs
                    df_sentence_rows.at[idx, 'adjs'] = adjs
                df_sentence_rows['positiveness'] = preds
                # df_sentence_rows['sentiment_point'] = maximum_probs
                # df_sentence_rows.set_index('sentence_id',inplace=True)
                print(df_sentence_rows)
                ##analysis_sentence 테이블 sentiment update 해주는 작업
                ##가져온 데이터의 primary 키를 u 로 잡아준다.
                # chunk_size = 10000 #chunk row size
                # list_df_sentence_rows = [df_sentence_rows[i:i+chunk_size] for i in range(0,df_sentence_rows.shape[0],chunk_size)]
                # for index,df_sentence_rows in enumerate(list_df_sentence_rows):
                # print('{}번째 chunk'.format(index))
                # df_sentence_rows.to_sql('analysis_sentence_tmp',self.engine.connect(),if_exists='replace',index=False,chunksize=1)

                conn = self.engine.connect()
                trans = conn.begin()

                try:
                    # #delete those rows that we are going to "upsert"
                    # self.engine.execute("DELETE anal_s FROM analysis_sentence AS anal_s, analysis_sentence_tmp AS anal_st WHERE anal_s.sentence_id = anal_st.sentence_id")
                    # print('delete...ing')
                    # trans.commit()

                    #insert changed rows
                    df_sentence_rows.to_sql('crawl_sentence',
                                            self.engine,
                                            if_exists='append',
                                            index=False)
                    print('insert...ing')

                except Exception as e:
                    print(e)
                    trans.rollback()
                    raise

                logger.info("Prediction Done!")
                conn.close()
            except Exception as e:
                print(e)
                continue
        '인상된다. 월세액 세액공제 한도는 750만원이며 임대차 계약서상 주소지와 계약기간 등 내역을 정확히 기재해야 공제를 받을 수 있다.임차보증금 3억원 이하의 주택 임차보증금 반환 보증 보험료도 '
        '올해 연말정산부터 보험료 세액공제를 받을 수 있으며, 생산직 근로자의 초과근로수당 비과세 적용 시 기준이 되는 월정액 급여액은 150만원 이하에서 190만원 이하로 상향된다.6세 이하 자녀 '
        '세액공제는 아동수당 지급에 따라 올해부터 폐지된다. 올 연말정산부터는 종교단체가 종교인에게 지급한 소득도 신고대상에 포함된다.'
    ]
]
mecab = Mecab()
# kkma = Kkma()
sentences = []
list_vec = []
for da in data:
    # print(da)
    # sentences.append(kkma.sentences(da[0]))
    # for s in sentences:
    #     for w in s:
    #         list_vec.append(mecab.nouns(w))
    list_vec.append(mecab.nouns(da[0]))

word_list = []
for l in list_vec:
    empty_vec = []
    for w in l:
        if len(w) >= 2:
            empty_vec.append(w)
    word_list.append(empty_vec)

embedding_model = Word2Vec(word_list,
                           size=100,
                           window=5,
                           min_count=2,
                           workers=3,
                           iter=20,
# In[30]:

travel_sentences = []
for content in contents:
    travel_sentences.extend(re.split(
        ';|\.|\?|\!', content))  # 블로그 내용에 대해서 문장으로 나누기 위해서 문장의 끝을 나타내는
    # ;,.,?,! 를 구분자로 사용하겠습니다.
travel_sentences[:10]

# In[31]:

# 블로그 내용을 문장별로 구분하고, 구분된 문장 별로 명사를 추출하여 정리하겠습니다

travel_sentence_nouns = []
for sentence in travel_sentences:
    sentence_nouns = mecab.nouns(sentence)
    if sentence_nouns not in stop_words:
        travel_sentence_nouns.append(sentence_nouns)

travel_sentence_nouns[0:5]

# In[32]:

# 상위 단어 top_nouns 에 대해서 key에 해당하는 단어, value에 해당하는 id를 넣어 딕셔너리 형태로 저장하겠습니다.

travel_word2id = {
    word: num
    for num, word in enumerate(sorted_tfidf_dict_df.index)
}
sorted(travel_word2id.items(), key=lambda x: x[1], reverse=False)[:10]
Esempio n. 22
0
for i, text in enumerate(x_data):
    text = BeautifulSoup(text, 'html.parser').text
    #print(text) #스토리가 진짜 너무 노잼
    x_data[i] = text
print(x_data)

# 텍스트 정제 (특수기호 제거)
for i, text in enumerate(x_data):
    text = re.sub(r'[^ ㄱ-ㅣ가-힣]', '', text)  #특수기호 제거, 정규 표현식
    #print(document) stale and uninspired
    x_data[i] = text

#텍스트 정제 (어간 추출)
for i, text in enumerate(x_data):
    # okt = konlpy.tag.Okt()
    clean_words = mecab.nouns(text)
    print(clean_words)  #['스토리', '진짜', '노잼']
    text = ' '.join(clean_words)
    print(text)  #스토리 진짜 노잼
    x_data[i] = text
print(x_data)

#단어 카운트 (가중치 부여)
transformer = TfidfVectorizer()
transformer.fit(x_data)
#print(transformer.get_feature_names()) #['계약', '기간', '등록', '무료', '배송', '백화점', '보고', '상황', '선물', '소식', '신제품', '오늘', '인기', '일정', '제품', '진행', '쿠폰', '파격', '프로젝트', '한정', '할인', '확인', '회의']
#print(vectorizer.vocabulary_) #{'신제품': 10, '소식': 9, '쿠폰': 16, '선물': 8, '무료': 3, '배송': 4, '백화점': 5, '파격': 17, '오늘': 11, '할인': 20, '인기': 12, '제품': 14, '기간': 1, '한정': 19, '일정': 13, '확인': 21, '프로젝트': 18, '진행': 15, '상황': 7, '보고': 6, '계약': 0, '회의': 22, '등록': 2}
x_data = transformer.transform(x_data)
print(x_data.shape)  #(10, 23)
#print(x_data)
Esempio n. 23
0
message_list = dataset["MESSAGE"].values.tolist()
category_list = dataset["CATEGORY"].values.tolist()
sub_list = dataset["SUB"].values.tolist()

# Mecab
mecab = Mecab()

noun_message_list = []
for message in message_list:
    message_split = message.split("]")
    new_message = message_split[1]
    if len(message_split) > 2:
        for message_piece in message_split[2:]:
            new_message = f"{new_message}]{message_piece}"
    # noun_list = mecab.nouns(message.split("]")[1])
    noun_list = mecab.nouns(new_message)
    if len(noun_list) > 0:
        message_noun = noun_list[0]
        for noun in noun_list[1:]:
            message_noun = f"{message_noun} {noun}"
    else:
        message_noun = " "
    noun_message_list.append(message_noun)
    # print(message_noun)

# new_dataset = [category_list, sub_list, message_list]
new_dataset = {
    "CATEGORY": category_list,
    "SUB": sub_list,
    "MESSAGE": noun_message_list
}
Esempio n. 24
0
dirIdx = ['AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH']
mecab = Mecab()
documents = dict()
keyword_dict = dict()
t = tqdm(total=512761)

documents = dict()
# 단어빈도계산 tf
for dirChar in dirIdx:
    dir = f'corpus\{dirChar}'
    numOfFiles = len(os.listdir(dir))
    for wikiNum in numToString(numOfFiles):
        with open(f"corpus\{dirChar}\wiki_{wikiNum}", encoding='utf-8') as f:
            for doc in f:
                data = json.loads(doc)
                contents = [
                    ' '.join([
                        token for token in mecab.nouns(
                            re.sub('[^A-Za-z0-9가-힣]', '', line))
                        if token not in stopwords
                    ]) for line in filter(None, data['text'].split('\n'))
                ]
                title = data['title']
                url = data['url']
                id = data['id']
                documents[id] = (title, url, contents)
                t.update(1)

with open("corpus.txt", "wb") as f:
    data = pickle.dumps(documents)
    f.write(data)
Esempio n. 25
0
class MorphologicalAnalysis:

    def __init__(self):
        self.tagger = Mecab()
        self.Regualr_contents = []
        self.result_contents = []
        self.morphological_list = []
        self.fillter = '[!"#$%&\'()*+,-./:;<=>?[\]^_`{|}~“”·]'
        
    def get_result_contents(self):
        return self.result_contents

    def set_result_contents(self,content):
        self.result_contents.append(content)
    
    def get_morphological_list(self):
        return self.morphological_list

    def set_morphological_list(self,content):
        self.morphological_list.append(content)
        
    def contents_fillter(self,contents):        
        for i in range(len(contents)):
            self.set_result_contents(re.sub(self.fillter , '', contents[i]))
            
        return self.get_result_contents()
            
    def morphological_analysis(self, fillter_contents):
        return_list = []
        for i in range (len(fillter_contents)):
            return_list.append(self.tagger.pos(fillter_contents[i]))
        
        return return_list
    
    def extract_nouns(self, fillter_contents):
        for i in range (len(fillter_contents)):
            self.set_morphological_list(self.tagger.nouns(fillter_contents[i]))
        
        result_list = sum(self.get_morphological_list(),[])
        
        return result_list
    
    def count_morphological(self,morphological_data):
        count = Counter(morphological_data)
        tag_count = []
        tags = []
        return_list = []
        
        for n , c in count.most_common(100):
            dics = {'tag':n , 'count':c}
            if len(dics['tag']) >= 2 and len(tags) <= 49:
                tag_count.append(dics)
                tags.append(dics['tag'])
  

        for tag in tag_count:

            print(" {:<14}".format(tag['tag']), end='\t')

            print("{}".format(tag['count']))
        
        for tag in tag_count:
            return_list.append(tag['tag'])
            return_list.append(tag['count'])
            
        result_words = []
        result_count = []

        for i in range (0,len(return_list),2):
            result_words.append(return_list[i])
    
        for j in range (1, len(return_list),2):
            result_count.append(return_list[j])

        result = dict(zip(result_words , result_count))
        
        return result
            
    
    def to_one_list(self,morphological_data):
        one_list = sum(morphological_data , [])
        return one_list
    
    def count_test_module(self, data):
        count = Counter(data)
        words = dict(count.most_common())
        
        return words
Esempio n. 26
0
class Predict:
    def __init__(self, pred_config:Pred_config,keyword=None,contents_id=None):
        self.pred_config = pred_config
        self.engine = create_engine(("mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8mb4").format('root','robot369',
                                                                                                '10.96.5.179',3306,'datacast2'))
        self.args = self.pred_config.get_args()

        ##쿠다, cpu 중 사용할 디바이스 설정
        self.device = self.pred_config.get_device()

        ##배치사이즈 설정(모델이 한번에 처리할 input 사이즈 크기)
        self.batch_size = self.pred_config.batch_size

        ##모델 가져오기
        self.model = self.pred_config.load_model(self.args, self.device)

        ##토크나이저 가져오기
        self.tokenizer = self.pred_config.load_tokenizer()
        self.nlp = Mecab()
        self.keyword = keyword
        self.contents_id = contents_id
        self.db = Sql("datacast2")
    def verbs(self,phrase):
        """Verbs extractor."""
        verbs = ['VV']
        tagged = self.nlp.pos(phrase)
        return [s for s, t in tagged if t in verbs]

    def adjs(self,phrase):

        """Adjs extractor."""
        adjs = ['VA','IC']
        tagged = self.nlp.pos(phrase)
        return [s for s, t in tagged if t in adjs]

    def read(self):
        # conn = pymysql.connect(host='1.221.75.76', user='******', password='******', database='datacast')
        # curs = conn.cursor(pymysql.cursors.DictCursor)
        # sql_select_sentence = 'select * from analysis_sentence'
        # curs.execute(sql_select_sentence)
        # rows = curs.fetchall()
        ##pandas datatable 형태로 sentece 테이블 읽어들이기


        print('sql:',"SELECT ct.channel,cc.contents_id,cs.text from crawl_task as ct join crawl_contents as cc on ct.task_id=cc.task_id JOIN crawl_sentence AS cs ON cs.contents_id = cc.contents_id "
            "WHERE cc.contents_id=\'%s\' and ct.keyword=\'%s\'" % (self.contents_id ,self.keyword))
        # df_sentence_rows = pd.read_sql("SELECT ct.task_id,ct.channel,cc.contents_id,cc.text,cc.url from crawl_task as ct join crawl_contents as cc on ct.task_id=cc.task_id WHERE ct.keyword=\'%s\' limit %d,%d;"%(self.keyword,start_num,chunk_size),self.engine)
        df_sentence_rows = pd.read_sql(
            "SELECT ct.keyword,ct.channel,cc.contents_id as contents_id,cs.sentence_id as sentence_id, cs.text as sentence from crawl_task as ct join crawl_contents as cc on ct.task_id=cc.task_id JOIN crawl_sentence AS cs ON cs.contents_id = cc.contents_id "
            "WHERE cc.contents_id=\'%s\' and ct.keyword=\'%s\'" % (
            self.contents_id,self.keyword),
            self.engine)

        return df_sentence_rows

    def convert_input_sentence_to_tensor_dataset(self,df_sentence_rows,cls_token_segment_id=0,
                                             pad_token_segment_id=0,
                                             sequence_a_segment_id=0,
                                             mask_padding_with_zero=True):
        tokenizer = self.tokenizer
        args = self.args


        cls_token = tokenizer.cls_token
        sep_token = tokenizer.sep_token
        pad_token_id = tokenizer.pad_token_id

        all_input_ids = []
        all_attention_mask = []
        all_token_type_ids = []

        ###input file 읽어들이기
        ###input file 읽어서 tensordata type 으로 변환
        for index in df_sentence_rows.index:
            sentence = df_sentence_rows.at[index, 'sentence']

            tokens = tokenizer.tokenize(sentence)

            # Account for [CLS] and [SEP]
            special_tokens_count = 2
            #문장의 최대길이 보다 큰 문장에 대해서 길이 조정을 해준다.
            if len(tokens) > args.max_seq_len - special_tokens_count:
                tokens = tokens[:(args.max_seq_len - special_tokens_count)]

            # Add [SEP] token
            tokens += [sep_token]
            token_type_ids = [sequence_a_segment_id] *len(tokens)

            # Add [CLS] token
            tokens = [cls_token] + tokens
            token_type_ids = [cls_token_segment_id] + token_type_ids
            input_ids = tokenizer.convert_tokens_to_ids(tokens)

            # The mask has 1 real tokens and 0 for padding tokens. Only real tokens are attended to.
            attention_mask = [1 if mask_padding_with_zero else 0] * len(input_ids)
            # Zero-pad up to the sequence length.
            padding_length = args.max_seq_len - len(input_ids)
            input_ids = input_ids+([pad_token_id] * padding_length)
            attention_mask = attention_mask + ([0 if mask_padding_with_zero else 1] * padding_length)
            token_type_ids = token_type_ids + ([pad_token_segment_id] * padding_length)

            all_input_ids.append(input_ids)
            all_attention_mask.append(attention_mask)
            all_token_type_ids.append(token_type_ids)

        # Change to Tensor
        all_input_ids = torch.tensor(all_input_ids, dtype=torch.long)
        all_attention_mask = torch.tensor(all_attention_mask, dtype=torch.long)
        all_token_type_ids = torch.tensor(all_token_type_ids, dtype=torch.long)
        dataset = TensorDataset(all_input_ids, all_attention_mask, all_token_type_ids)
        return dataset

    def predict(self):
        ##tuning 시 파라미터 정보가 들어있는 파일(training_args.bin)
        args = self.args

        ##쿠다, cpu 중 사용할 디바이스 설정
        device = self.device

        ##배치사이즈 설정(모델이 한번에 처리할 input 사이즈 크기)
        batch_size= self.batch_size

        ##모델 가져오기
        model = self.model
        logger.info(args)

        ##감성분석할 데이터 가져오기
        df_sentence_data_rows = self.read()

        dataset = self.convert_input_sentence_to_tensor_dataset(df_sentence_data_rows)

        # dataset 을 model 을 이용하여 output 도출
        # Predict
        sampler = SequentialSampler(dataset)
        data_loader = DataLoader(dataset, sampler=sampler, batch_size=batch_size)
        preds = None
        probs = None
        print(type(data_loader),len(data_loader))
        for index,batch in enumerate(tqdm(data_loader, desc="Prediction")):
            batch = tuple(t.to(device) for t in batch)
            with torch.no_grad():
                inputs = {"input_ids": batch[0],
                          "attention_mask": batch[1],
                          "labels": None}
                if args.model_type != "distilkobert":
                    inputs["token_type_ids"] = batch[2]
                outputs = model(**inputs)
                logits = outputs[0]

                if preds is None:
                    preds = logits.detach().cpu().numpy()
                    probs = np.exp(logits.detach().cpu().numpy())/ (1 + np.exp(logits.detach().cpu().numpy()))
                else:
                    preds = np.append(preds, logits.detach().cpu().numpy(), axis=0)
                    probs = np.append(probs, np.exp(logits.detach().cpu().numpy())/ (1 + np.exp(logits.detach().cpu().numpy())), axis=0)
        preds = np.argmax(preds, axis=1).tolist()
        prob_max_index = np.argmax(probs, axis=-1)
        maximum_probs = probs[np.arange(probs.shape[0]), prob_max_index]
        # maximum_probs = maximum_probs.tolist()
        # maximum_probs = list([round(maximum_prob,2) if pred==1 else round(maximum_prob,2)*(-1) for pred,maximum_prob in zip(preds,maximum_probs)])
        df_sentence_data_rows['positiveness'] = preds

        #update
        for idx in tqdm(df_sentence_data_rows.index,desc="sentence_anlysis&db_update"):
            try:
                sentence_id = df_sentence_data_rows.at[idx,'sentence_id']
                sentence = df_sentence_data_rows.at[idx,'sentence']
                positiveness = df_sentence_data_rows.at[idx,'positiveness']
                nouns = list(set(self.nlp.nouns(sentence)))
                nouns = json.dumps(nouns,ensure_ascii=False)

                verbs = list(set(self.verbs(sentence)))
                verbs = json.dumps(verbs,ensure_ascii=False)

                adjs = list(set(self.adjs(sentence)))
                adjs = json.dumps(adjs,ensure_ascii=False)
                self.db.update_multi_column("crawl_sentence",
                                     update_dict={"nouns":nouns,"verbs":verbs,"adjs":adjs,"positiveness":float(positiveness)},
                                     where_dict={"sentence_id":float(sentence_id)})
            except Exception as e:
                print(e)
                continue
Esempio n. 27
0
# konlpy : 설치하기
# http://konlpy.org/ko/v0.5.2/install/#id2

from eunjeon import Mecab

# Mecab 함수를 tagger라는 이름으로 사용
tagger = Mecab()
# 문장에서 명사만 분류
tagger.nouns("고양이가 냐 하고 울면 나는 녜 하고 울어야지")

# 빛 아래 유령
poem = """
흘러내린 머리카락이 흐린 호박빛 아래 빛난다.
유영하며.
저건가보다.
세월의 힘을 이겨낸 마지막 하나 남은 가로등.
미래의 색, 역겨운 청록색으로 창백하게 바뀔 마지막 가로등
난 유영한다. 차분하게 과거에 살면서 현재의 공기를 마신다.
가로등이 깜빡인다.
나도 깜빡여준다.
"""
# 문장을 형태소 단위로 끊어줌
tagger.morphs(poem)

# 문장을 형태소단위로 끊고, 형태소 마다 품사를 분석
# 이때, ('지우개', 'NNG')등의 형식을 분류되는데, NNG는 일반명사를 뜻
# 자세한 품사태그는 링크를 참고 : https://m.blog.naver.com/PostView.nhn?blogId=aul-_-&logNo=221557243190
print(tagger.pos(poem))
# print(tagger.nouns(poem))