저는 파이썬 프로그래밍에 익숙하지 않습니다. 지금은 텍스트 파일에서 자연어 처리를하고 있습니다. 문제는 제가 약 200 개의 텍스트 파일을 가지고있어서 각 파일을 개별적으로로드하고 동일한 방법을 적용하기가 매우 어렵다는 것입니다.파이썬을 사용하여 여러 텍스트 파일에 동일한 알고리즘을로드하고 적용하는 방법
import nltk
from nltk.collocations import *
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk import FreqDist
with open("c:/users/user/desktop/datascience/sotu/stopwords.txt", 'r') as sww:
sw = sww.read()
**with open("c:/users/user/desktop/datascience/sotu/a41.txt", 'r') as a411:
a41 = a411.read()
a41c=word_tokenize(str(a41))
a41c = [w for w in a41c if not w in sw]**
그래서 내가 여러 파일에이 방법을 적용 할 :
여기 내 프로그램입니다. 한 단계에서 모든 파일을로드하고 동일한 방법을 적용 할 수있는 방법이 있습니까? 나는 이것을 시도했지만 작동하지 않았다 :
import os
import glob
import nltk
from nltk.collocations import *
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from nltk import FreqDist
with open("c:/users/user/desktop/datascience/sotu/stopwords.txt", 'r') as sww:
sw = sww.read()
for filename in glob.glob(os.path.join("c:/users/user/desktop/DataScience/sotu/",'*.txt')):
filename=word_tokenize(str(filename))
filename = [w for w in filename if not w in sw]
xqc=FreqDist(filename)
이 도움을 주시기 바랍니다.
"...하지만 작동하지 않습니까?" 어떤 오류 또는 무엇인가? 'filenamec'이란 무엇입니까? – Marcin
아무 일도 없었어요. 그리고 내가 파일 이름을 인쇄했을 때 ... 'filename'이 정의되어 있지 않습니다. – Learner27
무엇이'filenamec'입니까? 또한 파일의 확장자는'* .text'이거나 첫번째 예제에서와 같은'* .txt'입니까? – Marcin