2017-04-13 3 views
0

現在、私はBrown Corpusと共同で作業しており、若干問題があります。トークン化機能を適用するには、まずブラウンコーパスを文章にする必要があります。これは私がこれまで持っているものです。単語を文章に変換する方法 - テキスト分類

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 

私はこれを行うと、私は次のようになり、データ取得:私は必要なもの

[('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', 
    '.']) 

は、のようなものです:

[('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...)] 

これを修正する方法はありますか?このプロジェクトは、私が予想していたよりも長い時間がかかります。

答えて

1

the docs,によると、.sentsメソッドは、文字列(単語)のリスト(文章)のリスト(文書)を返します。 。

文章を再構成する場合は、文章をスペースで結合してみてください。しかし、これは実際に起因する句読点に動作しません。

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

あなたはこのようなもの買ってあげる:にマップ

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

the election , `` deserves the 

があるためではないん参加します句読点について知っている。 nltkには句読点対応のフォーマッタが含まれていますか?

関連する問題