파이썬-청크와 턱

청킹은 단어의 특성에 따라 유사한 단어를 함께 그룹화하는 프로세스입니다. 아래 예에서는 청크를 생성해야하는 문법을 정의합니다. 문법은 청크를 만들 때 따라야 할 명사 및 형용사 등과 같은 구문의 순서를 제안합니다. 청크의 그림 출력은 다음과 같습니다.

import nltk
sentence = [("The", "DT"), ("small", "JJ"), ("red", "JJ"),("flower", "NN"), 
("flew", "VBD"), ("through", "IN"),  ("the", "DT"), ("window", "NN")]
grammar = "NP: {
      
       ?
       
* }" cp = nltk.RegexpParser(grammar) result = cp.parse(sentence) print(result) result.draw()

When we run the above program we get the following output −

Changing the grammar, we get a different output as shown below.

import nltk
sentence = [("The", "DT"), ("small", "JJ"), ("red", "JJ"),("flower", "NN"),
 ("flew", "VBD"), ("through", "IN"),  ("the", "DT"), ("window", "NN")]
grammar = "NP: {
      
? * }" chunkprofile = nltk.RegexpParser(grammar) result = chunkprofile.parse(sentence) print(result) result.draw()

When we run the above program we get the following output −

Chinking

Chinking is the process of removing a sequence of tokens from a chunk. If the sequence of tokens appears in the middle of the chunk, these tokens are removed, leaving two chunks where they were already present.

import nltk
sentence = [("The", "DT"), ("small", "JJ"), ("red", "JJ"),("flower", "NN"), ("flew", "VBD"), ("through", "IN"),  ("the", "DT"), ("window", "NN")]
grammar = r"""
  NP:
    {<.*>+}         # Chunk everything
    }
      
       +{      # Chink sequences of JJ and NN
  """
chunkprofile = nltk.RegexpParser(grammar)
result = chunkprofile.parse(sentence) 
print(result)
result.draw()

      

When we run the above program, we get the following output −

As you can see the parts meeting the criteria in grammar are left out from the Noun phrases as separate chunks. This process of extracting text not in the required chunk is called chinking.