OpenNLP (Open Natural Language Processing) 기능 라이브러리와 인터페이스하는 라이브러리. 모든 기능이 아직 구현되는 것은 아닙니다.
추가 정보/문서 :
Marginalia의 소스를 읽으십시오
[clojure-opennlp " 0.5.0 " ] ; ; uses Opennlp 1.9.0
Clojure-Opennlp는 Clojure 1.5+와 함께 작동합니다
( use 'clojure.pprint) ; just for this documentation
( use 'opennlp.nlp)
( use 'opennlp.treebank) ; treebank chunking, parsing and linking lives here
모델 파일을 사용하여 처리 기능을 만들어야합니다. 루트 프로젝트 디렉토리에서 실행중인 것으로 가정합니다. OpenNLP 프로젝트에서 http://opennlp.sourceforge.net/models-1.5에서 모델 파일을 다운로드 할 수도 있습니다.
( def get-sentences ( make-sentence-detector " models/en-sent.bin " ))
( def tokenize ( make-tokenizer " models/en-token.bin " ))
( def detokenize ( make-detokenizer " models/english-detokenizer.xml " ))
( def pos-tag ( make-pos-tagger " models/en-pos-maxent.bin " ))
( def name-find ( make-name-finder " models/namefind/en-ner-person.bin " ))
( def chunker ( make-treebank-chunker " models/en-chunker.bin " ))
도구 제작자는 다중 방법이므로 파일 이름 대신 모델을 사용하여 도구를 만들 수도 있습니다 (SRC/OpenNLP/Tools.clj의 교육 도구를 사용하여 모델을 만들 수 있습니다) :
( def tokenize ( make-tokenizer my-tokenizer-model)) ; ; etc, etc
그런 다음 만든 기능을 사용하여 텍스트에서 작업을 수행하십시오.
문장 감지 :
( pprint ( get-sentences " First sentence. Second sentence? Here is another one. And so on and so forth - you get the idea... " ))
[ " First sentence. " , " Second sentence? " , " Here is another one. " ,
" And so on and so forth - you get the idea... " ]
토큰 화 :
( pprint ( tokenize " Mr. Smith gave a car to his son on Friday " ))
[ " Mr. " , " Smith " , " gave " , " a " , " car " , " to " , " his " , " son " , " on " ,
" Friday " ]
detokenizing :
( detokenize [ " Mr. " , " Smith " , " gave " , " a " , " car " , " to " , " his " , " son " , " on " , " Friday " ])
" Mr. Smith gave a car to his son on Friday. "
이상적으로, s == (detokenize (tokenize s)), detokenization 모델 XML 파일은 진행중인 작업입니다. 영어로 제대로 제대로하지 않는 것이 있으면 알려주십시오.
부품 태깅 :
( pprint ( pos-tag ( tokenize " Mr. Smith gave a car to his son on Friday. " )))
([ " Mr. " " NNP " ]
[ " Smith " " NNP " ]
[ " gave " " VBD " ]
[ " a " " DT " ]
[ " car " " NN " ]
[ " to " " TO " ]
[ " his " " PRP$ " ]
[ " son " " NN " ]
[ " on " " IN " ]
[ " Friday. " " NNP " ])
이름 찾기 :
( name-find ( tokenize " My name is Lee, not John. " ))
( "Lee" " John " )
TreeBank-Chunking은 POS 태그 문장에서 문구를 분할 및 태그로 표시합니다. 주목할만한 차이점은 다음과 같이 문구 및 : 태그 키를 사용하여 스트러크 목록을 반환한다는 것입니다.
( pprint ( chunker ( pos-tag ( tokenize " The override system is meant to deactivate the accelerator when the brake pedal is pressed. " ))))
({ :phrase [ " The " " override " " system " ], :tag " NP " }
{ :phrase [ " is " " meant " " to " " deactivate " ], :tag " VP " }
{ :phrase [ " the " " accelerator " ], :tag " NP " }
{ :phrase [ " when " ], :tag " ADVP " }
{ :phrase [ " the " " brake " " pedal " ], :tag " NP " }
{ :phrase [ " is " " pressed " ], :tag " VP " })
문구 만 :
( phrases ( chunker ( pos-tag ( tokenize " The override system is meant to deactivate the accelerator when the brake pedal is pressed. " ))))
([ " The " " override " " system " ] [ " is " " meant " " to " " deactivate " ] [ " the " " accelerator " ] [ " when " ] [ " the " " brake " " pedal " ] [ " is " " pressed " ])
그리고 단지 문자열로 :
( phrase-strings ( chunker ( pos-tag ( tokenize " The override system is meant to deactivate the accelerator when the brake pedal is pressed. " ))))
( "The override system " " is meant to deactivate " " the accelerator " " when " " the brake pedal " " is pressed " )
문서 분류 :
더 나은 사용 예제는 OpenNlp.test.tools.train을 참조하십시오.
( def doccat ( make-document-categorizer " my-doccat-model " ))
( doccat " This is some good text " )
" Happy "
주어진 작업에 대한 확률 OpenNLP 공급품은 해당되는 경우 결과에 대한 메타 데이터로 제공됩니다.
( meta ( get-sentences " This is a sentence. This is also one. " ))
{ :probabilities ( 0.9999054310803004 0.9941126097177366 )}
( meta ( tokenize " This is a sentence. " ))
{ :probabilities ( 1.0 1.0 1.0 0.9956236737394807 1.0 )}
( meta ( pos-tag [ " This " " is " " a " " sentence " " . " ]))
{ :probabilities ( 0.9649410482478001 0.9982592902509803 0.9967282012835504 0.9952498677248117 0.9862225658078769 )}
( meta ( chunker ( pos-tag [ " This " " is " " a " " sentence " " . " ])))
{ :probabilities ( 0.9941248001899835 0.9878092935921453 0.9986106511439116 0.9972975733070356 0.9906377695586069 )}
( meta ( name-find [ " My " " name " " is " " John " ]))
{ :probabilities ( 0.9996272005494383 0.999999997485361 0.9999948113868132 0.9982291838206192 )}
pos-tagger 및 treebank-parser의 경우 opennlp.nlp/*beam-size*
(기본값은 3)를 다시 반짝 반응 할 수 있습니다.
( binding [*beam-size* 1 ]
( def pos-tag ( make-pos-tagger " models/en-pos-maxent.bin " )))
TreeBank-Parser의 경우 opennlp.treebank/*advance-percentage*
(기본값은 0.95)를 다시 반게 할 수 있습니다.
( binding [*advance-percentage* 0.80 ]
( def parser ( make-treebank-parser " parser-model/en-parser-chunking.bin " )))
참고 : TreeBank Parsing은 메모리 집약적이므로 JVM에 충분한 양의 메모리를 사용할 수 있는지 확인하십시오 (-xmx512m와 같은 사용). TreeBank 파서를 사용할 때 힙 공간이 부족합니다.
TreeBank Parsing은 얼마나 복잡한 지에 따라 자체 섹션을 얻습니다.
참고 TreeBank-Parser 모델 중 어느 것도 GIT Repo에 포함되지 않으므로 OpenNLP 프로젝트와 별도로 다운로드해야합니다.
만들기 :
( def treebank-parser ( make-treebank-parser " parser-model/en-parser-chunking.bin " ))
TreeBank-Parser를 사용하려면 Whitespace로 분리 된 토큰으로 문장 배열을 전달하십시오 (바람직하게는 토큰 화를 사용).
( treebank-parser [ " This is a sentence . " ])
[ " (TOP (S (NP (DT This)) (VP (VBZ is) (NP (DT a) (NN sentence))) (. .))) " ]
TreeBank-Parser 문자열을 Clojure가 수행하기 쉬운 것으로 변환하려면 (Make-Tree ...) 기능을 사용하십시오.
( make-tree ( first ( treebank-parser [ " This is a sentence . " ])))
{ :chunk { :chunk ({ :chunk { :chunk " This " , :tag DT}, :tag NP} { :chunk ({ :chunk " is " , :tag VBZ} { :chunk ({ :chunk " a " , :tag DT} { :chunk " sentence " , :tag NN}), :tag NP}), :tag VP} { :chunk " . " , :tag .}), :tag S}, :tag TOP}
다음은 데이터를 조금 더 읽기 쉬운 형식으로 나누는 다음과 같습니다.
{ :tag TOP
:chunk { :tag S
:chunk ({ :tag NP
:chunk { :tag DT
:chunk " This " }}
{ :tag VP
:chunk ({ :tag VBZ
:chunk " is " }
{ :tag NP
:chunk ({ :tag DT
:chunk " a " }
{ :tag NN
:chunk " sentence " })})}
{ :tag .
:chunk " . " })}}
바라건대 그것은 조금 더 명확하고 중첩 된지도가되기를 바랍니다. 다른 사람 이이 정보를 표현하는 더 나은 방법에 대해 제안을받는 경우 이메일이나 패치를 보내 주시기 바랍니다.
이 시점에서 TreeBank 파싱은 베타 베타로 간주됩니다.
( use 'opennlp.tools.filters)
( pprint ( nouns ( pos-tag ( tokenize " Mr. Smith gave a car to his son on Friday. " ))))
([ " Mr. " " NNP " ]
[ " Smith " " NNP " ]
[ " car " " NN " ]
[ " son " " NN " ]
[ " Friday " " NNP " ])
( pprint ( verbs ( pos-tag ( tokenize " Mr. Smith gave a car to his son on Friday. " ))))
([ " gave " " VBD " ])
( use 'opennlp.tools.filters)
( pprint ( noun-phrases ( chunker ( pos-tag ( tokenize " The override system is meant to deactivate the accelerator when the brake pedal is pressed " )))))
({ :phrase [ " The " " override " " system " ], :tag " NP " }
{ :phrase [ " the " " accelerator " ], :tag " NP " }
{ :phrase [ " the " " brake " " pedal " ], :tag " NP " })
( pos-filter determiners #"^DT" )
#'user/determiners
( doc determiners)
-------------------------
user/determiners
([elements__52__auto__])
Given a list of pos-tagged elements, return only the determiners in a list.
( pprint ( determiners ( pos-tag ( tokenize " Mr. Smith gave a car to his son on Friday. " ))))
([ " a " " DT " ])
(청크 필터 ...)를 사용하여 TreeBank-Chunk 필터를 만들 수도 있습니다.
( chunk-filter fragments #"^FRAG$" )
( doc fragments)
-------------------------
opennlp.nlp/fragments
([elements__178__auto__])
Given a list of treebank-chunked elements, return only the fragments in a list.
원하는 작업에 따라 태깅 방법에 따라 게으르는 데 도움이되는 몇 가지 방법이 있습니다.
#'opennlp.tools.lazy/lazy-get-sentences
#'opennlp.tools.lazy/lazy-tokenize
#'opennlp.tools.lazy/lazy-tag
#'opennlp.tools.lazy/lazy-chunk
#'opennlp.tools.lazy/sentence-seq
사용 방법은 다음과 같습니다.
( use 'opennlp.nlp)
( use 'opennlp.treebank)
( use 'opennlp.tools.lazy)
( def get-sentences ( make-sentence-detector " models/en-sent.bin " ))
( def tokenize ( make-tokenizer " models/en-token.bin " ))
( def pos-tag ( make-pos-tagger " models/en-pos-maxent.bin " ))
( def chunker ( make-treebank-chunker " models/en-chunker.bin " ))
( lazy-get-sentences [ " This body of text has three sentences. This is the first. This is the third. " " This body has only two. Here's the last one. " ] get-sentences)
; ; will lazily return:
([ " This body of text has three sentences. " " This is the first. " " This is the third. " ] [ " This body has only two. " " Here's the last one. " ])
( lazy-tokenize [ " This is a sentence. " " This is another sentence. " " This is the third. " ] tokenize)
; ; will lazily return:
([ " This " " is " " a " " sentence " " . " ] [ " This " " is " " another " " sentence " " . " ] [ " This " " is " " the " " third " " . " ])
( lazy-tag [ " This is a sentence. " " This is another sentence. " ] tokenize pos-tag)
; ; will lazily return:
(([ " This " " DT " ] [ " is " " VBZ " ] [ " a " " DT " ] [ " sentence " " NN " ] [ " . " " . " ]) ([ " This " " DT " ] [ " is " " VBZ " ] [ " another " " DT " ] [ " sentence " " NN " ] [ " . " " . " ]))
( lazy-chunk [ " This is a sentence. " " This is another sentence. " ] tokenize pos-tag chunker)
; ; will lazily return:
(({ :phrase [ " This " ], :tag " NP " } { :phrase [ " is " ], :tag " VP " } { :phrase [ " a " " sentence " ], :tag " NP " }) ({ :phrase [ " This " ], :tag " NP " } { :phrase [ " is " ], :tag " VP " } { :phrase [ " another " " sentence " ], :tag " NP " }))
게으른 기능을 자유롭게 사용하십시오. 그러나 여전히 레이아웃에 100% 설정되지 않으므로 향후 변경 될 수 있습니다. (아마도 일련의 문장 대신에 그들을 연쇄시키는 것처럼 보일 수 있습니다 (게으른 태그 (게으른 태그 (게으른 고정제 (게으른-get 서센트 ...)))).
OpenNlp.tools.lazy/sentence-seq를 사용하여 파일에서 게으른 문장 순서를 생성합니다.
( with-open [rdr ( clojure.java.io/reader " /tmp/bigfile " )]
( let [sentences ( sentence-seq rdr get-sentences)]
; ; process your lazy seq of sentences however you desire
( println " first 5 sentences: " )
( clojure.pprint/pprint ( take 5 sentences))))
각 도구에 대한 교육 모델을 허용하는 코드가 있습니다. Training.Markdown의 문서를 참조하십시오
저작권 (C) 2010 Matthew Lee Hinman
Clojure가 사용하는 Eclipse Public License에 따라 배포됩니다. 복사 파일을 참조하십시오.