2017-03-18 1 views
0
import tokenize 
tags = [ 
    "python, tools", 
    "linux, tools, ubuntu", 
    "distributed systems, linux, networking, tools", 
] 
from sklearn.feature_extraction.text import CountVectorizer 
vec = CountVectorizer(tokenizer=tokenize) 
data = vec.fit_transform(tags).toarray() 
print data 

텍스트를 벡터로 변환하려고합니다. 하지만 다음 오류가 발생했습니다.파이썬에서 텍스트를 벡터로 변환

Traceback (most recent call last): 
File "<stdin>", line 1, in <module> 
File "/usr/lib/pymodules/python2.7/sklearn/feature_extraction/text.py", line 398, in fit_transform 
term_count_current = Counter(analyze(doc)) 
File "/usr/lib/pymodules/python2.7/sklearn/feature_extraction/text.py", line 313, in <lambda> 
tokenize(preprocess(self.decode(doc))), stop_words) 
TypeError: 'module' object is not callable 

다른 라이브러리도 가져 오려고했습니다. 그러나 아무것도 작동하지 않는 것 같습니다. 어떻게 해결할 수 있습니까?

+2

당신은 아마 토큰 화 수입에서'원하는 데 도움이 tokenize' –

+0

@MosesKoledoye 꽤 익숙하지 이 접근 방식으로, 주어진 데이터 세트에 대해'tokenize' 작업을 할 것인가? [docs] (https://docs.python.org/2/library/tokenize.html#tokenize.tokenize)는 tokenize에 대해 다음과 같이 말합니다. * 첫 번째 매개 변수 인 readline은 동일한 인터페이스를 제공하는 호출 가능 객체 여야합니다. 내장 된 파일 객체의 readline() 메소드 * 문자열 목록과 함께 작동합니까? 만약 당신이 처음에'tokenizer' 설정이 필요하다면 Idk? – patrick

+1

명시적인 토크 나이저없이 작동해야합니다. –

답변

0

정확히 nltk.org의 메인 페이지에서 주위 작품의 솔루션 더보기 :

>>> import nltk 
>>> sentence = """At eight o'clock on Thursday morning 
... Arthur didn't feel very good.""" 
>>> tokens = nltk.word_tokenize(sentence) 
>>> tokens 
['At', 'eight', "o'clock", 'on', 'Thursday', 'morning', 
'Arthur', 'did', "n't", 'feel', 'very', 'good', '.'] 

는 희망이

+0

** 입력 텍스트의 벡터 표현이 필요합니다. ** – Neenu

+0

이 웹 페이지에 대한 자습서가 있습니다. 작동하지 않을 경우 알려주십시오. http : // /scikit-learn.org/stable/modules/feature_extraction.html –

+0

@ 네누 만약 당신이 그 해답을 유용하게 찾았다면 친절하게 업 그레 이드하거나 대답을 수락하십시오. –