2011-07-27 5 views
2

NLTK 용 분류 된 XML 코퍼스 판독기를 작성한 사람이 있습니까?NLTK에 분류 된 XML 코퍼스 판독기가있는 사람이 있습니까?

저는 Annotated NYTimes corpus로 작업하고 있습니다. 그것은 XML 코퍼스입니다. XMLCorpusReader으로 파일을 읽을 수 있지만 NLTK의 카테고리 기능 중 일부를 사용하고 싶습니다. NLTK 리더를 서브 클래 싱하기위한 nice tutorial이 있습니다. 내가이 글을 쓰고 누군가가 이미 이것을했다면 시간을 절약하기를 희망하고있다.

내가 작성한 것을 게시하지 않으면 알 수 있습니다.

답변

1

다음은 NLTK에 대한 분류 된 XML 코퍼스 리더입니다. this tutorial. 을 기반으로하여 New York Times Annotated Corpus와 같은 XML Corpora에서 NLTK의 범주 기반 기능을 사용할 수 있습니다.

콜이 파일 CategorizedXMLCorpusReader.py 등이 가져 : 그런 다음 다른 NLTK 리더처럼 이것을 사용할 수 있습니다

import imp                                                      
CatXMLReader = imp.load_source('CategorizedXMLCorpusReader','PATH_TO_THIS_FILE/CategorizedXMLCorpusReader.py') 

. 예를 들어,

CatXMLReader = CatXMLReader.CategorizedXMLCorpusReader('.../nltk_data/corpora/nytimes', file_ids, cat_file='PATH_TO_CATEGORIES_FILE') 

나는 여전히 NLTK를 수정하고 있으므로 수정이나 제안을 환영합니다.

# Categorized XML Corpus Reader                                                 

from nltk.corpus.reader import CategorizedCorpusReader, XMLCorpusReader 
class CategorizedXMLCorpusReader(CategorizedCorpusReader, XMLCorpusReader): 
    def __init__(self, *args, **kwargs): 
     CategorizedCorpusReader.__init__(self, kwargs) 
     XMLCorpusReader.__init__(self, *args, **kwargs) 
    def _resolve(self, fileids, categories): 
     if fileids is not None and categories is not None: 
      raise ValueError('Specify fileids or categories, not both') 
     if categories is not None: 
      return self.fileids(categories) 
     else: 
      return fileids 

     # All of the following methods call the corresponding function in ChunkedCorpusReader                                  
     # with the value returned from _resolve(). We'll start with the plain text methods.                                  
    def raw(self, fileids=None, categories=None): 
     return XMLCorpusReader.raw(self, self._resolve(fileids, categories)) 

    def words(self, fileids=None, categories=None): 
     #return CategorizedCorpusReader.words(self, self._resolve(fileids, categories))                                   
     # Can I just concat words over each file in a file list?                                         
     words=[] 
     fileids = self._resolve(fileids, categories) 
     # XMLCorpusReader.words works on one file at a time. Concatenate them here.                                    
     for fileid in fileids: 
      words+=XMLCorpusReader.words(self, fileid) 
     return words 

    # This returns a string of the text of the XML docs without any markup                                      
    def text(self, fileids=None, categories=None): 
     fileids = self._resolve(fileids, categories) 
     text = "" 
     for fileid in fileids: 
      for i in self.xml(fileid).getiterator(): 
       if i.text: 
        text += i.text 
     return text 

    # This returns all text for a specified xml field                                            
    def fieldtext(self, fileids=None, categories=None): 
     # NEEDS TO BE WRITTEN                                                  
     return 

    def sents(self, fileids=None, categories=None): 
     #return CategorizedCorpusReader.sents(self, self._resolve(fileids, categories))                                   
     text = self.words(fileids, categories) 
     sents=nltk.PunktSentenceTokenizer().tokenize(text) 
     return sents 

    def paras(self, fileids=None, categories=None): 
     return CategorizedCorpusReader.paras(self, self._resolve(fileids, categories)) 
0

죄송 NAD, 그러나 새로운 질문으로 게시는이 코드를 논의하기 위해 찾을 수있는 유일한 방법이었다. words() 메서드를 사용하여 범주를 사용할 때도 작은 버그를 발견했습니다. 여기 : https://github.com/nltk/nltk/issues/250#issuecomment-5273102

나에게이 문제가 발생 했습니까? 또한 범주를 작동시킬 수있는 추가 수정 작업을 수행 했습니까? 내 전자 메일은 내 프로필 페이지에 있습니다. 꺼져서 얘기하고 싶다면 :-)

관련 문제