Skip to content

Latest commit

 

History

History
495 lines (488 loc) · 12.9 KB

220122.md

File metadata and controls

495 lines (488 loc) · 12.9 KB

Day 103

파이썬으로 챗봇 만들기

Chapter 4

챗봇을 위한 데이터 준비하기

Rasa의 JSON 데이터를 시각화하고 수정하기
책의 예제 data.json을 사용한다.

{
    "rasa_nlu_data": {
        "common_examples": [
        {
            "text": "Hello",
            "intent": "greeting",
            "entities": []
        },
        {
            "text": "I want to know my Horoscope",
            "intent": "get_horoscope",
            "entities": []
        },
        {
            "text": "Can you please tell me my horoscope?",
            "intent": "get_horoscope",
            "entities": []
        },
        {
            "text": "Please subscribe me",
            "intent": "subscription",
            "entities": []
        },
        {
            "text": "Hi",
            "intent": "greeting",
            "entities": []
        },
        {
            "text": "Hello horoscopebot",
            "intent": "greeting",
            "entities": []
        },
        {
            "text": "hello bot",
            "intent": "greeting",
            "entities": []
        },
        {
            "text": "hi there",
            "intent": "greeting",
            "entities": []
        },
        {
            "text": "bonjour",
            "intent": "greeting",
            "entities": []
        },
        {
            "text": "Tell me my horoscope please",
            "intent": "get_horoscope",
            "entities": []
        },
        {
            "text": "What does my horoscope say?",
            "intent": "get_horoscope",
            "entities": []
        },
        {
            "text": "Can you tell me my horoscope?",
            "intent": "get_horoscope",
            "entities": []
        },
        {
            "text": "What's today's horoscope?",
            "intent": "get_horoscope",
            "entities": []
        },
        {
            "text": "I would like to know my horoscope for today",
            "intent": "get_horoscope",
            "entities": []
        },
        {
            "text": "12-12",
            "intent": "dob_intent",
            "entities": [
            {
                "start": 0,
                "end": 2,
                "value": "12",
                "entity": "DD"
            },
            {
                "start": 3,
                "end": 5,
                "value": "12",
                "entity": "MM"
            }
            ]
        },
        {
            "text": "08-01",
            "intent": "dob_intent",
            "entities": [
            {
                "start": 0,
                "end": 2,
                "value": "08",
                "entity": "DD"
            },
            {
                "start": 3,
                "end": 5,
                "value": "01",
                "entity": "MM"
            }
            ]
        },
        {
            "text": "01-01",
            "intent": "dob_intent",
            "entities": [
            {
                "start": 0,
                "end": 2,
                "value": "01",
                "entity": "DD"
            },
            {
                "start": 3,
                "end": 5,
                "value": "01",
                "entity": "MM"
            }
            ]
        },
        {
            "text": "15-08",
            "intent": "dob_intent",
            "entities": [
            {
                "start": 0,
                "end": 2,
                "value": "15",
                "entity": "DD"
            },
            {
                "start": 3,
                "end": 5,
                "value": "08",
                "entity": "MM"
            }
            ]
        },
        {
            "text": "19-01",
            "intent": "dob_intent",
            "entities": [
            {
                "start": 0,
                "end": 2,
                "value": "19",
                "entity": "DD"
            },
            {
                "start": 3,
                "end": 5,
                "value": "01",
                "entity": "MM"
            }
            ]
        },
        {
            "text": "28-02",
            "intent": "dob_intent",
            "entities": [
            {
                "start": 0,
                "end": 2,
                "value": "28",
                "entity": "DD"
            },
            {
                "start": 3,
                "end": 5,
                "value": "02",
                "entity": "MM"
            }
            ]
        },
        {
            "text": "please subscribe me",
            "intent": "subscription",
            "entities": [
            {
                "start": 7,
                "end": 16,
                "value": "True",
                "entity": "subscribe"
            }
            ]
        },
        {
            "text": "yes subscribe",
            "intent": "subscription",
            "entities": [
            {
                "start": 4,
                "end": 13,
                "value": "True",
                "entity": "subscribe"
            }
            ]
        },
        {
            "text": "subscribe",
            "intent": "subscription",
            "entities": [
            {
                "start": 0,
                "end": 9,
                "value": "True",
                "entity": "subscribe"
            }
            ]
        },
        {
            "text": "subscribe me horoscopebot",
            "intent": "subscription",
            "entities": [
            {
                "start": 0,
                "end": 9,
                "value": "True",
                "entity": "subscribe"
            }
            ]
        },
        {
            "text": "subscribe me please",
            "intent": "subscription",
            "entities": [
            {
                "start": 0,
                "end": 9,
                "value": "True",
                "entity": "subscribe"
            }
            ]
        },
        {
            "text": "what is the horoscope for Capricorn?",
            "intent": "get_horoscope",
            "entities": [
            {
                "start": 26,
                "end": 35,
                "value": "Capricorn",
                "entity": "horoscope_sign"
            }
            ]
        },
        {
            "text": "tell me the horoscope for Gemini",
            "intent": "get_horoscope",
            "entities": [
            {
                "start": 26,
                "end": 32,
                "value": "Gemini",
                "entity": "horoscope_sign"
            }
            ]
        },
        {
            "text": "horoscope for Aries",
            "intent": "get_horoscope",
            "entities": [
            {
                "start": 14,
                "end": 19,
                "value": "Aries",
                "entity": "horoscope_sign"
            }
            ]
        },
        {
            "text": "Leo's today's horoscope",
            "intent": "get_horoscope",
            "entities": [
            {
                "start": 0,
                "end": 3,
                "value": "Leo",
                "entity": "horoscope_sign"
            }
            ]
        },
        {
            "text": "what's the horoscope for Sagittarius",
            "intent": "get_horoscope",
            "entities": [
            {
                "start": 25,
                "end": 36,
                "value": "Sagittarius",
                "entity": "horoscope_sign"
            }
            ]
        },
        {
            "text": "What is the horoscope for Scorpio today?",
            "intent": "get_horoscope",
            "entities": [
            {
                "start": 26,
                "end": 33,
                "value": "Scorpio",
                "entity": "horoscope_sign"
            }
            ]
        },
        {
            "text": "what's the horoscope for Aquarius?",
            "intent": "get_horoscope",
            "entities": [
            {
                "start": 25,
                "end": 33,
                "value": "Aquarius",
                "entity": "horoscope_sign"
            }
            ]
        },
        {
            "text": "unsubscribe me",
            "intent": "subscription",
            "entities": [
            {
                "start": 0,
                "end": 11,
                "value": "False",
                "entity": "subscribe"
            }
            ]
        },
        {
            "text": "unsubscribe",
            "intent": "subscription",
            "entities": [
            {
                "start": 0,
                "end": 11,
                "value": "False",
                "entity": "subscribe"
            }
            ]
        },
        {
            "text": "Capricorn",
            "intent": "get_horoscope",
            "entities": [
            {
                "start": 0,
                "end": 9,
                "value": "Capricorn",
                "entity": "horoscope_sign"
            }
            ]
        },
        {
            "text": "Aries",
            "intent": "get_horoscope",
            "entities": [
            {
                "start": 0,
                "end": 5,
                "value": "Aries",
                "entity": "horoscope_sign"
            }
            ]
        },
        {
            "text": "My horoscope sign is Cancer",
            "intent": "get_horoscope",
            "entities": [
            {
                "start": 21,
                "end": 27,
                "value": "Cancer",
                "entity": "horoscope_sign"
            }
            ]
        },
        {
            "text": "sagittarius",
            "intent": "get_horoscope",
            "entities": [
            {
                "start": 0,
                "end": 11,
                "value": "sagittarius",
                "entity": "horoscope_sign"
            }
            ]
        },
        {
            "text": "What is my horoscope?",
            "intent": "get_horoscope",
            "entities": []
        },
        {
            "text": "Virgo is my horoscope sign",
            "intent": "get_horoscope",
            "entities": [
            {
                "start": 0,
                "end": 5,
                "value": "Virgo",
                "entity": "horoscope_sign"
            }
            ]
        },
        {
            "text": "Hi there!",
            "intent": "greeting",
            "entities": []
        }
        ],
        "regex_features": [],
        "entity_synonyms": []
    }
}

정의한 각 인텐트에 5~6개의 어터런스가 추가되었다.
또한 rasa-nlu-trainer UI를 사용하여 정의한 엔티티가 common_example 목록에 start 및 end 키로 포함된 것을 볼 수 있다. 이는 특정 엔티티 값이 어느 지점에서 시작되고 언제 끝나는지를 모델에 알려준다.

        {
            "text": "28-02",
            "intent": "dob_intent",
            "entities": [
            {
                "start": 0,
                "end": 2,
                "value": "28",
                "entity": "DD"
            },
            {
                "start": 3,
                "end": 5,
                "value": "02",
                "entity": "MM"
            }
            ]
        },

챗봇 모델 훈련하기

설정 파일(Configuration File) 생성하기
우선 프로젝트 폴더 밖에 config.json 파일을 만든다.

{
    "pipeline":"tensorflow_embedding",
    "path":"./models/nlu",
    "data":"./data/data.json"
}

config.json 중요 설정 파라미터

  • pipeline : 입력받은 문장에서 중요한 정보를 추출하기 위한 플랫폼 featurizers 혹은 feature extractors으로 무엇을 사용할 것인지에 대하여 명시한다.
  • path : 학습을 끝마친 모델을 어디에 보관할지 정의한 디렉토리이다.
  • data : 모델을 학습시키기 위한 훈련 데이터를 저장하기 위한 경로이다.

YAML 설정
.yml 파일을 설정 파일로 사용해도 된다.

# 예제 1
language: "en"

pipeline: "tensorflow_embedding"

# 예제 2
language: "en"

pipeline:
- name: "nlp_spacy"
- name: "tokenizer_spacy"
- name: "intent_entity_featureizer_regex"
- name: "intent_featurizer_spacy"
- name: "ner_crf"
- name: "ner_synonyms"
- name: "intent_classifier_sklearn"

모든 수신 메시지는 정의된 구성 요소의 순서에 따라 처리되는데, 이 때 정의된 구성 요소가 순차적으로 하나씩 실행되므로 프로세싱 파이프라인이라고 부른다. 엔티티 추출, 인텐트 분류, 전처리 등 각각의 목적에는 서로 다른 구성요소가 사용된다. 이러한 포맷의 장점은 Rasa를 통해 사전에 정의된 파이프라인을 명확하게 지정할 수 있다는 것이다.

rasa-nlu가 지원하는 파이썬 버전이 내 환경과 맞지 않아 대안을 찾아야 할 것 같다.

그래도 그냥 rasa를 이용하면 학습은 가능했고 인텐트도 정상적으로 식별해내기까진 했다.