Files
2024-04-29 16:01:03 +02:00

49 lines
2.1 KiB
Python

import pandas as pd
from tqdm import tqdm
from bertopic import BERTopic
def assign_highest_n_contribution(origin_df, model, highest_n):
topics_df = pd.DataFrame(columns=['topic_contribution', 'topic_keywords', 'title', 'faculty', 'institute', 'chair'])
for index, row in tqdm(origin_df.iterrows(), total = origin_df.shape[0]):
title = row["title"]
topics, similarities = model.find_topics(title, top_n=highest_n)
for idx,tupel in enumerate(zip(topics, similarities)):
topic_num, similarity = tupel
topic_keywords = ", ".join([word[0] for word in model.get_topic(topic_num)])
topics_df.loc[len(topics_df)] = {"dominant_topic": topic_num,
"topic_contribution": round(similarity,4),
"topic_keywords": topic_keywords,
"title": row["title"],
"institute": row["institute"],
"faculty": row["faculty"],
"chair": row["chair"]}
topics_df.reset_index(drop=True, inplace=True)
return topics_df
model = BERTopic.load("model_title_orgs_600.bertopic", embedding_model="paraphrase-multilingual-MiniLM-L12-v2")
text = """Der SFB „Supramolekulare Chemie an Proteinen" verschränkt Forscherteams aus den Fakultäten Chemie, Biologie und Medizin. Die beteiligten Chemiker nutzen ihr Wissen und die Methoden der Supramolekularen Chemie zur Konstruktion neuer Greifwerkzeuge für Eiweißmoleküle. Mit ihrer Hilfe klären die Biologen bisher unverstandene biochemische Mechanismen auf, und Mediziner entwickeln neue Ansatzpunkte zur Bekämpfung von Krankheiten. Der neue Sonderforschungsbereich (SFB 1093) befindet sich aktuell in der zweiten Förderperiode (Sprecher: Prof. Thomas Schrader)"""
print("-")
print(model.find_topics(text, top_n=10))
print("-")
print(model.get_topic(17))
print("-")
df = pd.read_csv("pubs_title_orgs.csv", encoding='utf-8', delimiter=';')
print(df.info())
print("-")
doc_topics_df = assign_highest_n_contribution(df, model, 10)
print(doc_topics_df.tolist())
print("-")
print(doc_topics_df.groupby("faculty").size())