사용자 지정 단어 제거, 형태소 분석 (단어의 루트 형식 가져 오기) 및 tf-idf를 수행하는 함수를 작성 중입니다.
함수에 대한 내 입력 데이터는 목록입니다. 개별 목록에서 사용자 지정 단어 제거를 시도하면 작동하지만 함수에서 결합하면 속성 오류가 발생합니다.
AttributeError : 'list'개체에 'lower'속성이 없습니다.
내 코드는 다음과 같습니다.
def tfidf_kw(K):
# Select docs in cluster K
docs = np.array(mydata2)[km_r3.labels_==K]
ps= PorterStemmer()
stem_docs = []
for doc in docs:
keep_tokens = []
for token in doc.split(' '):
#custom stopword removal
my_list = ['model', 'models', 'modeling', 'modelling', 'python',
'train','training', 'trains', 'trained','test','testing', 'tests','tested']
token = [sub_token for sub_token in list(doc) if sub_token not in my_list]
stem_token=ps.stem(token)
keep_tokens.append(stem_token)
keep_tokens =' '.join(keep_tokens)
stem_docs.append(keep_tokens)
return(keep_tokens)
추가 코드는 작동하는 tf-idf 용입니다. 내가 뭘 잘못하고 있는지 이해하려면 여기에 도움이 필요합니까?
token = [sub_token for sub_token in list(doc) if sub_token not in my_list]
다음은 전체 오류입니다.
AttributeError Traceback (most recent call last)
<ipython-input-154-528a540678b0> in <module>
49 #return(sorted_df)
50
---> 51 tfidf_kw(0)
<ipython-input-154-528a540678b0> in tfidf_kw(K)
20
21
---> 22 stem_token=ps.stem(token)
23 keep_tokens.append(stem_token)
24
~/opt/anaconda3/lib/python3.8/site-packages/nltk/stem/porter.py in stem(self, word)
650
651 def stem(self, word):
--> 652 stem = word.lower()
653
654 if self.mode == self.NLTK_EXTENSIONS and word in self.pool:
AttributeError: 'list' object has no attribute 'lower'
51 행에서라고 표시된 tfidf_kw(0)
곳에서 k = 0에 대한 함수를 확인하고 있습니다.
이 ps.stem
메서드는 인수로 단일 단어 (문자열)를 예상하지만 문자열 목록을 전달합니다.
이미 for token in doc.split(' ')
루프 안에 있기 때문에 목록 이해력을 [... for sub_token in list(doc) ...]
추가 로 사용하는 것이 이해가되지 않는 것 같습니다 .
목표가에있는 토큰을 건너 뛰는 것이라면 my_list
아마도 다음 for token in doc.split(' ')
과 같이 루프 를 작성하고 싶을 것입니다 .
for token in doc.split(' '):
my_list = ['model', 'models', 'modeling', 'modelling', 'python',
'train','training', 'trains', 'trained','test','testing', 'tests','tested']
if token in my_list:
continue
stem_token=ps.stem(token)
keep_tokens.append(stem_token)
경우 여기 token
에있는 단어 중 하나 my_list
의 continue
문은 현재 반복의 나머지 부분을 건너 뛰고 루프는 다음에 계속 token
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다