2017-04-13 15 views
0

저는 현재 Brown Corpus와 협력 중이며 약간의 문제가 있습니다. 토큰 화 기능을 적용하려면 먼저 Brown Corpus를 문장으로 작성해야합니다.단어를 문장 문자열로 변환하는 방법 - 텍스트 분류

[('pos', 'The Fulton County Grand Jury said Friday an investigation of Atlanta's recent primary election ....'), ('pos', The jury further said in term-end presentments that the City...)] 
:

[('pos', 
    ['The', 
    'Fulton', 
    'County', 
    'Grand', 
    'Jury', 
    'said', 
    'Friday', 
    'an', 
    'investigation', 
    'of', 
    "Atlanta's", 
    'recent', 
    'primary', 
    'election', 
    'produced', 
    '``', 
    'no', 
    'evidence', 
    "''", 
    'that', 
    'any', 
    'irregularities', 
    'took', 
    'place', 
    '.']), 
('pos', 
    ['The', 
    'jury', 
    'further', 
    'said', 
    'in', 
    'term-end', 
    'presentments', 
    'that', 
    'the', 
    'City', 
    'Executive', 
    'Committee', 
    ',', 
    'which', 
    'had', 
    'over-all', 
    'charge', 
    'of', 
    'the', 
    'election', 
    ',', 
    '``', 
    'deserves', 
    'the', 
    'praise', 
    'and', 
    'thanks', 
    'of', 
    'the', 
    'City', 
    'of', 
    'Atlanta', 
    "''", 
    'for', 
    'the', 
    'manner', 
    'in', 
    'which', 
    'the', 
    'election', 
    'was', 
    'conducted', 
    '.']) 

내가 필요로하는처럼 보이는 무언가이다 :

from nltk.corpus import brown 
import nltk 


target_text = [s for s in brown.fileids() 
        if s.startswith('ca01') or s.startswith('ca02')] 

data = [] 

total_text = [s for s in brown.fileids() 
        if s.startswith('ca01') or s.startswith('ca02') or s.startswith('cp01') or s.startswith('cp02')] 


for text in total_text: 

    if text in target_text: 
     tag = "pos" 
    else: 
     tag = "neg" 
    words=list(brown.sents(total_text))  
    data.extend([(tag, word) for word in words]) 

data 

나는이 작업을 수행 할 때, 나는 다음과 같다 데이터를 얻을 : 이것은 내가 지금까지 무엇을 가지고

이 문제를 해결할 방법이 있습니까? 이 프로젝트는 예상보다 오래 걸립니다.

답변

1

에 따르면 .sents 메서드는 문자열 (단어) 목록 (문장)의 목록 (문서)을 반환합니다. 전화에서 아무 것도 잘못하지 않았습니다. .

문장을 다시 구성하려면 문장을 공백으로 결합 해보십시오. 그러나 이것은 정말 때문에 문장 부호에 작동하지 않습니다

data.extend([(tag, ' '.join(word)) for word in words]) 

당신은 다음과 같은 것을 얻을 것이다 :

'the', 
'election', 
',', 
'``', 
'deserves', 
'the', 

하는지도 :

the election , `` deserves the 

이 때문에 참여하지 않습니다 구두점을 알아라. nltk에 구두점 인식 포매터가 포함되어 있습니까?