본문 바로가기
웹/elasticsearch

한글 형태소 분석기 비교

by java개발자 2019. 7. 5.

1. 은전한닢 형태소 분석기
bin/elasticsearch-plugin install https://github.com/javacafe-project/elastic-book-etc/raw/master/plugin/elasticsearch-analysis-seunjeon-6.4.3.zip

*인덱스설정

PUT seunjeon_default_analyzer
{
  "settings": {
    "index": {
      "analysis": {
        "tokenizer": {
          "seunjeon_default_tokenizer": {
            "type": "seunjeon_tokenizer",
            "index_eojeol": false
          }
        },        
        "analyzer": {
          "korean": {
            "type": "custom",
            "tokenizer": "seunjeon_default_tokenizer"
          }
        }
      }
    }
  }
}

*테스트

POST seunjeon_default_analyzer/_analyze
{
  "analyzer": "korean",
  "text": "나는 사과를 먹는다. 집에 가고 싶다."
}

*결과

{
  "tokens": [
    {
      "token": "나/N",
      "start_offset": 0,
      "end_offset": 1,
      "type": "N",
      "position": 0
    },
    {
      "token": "사과/N",
      "start_offset": 3,
      "end_offset": 5,
      "type": "N",
      "position": 1
    },
    {
      "token": "먹/V",
      "start_offset": 7,
      "end_offset": 8,
      "type": "V",
      "position": 2
    },
    {
      "token": "집/N",
      "start_offset": 12,
      "end_offset": 13,
      "type": "N",
      "position": 3
    },
    {
      "token": "갈/V",
      "start_offset": 15,
      "end_offset": 16,
      "type": "V",
      "position": 4
    },
    {
      "token": "싶/V",
      "start_offset": 18,
      "end_offset": 19,
      "type": "V",
      "position": 5
    }
  ]
}

2. Nori 형태소 분석기
bin/elasticsearch-plugin install analysis-nori

*인덱스설정

PUT nori_analyzer
{
  "settings": {
    "index": {
      "analysis": {
        "tokenizer": {
          "nori_user_dict_tokenizer": {
            "type": "nori_tokenizer",
            "decompound_mode": "mixed"
         
          }
        },        
        "analyzer": {
          "nori_token_analyzer": {
            "type": "custom",
            "tokenizer": "nori_user_dict_tokenizer"
          }
        }
      }
    }
  }
}

*테스트

POST nori_analyzer/_analyze
{
  "analyzer": "nori_token_analyzer",
  "text": "나는 사과를 먹는다. 집에 가고 싶다."
}

*결과

{
  "tokens": [
    {
      "token": "나",
      "start_offset": 0,
      "end_offset": 1,
      "type": "word",
      "position": 0
    },
    {
      "token": "는",
      "start_offset": 1,
      "end_offset": 2,
      "type": "word",
      "position": 1
    },
    {
      "token": "사과",
      "start_offset": 3,
      "end_offset": 5,
      "type": "word",
      "position": 2
    },
    {
      "token": "를",
      "start_offset": 5,
      "end_offset": 6,
      "type": "word",
      "position": 3
    },
    {
      "token": "먹",
      "start_offset": 7,
      "end_offset": 8,
      "type": "word",
      "position": 4
    },
    {
      "token": "는다",
      "start_offset": 8,
      "end_offset": 10,
      "type": "word",
      "position": 5
    },
    {
      "token": "집",
      "start_offset": 12,
      "end_offset": 13,
      "type": "word",
      "position": 6
    },
    {
      "token": "에",
      "start_offset": 13,
      "end_offset": 14,
      "type": "word",
      "position": 7
    },
    {
      "token": "가",
      "start_offset": 15,
      "end_offset": 16,
      "type": "word",
      "position": 8
    },
    {
      "token": "갈",
      "start_offset": 15,
      "end_offset": 16,
      "type": "word",
      "position": 8
    },
    {
      "token": "고",
      "start_offset": 16,
      "end_offset": 17,
      "type": "word",
      "position": 9
    },
    {
      "token": "싶",
      "start_offset": 18,
      "end_offset": 19,
      "type": "word",
      "position": 10
    },
    {
      "token": "다",
      "start_offset": 19,
      "end_offset": 20,
      "type": "word",
      "position": 11
    }
  ]
}

 

3. 트위터 형태소 분석기
bin/elasticsearch-plugin install https://github.com/javacafe-project/elastic-book-etc/raw/master/plugin/elasticsearch-analysis-openkoreantext-6.4.3-plugin.zip

*인덱스설정

PUT openkoreantext_analyzer
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "openkoreantext-analyzer": {
            "tokenizer": "openkoreantext-tokenizer",
            "char_filter": ["openkoreantext-normalizer"],
            "filter": ["openkoreantext-stemmer", "openkoreantext-redundant-filter", "lowercase"]
          }
        }
      }
    }
  }
}

*테스트

POST openkoreantext_analyzer/_analyze
{
  "analyzer": "openkoreantext-analyzer",
  "text": "나는 사과를 먹는다. 집에 가고 싶다."
}

*결과

{
  "tokens": [
    {
      "token": "나",
      "start_offset": 0,
      "end_offset": 1,
      "type": "Noun",
      "position": 0
    },
    {
      "token": "사과",
      "start_offset": 3,
      "end_offset": 5,
      "type": "Noun",
      "position": 1
    },
    {
      "token": "먹다",
      "start_offset": 7,
      "end_offset": 10,
      "type": "Verb",
      "position": 2
    },
    {
      "token": "집",
      "start_offset": 12,
      "end_offset": 13,
      "type": "Noun",
      "position": 3
    },
    {
      "token": "가다",
      "start_offset": 15,
      "end_offset": 17,
      "type": "Verb",
      "position": 4
    },
    {
      "token": "싶다",
      "start_offset": 18,
      "end_offset": 20,
      "type": "Verb",
      "position": 5
    }
  ]
}

 

4. 구글 형태소 분석기

https://cloud.google.com/natural-language/

 

Cloud Natural Language  |  Cloud Natural Language API  |  Google Cloud

선행 학습된 커스텀 머신러닝 모델을 사용한 텍스트 분석으로 관련 항목을 추출하고 전반적인 감정 등을 파악하세요.

cloud.google.com

5. arirang
bin/elasticsearch-plugin install https://github.com/javacafe-project/elastic-book-etc/raw/master/plugin/elasticsearch-analysis-arirang-6.4.3.zip

*인덱스설정

PUT arirang_analyzer
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "arirang_custom": {
            "type": "arirang_analyzer",
            "tokenizer": "arirang_tokenizer",
            "filter": [
              "lowercase",
              "trim",
              "arirang_filter"
            ]
          }
        }
      }
    }
  }
}

*테스트

POST arirang_analyzer/_analyze
{
  "analyzer": "arirang_analyzer",
  "text": "나는 사과를 먹는다. 집에 가고 싶다."
}

*결과

{
  "tokens": [
    {
      "token": "나",
      "start_offset": 0,
      "end_offset": 1,
      "type": "korean",
      "position": 0
    },
    {
      "token": "사",
      "start_offset": 3,
      "end_offset": 4,
      "type": "korean",
      "position": 1
    },
    {
      "token": "사과",
      "start_offset": 3,
      "end_offset": 5,
      "type": "korean",
      "position": 1
    },
    {
      "token": "집",
      "start_offset": 12,
      "end_offset": 13,
      "type": "korean",
      "position": 2
    }
  ]
}

 

' > elasticsearch' 카테고리의 다른 글

openkoreantext_analyzer start_offset bug  (0) 2019.10.09
elasticsearch 에 없는 기능 - return terms  (1) 2019.04.26
test query  (0) 2019.04.26