示例#1
0
    def word_game(self, username, request_string, nickname):
        # 새로운 블록버스터급 재난이 찾아온다.

        # '더 코더스: 더 뉴 헬: 끝말잇기' 2019년 10월 말 대개봉!

        la = LuisAI()
        hl = HackerLibrary()
        db = DataBase()

        nlp_result = la.think(request_string)['topScoringIntent']['intent']

        if nlp_result == 'Communication.Interrupt.QuitWordGame':
            print('WordGame Exit!')
            random_response_string = [["훗! 제가 이겼네요."],
                                      ["후훗! 제가 이겼어요! 앞으로도 끝말잇기 많이 해요!"],
                                      ["제가 이겼어요! " + nickname + "님과 하는 거라 더 재미있었던 것 같아요. 앞으로도 자주 같이 놀아 주세요!"],
                                      ]
            feelings_result = db.alter_feelings(username, 5)
            db.set_state(username, "normal")
            db.reset_used_word(username)

            return hl.choose_reply(random_response_string, feelings_result['data']['userstate']['feelings'])

        db.set_state(username, "wordgame")

        if self.check_dict(request_string) is not 0:
            return "사전에서 단어를 찾을 수 없어요!"

        add_result = db.add_used_word(username, request_string)

        if add_result is not 0:
            if add_result is 1:
                return "이미 사용한 낱말이에요!"
            else:
                return "낱말이 올바르지 않아요!"

        result = self.gen_word(request_string, username)
        if result is -1:
            db.set_state(username, "normal")
            return "제가 졌어요!"
        else:
            db.add_used_word(username, result)
            return result
示例#2
0
    def gen_word(string, username):
        # TODO: More Words
        db = DataBase()
        used_words = db.get_used_words(username)

        # read csv, and split on "," the line
        csv_file = csv.reader(open('./worddb/fucking_words.csv', "r", encoding='utf8'), delimiter=",")

        reply_arr = []

        # loop through csv list
        for row in csv_file:
            for r in row:
                if r.startswith(list(string)[-1]):
                    if r not in used_words:
                        reply_arr.append(r)

        if len(reply_arr) is 0:
            # 우리말샘 AJAX API 사용하기 (Unofficial)
            print("우리말샘 AJAX 진입...")

            params = {
                # Query parameter
                'searchTerm': list(string)[-1]
            }

            print(params)

            try:
                r = requests.post(
                    "https://opendict.korean.go.kr/search/autoComplete",
                    params=params)
                if r.json()['json'][1] < 1:
                    return -1

                print(r.json())

                # 따옴표 안에있는것만 추출
                matched_groups = re.findall(r"'(.*?)'", r.json()['json'][0], re.DOTALL)
                print("BEFORE: ")
                print(matched_groups)

                if len(matched_groups) > 0:
                    for m in matched_groups:
                        # 한글자인거 필터링
                        if len(list(m)) < 2:
                            matched_groups.remove(m)

                        # '다' 로 끝나는거 필터링 (임시)
                        if m.endswith('다'):
                            matched_groups.remove(m)
                            print('Removed %s' % str(m))
                    print("AFTER: ")
                    print(matched_groups)

                    if len(list(matched_groups)) < 1:
                        return -1

                    return random.choice(matched_groups)

                """{
                    "json": [
                        "var dq_searchKeyword='섹'; var dq_searchResultList=new Array('섹','섹강','섹겅','섹게','섹게이','섹겡','섹겨','섹경','섹계이','섹고');",
                        104
                    ]
                }"""

            except Exception as e:
                print(e)
                print('에러! : AJAX 자동완성 API 접속 실패!')

            return -1

        final_reply = random.choice(reply_arr)
        debug_result = db.add_used_word(username, final_reply)

        if debug_result is not 0:
            return "에러! 단어 DB에 부정한 문자열이 있습니다!"
        return final_reply