2016-07-08 2 views
1

NLTK를 사용하여 목록 요소에서 불용어를 제거하고 있습니다. 여기파이썬에서 NLTK를 사용하여 불용어 제거

dict1 = {} 
    for ctr,row in enumerate(cur.fetchall()): 
      list1 = [row[0],row[1],row[2],row[3],row[4]] 
      dict1[row[0]] = list1 
      print ctr+1,"\n",dict1[row[0]][2] 
      list2 = [w for w in dict1[row[0]][3] if not w in stopwords.words('english')] 
      print list2 

문제는이 만 중지 단어를 제거하지 내 코드하지만, 예를 들어, 즉에서 또한 제거하고 문자 단어 'orientation'에서 'i'를 선택하면 더 많은 불용어가 제거되고 list2에 단어 대신 문자가 저장됩니다. 즉 [ 'O', 'r', 'e', ​​'n', 'n', '', 'f', ''3 ',' ','r ','e ' ','n ','\ n ','\ n ','O ','r ','e ','n ','n ' ',' ',' ',' ',' ','r ','e ','r ','e ',' ','r ','p ' ...................... [ '오리엔테이션', '.............. ......

+1

먼저 단어를 토큰 화해보십시오. – galaxyan

+2

코드에 cur이 무엇입니까? 컨텍스트 코드를 더 게시 할 수 있습니까? –

답변

1

먼저 list1은 문자 배열이 아닌 단어 목록이어야합니다. 여기에 내가 활용할 수있는 코드 스 니펫 (snippet)을 줄 수 있습니다.

from nltk import word_tokenize 
from nltk.corpus import stopwords 

english_stopwords = stopwords.words('english') # get english stop words 

# test document 
document = '''A moody child and wildly wise 
Pursued the game with joyful eyes 
''' 

# first tokenize your document to a list of words 
words = word_tokenize(document) 
print(words) 

# the remove all stop words 
content = [w for w in words if w.lower() not in english_stopwords] 
print(content) 

출력은 다음과 같습니다

['A', 'moody', 'child', 'and', 'wildly', 'wise', 'Pursued', 'the', 'game', 'with', 'joyful', 'eyes'] 
['moody', 'child', 'wildly', 'wise', 'Pursued', 'game', 'joyful', 'eyes'] 
0

첫째, 목록 1의 시공은 나에게 조금 독특한 것입니다. 내가 더 파이썬 해결책이 있다고 생각 : 다음

list1 = row[:5] 

, dict1 사용하면 [3] 행을 액세스하는 이유가 [행 [0] [3]보다는 [3]를 직접 행?

마지막으로 행이 문자열 목록이라고 가정하고 행 [3]에서 list2를 생성하면 모든 단어가 아닌 모든 문자가 반복됩니다. 그래서 'i'와 'a'(및 다른 소수의 캐릭터)를 파싱하는 이유가 될 수 있습니다.

올바른 이해는 다음과 같습니다

list2 = [w for w in row[3].split(' ') if w not in stopwords] 

당신은 아마 공간의 주위에 떨어져 어떻게 든 문자열을 분할해야합니다. 그것이 당신에게 전체 단어보다는 개별 문자를 제공을 통해

'Hello, this is row3' 

순회

['Hello,', 'this', 'is', 'row3'] 

사람 : 그건 뭔가 걸립니다.

+0

TypeError : 'LazyCorpusLoader'유형의 인수를 반복 할 수 없습니다. –

관련 문제