-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworldfrwqtoscv.py
More file actions
33 lines (26 loc) · 1.06 KB
/
Copy pathworldfrwqtoscv.py
File metadata and controls
33 lines (26 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import pandas as pd
from gensim.models import LdaModel
from gensim.corpora import Dictionary
# Load the LDA model and dictionary (adjust paths based on your files)
lda_model = LdaModel.load('/Users/nomantahir/Desktop/ve/venv/lda_model_best.model')
dictionary = Dictionary.load('/Users/nomantahir/Desktop/ve/venv/dictionary_best.gensim')
# Define the number of top words to extract per topic
num_words = 30
# Prepare a list to hold the data for the CSV
data = []
# Extract topics and word frequencies
for topic_id in range(lda_model.num_topics):
# Get the top words and their probabilities for this topic
top_words = lda_model.show_topic(topic_id, num_words)
for word, prob in top_words:
data.append({
'topic': topic_id,
'word': word,
'frequency': prob
})
# Convert the data to a DataFrame
df = pd.DataFrame(data)
# Save the DataFrame to a CSV file
csv_output_path = '/Users/nomantahir/Desktop/ve/venv/word_frequencies.csv'
df.to_csv(csv_output_path, index=False)
print(f"Word frequencies saved to {csv_output_path}")