import os
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation

#  Set Folder Path
folder_path = r"C:/Users/User/OneDrive/문서/corpus"

#  Read all .txt files
documents = []
filenames = []

for file in os.listdir(folder_path):
    if file.endswith(".txt"):
        filenames.append(file)
        with open(os.path.join(folder_path, file), 'r', encoding='utf-8', errors='ignore') as f:
            documents.append(f.read())

print("Files loaded:", filenames)

# Safety check
if not documents:
    print("No text files found in the folder.")
    exit()

#  Convert text into Document-Term Matrix
vectorizer = CountVectorizer(stop_words='english', lowercase=True)
dtm = vectorizer.fit_transform(documents)
print("DTM shape:", dtm.shape)

#  Set number of topics
k = 3

#  Apply LDA
lda_model = LatentDirichletAllocation(n_components=k, random_state=42)
lda_model.fit(dtm)

#  Print number of topics
print("Number of topics:", k)

#  Display top words per topic
words = vectorizer.get_feature_names_out()
for i, topic in enumerate(lda_model.components_):
    print(f"\nTopic {i+1}:")
    top_words = [words[j] for j in topic.argsort()[:-11:-1]]
    print(top_words)

#  Document-topic distribution
doc_topic_dist = lda_model.transform(dtm)
print("\nDocument-topic Distribution:")
for i, dist in enumerate(doc_topic_dist):
    print(f"{filenames[i]} -> {dist}")
#pip install scikit-learn numpy pandas