본문 바로가기
WIKI 기술 지식 베이스

Kuromoji tokenizer

원문 보기 위키 갱신

출처: 문서

본문

Kuromoji 토크나이저

kuromoji_tokenizer는 Kuromoji 라이브러리와 IPAdic 사전을 사용해 일본어 텍스트에 대해 사전 기반 형태소 분석을 수행해요. 공백이나 문장부호를 기준으로 분할하는 토크나이저와 달리, 단어를 구분할 때 공백을 사용하지 않는 일본어 문장에서 자연스러운 단어 경계를 식별해요.

설치

kuromoji_tokenizer를 사용하려면 analysis-kuromoji 플러그인이 필요해요. 설치 방법은 Kuromoji 분석기 문서를 참고하세요.

파라미터

다음 표에서 kuromoji_tokenizer의 파라미터를 확인할 수 있어요.

파라미터 데이터 타입 설명
mode String 토크나이즈 모드예요. 유효한 값은 normal, search(기본값), extended예요. 자세한 내용은 토크나이즈 모드를 참고하세요.
discard_punctuation Boolean true이면 출력에서 문장부호 토큰을 제거해요. 기본값은 true예요.
discard_compound_token Boolean true이면 search 모드에서 생성되는 복합 토큰을 제거하고 하위 토큰만 유지해요. 기본값은 false예요.
user_dictionary String OpenSearch config 디렉터리에 두는 사용자 정의 사전 CSV 파일의 경로예요. 각 줄은 <text>,<subtokens>,<readings>,<part of speech> 형식을 따라야 해요. 선택 사항이에요.
user_dictionary_rules Array of strings user_dictionary와 동일한 CSV 형식으로 인라인으로 작성하는 사용자 정의 사전 규칙이에요. 선택 사항이며, user_dictionary와 함께 사용할 수는 없어요.
nbest_cost Integer -1보다 큰 값으로 설정하면 n-best 분할이 활성화되어, cost(로그 확률 페널티)가 최적 분할과 비교해 이 값 이내인 대체 토크나이즈 결과를 반환해요. 기본값은 -1(비활성화)이에요.
nbest_examples String nbest_cost를 자동으로 계산할 때 사용하는 쉼표로 구분된 예시 단어 목록이에요. 이 값을 지정하면 토크나이저가 주어진 예시 단어들도 토큰으로 생성하기 위해 필요한 최소 추가 cost를 찾아요. 선택 사항이에요.

토크나이즈 모드

토크나이즈 모드는 토크나이저가 복합어와 미등록 단어를 처리하는 방식을 결정해요.

모드 복합어 미등록 단어
normal 단일 토큰으로 유지해요(예: 関西国際空港이 하나의 토큰). 단일 토큰으로 유지해요.
search(기본값) 복합 토큰 형태에 더해 하위 토큰으로도 분할해요(예: 関西国際空港은 関西, 国際, 空港, 関西国際空港을 생성해요). 검색 쿼리의 재현율을 높여줘요. 단일 토큰으로 유지해요.
extended 단일 토큰으로 유지해요(normal과 동일). 유니그램(개별 문자)으로 분할해 모든 문자가 색인되도록 해요.

예제: 기본 토크나이즈

다음 예제는 기본 search 모드의 kuromoji_tokenizer를 사용하는 커스텀 분석기로 인덱스를 생성해요:

PUT /kuromoji-tokenizer-index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "kuromoji_analyzer": {
          "tokenizer": "kuromoji_tokenizer"
        }
      }
    }
  }
}

{% include copy-curl.html %}

"간사이 국제공항은 큰 공항이다"라는 의미의 문장으로 토크나이저를 테스트해 보세요:

POST /kuromoji-tokenizer-index/_analyze
{
  "analyzer": "kuromoji_analyzer",
  "text": "関西国際空港は大きな空港です"
}

{% include copy-curl.html %}

search 모드에서는 복합 지명 関西国際空港(간사이 국제공항)이 구성 요소로 분할되면서 복합 토큰으로도 함께 유지돼요:

{
  "tokens": [
    {
      "token": "関西",
      "start_offset": 0,
      "end_offset": 2,
      "type": "word",
      "position": 0
    },
    {
      "token": "関西国際空港",
      "start_offset": 0,
      "end_offset": 6,
      "type": "word",
      "position": 0,
      "positionLength": 3
    },
    {
      "token": "国際",
      "start_offset": 2,
      "end_offset": 4,
      "type": "word",
      "position": 1
    },
    {
      "token": "空港",
      "start_offset": 4,
      "end_offset": 6,
      "type": "word",
      "position": 2
    },
    {
      "token": "は",
      "start_offset": 6,
      "end_offset": 7,
      "type": "word",
      "position": 3
    },
    {
      "token": "大きな",
      "start_offset": 7,
      "end_offset": 10,
      "type": "word",
      "position": 4
    },
    {
      "token": "空港",
      "start_offset": 10,
      "end_offset": 12,
      "type": "word",
      "position": 5
    },
    {
      "token": "です",
      "start_offset": 12,
      "end_offset": 14,
      "type": "word",
      "position": 6
    }
  ]
}

예제: 토크나이즈 모드 비교

다음 예제는 같은 텍스트가 모드에 따라 어떻게 다르게 토크나이즈되는지 보여줘요. 각 모드별로 커스텀 토크나이저를 만들고 출력을 비교해 보세요:

PUT /kuromoji-mode-comparison
{
  "settings": {
    "analysis": {
      "tokenizer": {
        "kuromoji_normal": {
          "type": "kuromoji_tokenizer",
          "mode": "normal"
        },
        "kuromoji_search": {
          "type": "kuromoji_tokenizer",
          "mode": "search"
        },
        "kuromoji_extended": {
          "type": "kuromoji_tokenizer",
          "mode": "extended"
        }
      }
    }
  }
}

{% include copy-curl.html %}

알려진 복합어(関西国際空港, "간사이 국제공항"이라는 뜻)와 미등록 외래어(アバクロンビー, "Abercrombie"라는 뜻)가 포함된 텍스트로 테스트해 보세요:

normal 모드

POST /kuromoji-mode-comparison/_analyze
{
  "tokenizer": "kuromoji_normal",
  "text": "関西国際空港とアバクロンビー"
}

{% include copy-curl.html %}

normal 모드에서는 복합어 関西国際空港이 단일 토큰으로 유지되고, 미등록 외래어 アバクロンビー 역시 단일 토큰으로 유지돼요:

{
  "tokens": [
    {
      "token": "関西国際空港",
      "start_offset": 0,
      "end_offset": 6,
      "type": "word",
      "position": 0
    },
    {
      "token": "と",
      "start_offset": 6,
      "end_offset": 7,
      "type": "word",
      "position": 1
    },
    {
      "token": "アバクロンビー",
      "start_offset": 7,
      "end_offset": 14,
      "type": "word",
      "position": 2
    }
  ]
}

search 모드

POST /kuromoji-mode-comparison/_analyze
{
  "tokenizer": "kuromoji_search",
  "text": "関西国際空港とアバクロンビー"
}

{% include copy-curl.html %}

search 모드에서는 복합어 関西国際空港이 하위 토큰(関西, 国際, 空港)으로 분할되면서 positionLength: 3을 가진 복합 토큰으로도 유지돼요. 미등록 외래어 アバクロンビー는 normal 모드와 마찬가지로 단일 토큰으로 유지돼요:

{
  "tokens": [
    {
      "token": "関西",
      "start_offset": 0,
      "end_offset": 2,
      "type": "word",
      "position": 0
    },
    {
      "token": "関西国際空港",
      "start_offset": 0,
      "end_offset": 6,
      "type": "word",
      "position": 0,
      "positionLength": 3
    },
    {
      "token": "国際",
      "start_offset": 2,
      "end_offset": 4,
      "type": "word",
      "position": 1
    },
    {
      "token": "空港",
      "start_offset": 4,
      "end_offset": 6,
      "type": "word",
      "position": 2
    },
    {
      "token": "と",
      "start_offset": 6,
      "end_offset": 7,
      "type": "word",
      "position": 3
    },
    {
      "token": "アバクロンビー",
      "start_offset": 7,
      "end_offset": 14,
      "type": "word",
      "position": 4
    }
  ]
}

extended 모드

POST /kuromoji-mode-comparison/_analyze
{
  "tokenizer": "kuromoji_extended",
  "text": "関西国際空港とアバクロンビー"
}

{% include copy-curl.html %}

extended 모드에서는 알려진 복합어 関西国際空港이 search 모드와 동일하게 처리돼요. 미등록 외래어 アバクロンビー는 개별 문자(유니그램)로 분할되어 모든 문자가 색인되도록 해요:

{
  "tokens": [
    {
      "token": "関西",
      "start_offset": 0,
      "end_offset": 2,
      "type": "word",
      "position": 0
    },
    {
      "token": "関西国際空港",
      "start_offset": 0,
      "end_offset": 6,
      "type": "word",
      "position": 0,
      "positionLength": 3
    },
    {
      "token": "国際",
      "start_offset": 2,
      "end_offset": 4,
      "type": "word",
      "position": 1
    },
    {
      "token": "空港",
      "start_offset": 4,
      "end_offset": 6,
      "type": "word",
      "position": 2
    },
    {
      "token": "と",
      "start_offset": 6,
      "end_offset": 7,
      "type": "word",
      "position": 3
    },
    {
      "token": "ア",
      "start_offset": 7,
      "end_offset": 8,
      "type": "word",
      "position": 4
    },
    {
      "token": "バ",
      "start_offset": 8,
      "end_offset": 9,
      "type": "word",
      "position": 5
    },
    {
      "token": "ク",
      "start_offset": 9,
      "end_offset": 10,
      "type": "word",
      "position": 6
    },
    {
      "token": "ロ",
      "start_offset": 10,
      "end_offset": 11,
      "type": "word",
      "position": 7
    },
    {
      "token": "ン",
      "start_offset": 11,
      "end_offset": 12,
      "type": "word",
      "position": 8
    },
    {
      "token": "ビ",
      "start_offset": 12,
      "end_offset": 13,
      "type": "word",
      "position": 9
    },
    {
      "token": "ー",
      "start_offset": 13,
      "end_offset": 14,
      "type": "word",
      "position": 10
    }
  ]
}

예제: 사용자 사전

토크나이저 사전에 사용자 정의 복합어를 추가하면 하위 토큰으로 분할되는 방식을 제어할 수 있어요. 인라인 규칙에는 user_dictionary_rules를, 파일에는 user_dictionary를 사용하세요.

다음 예제는 東京スカイツリー(도쿄 스카이트리)를 하나의 명사로 등록해요:

PUT /kuromoji-user-dict-index
{
  "settings": {
    "analysis": {
      "tokenizer": {
        "kuromoji_with_user_dict": {
          "type": "kuromoji_tokenizer",
          "mode": "search",
          "user_dictionary_rules": [
            "東京スカイツリー,東京 スカイツリー,トウキョウ スカイツリー,カスタム名詞"
          ]
        }
      },
      "analyzer": {
        "user_dict_analyzer": {
          "tokenizer": "kuromoji_with_user_dict"
        }
      }
    }
  }
}

{% include copy-curl.html %}

user_dictionary_rules의 각 항목은 <text>,<subtokens>,<readings>,<part of speech> 순서로 쉼표로 구분된 네 개 필드를 가진 CSV 문자열이에요. 각 필드에 대한 설명은 다음 표를 참고하세요.

필드 설명
<text> 문서에 나타나는 그대로의 텍스트예요(예: 東京スカイツリー).
<subtokens> 분할에 사용되는 공백으로 구분된 하위 토큰이에요(예: 東京 スカイツリー).
<readings> 각 하위 토큰의 가타카나 발음이에요. 공백으로 구분돼요(예: トウキョウ スカイツリー).
<part of speech> 해당 단어에 할당되는 IPAdic 품사 태그예요(예: カスタム名詞).

분석기를 사용해 사용자 사전 항목을 테스트해 보세요:

POST /kuromoji-user-dict-index/_analyze
{
  "analyzer": "user_dict_analyzer",
  "text": "東京スカイツリーに登る"
}

{% include copy-curl.html %}

사용자 사전은 텍스트가 분할되는 방식을 제어해요. 규칙이 東京 スカイツリー를 두 개의 하위 토큰으로 정의하기 때문에, 토크나이저는 東京スカイツリー를 단일 토큰으로 유지하는 대신 東京과 スカイツリー를 별개의 토큰으로 출력해요:

{
  "tokens": [
    {
      "token": "東京",
      "start_offset": 0,
      "end_offset": 2,
      "type": "word",
      "position": 0
    },
    {
      "token": "スカイツリー",
      "start_offset": 2,
      "end_offset": 8,
      "type": "word",
      "position": 1
    },
    {
      "token": "に",
      "start_offset": 8,
      "end_offset": 9,
      "type": "word",
      "position": 2
    },
    {
      "token": "登る",
      "start_offset": 9,
      "end_offset": 11,
      "type": "word",
      "position": 3
    }
  ]
}

東京スカイツリー를 단일 토큰으로 유지하려면 규칙에서 이를 하나의 하위 토큰으로 정의하세요:

"東京スカイツリー,東京スカイツリー,トウキョウスカイツリー,カスタム名詞"

관련 문서

더 알아보기 (Learn more)