”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > 使用 Topc 进行主题建模:Dreyfus、AI 和 Wordclouds

使用 Topc 进行主题建模:Dreyfus、AI 和 Wordclouds

发布于2024-07-30
浏览:640

使用 Python 从 PDF 中提取见解:综合指南

该脚本演示了用于处理 PDF、提取文本、标记句子以及通过可视化执行主题建模的强大工作流程,专为高效且富有洞察力的分析而定制。

库概述

  • os:提供与操作系统交互的功能。
  • matplotlib.pyplot:用于在 Python 中创建静态、动画和交互式可视化。
  • nltk:自然语言工具包,一套用于自然语言处理的库和程序。
  • pandas:数据操作和分析库。
  • pdftotext:用于将 PDF 文档转换为纯文本的库。
  • re:提供正则表达式匹配操作。
  • seaborn:基于matplotlib的统计数据可视化库。
  • nltk.tokenize.sent_tokenize:NLTK 函数将字符串标记为句子。
  • top2vec:主题建模和语义搜索库。
  • wordcloud:用于从文本数据创建词云的库。

初始设置

导入模块

import os
import matplotlib.pyplot as plt
import nltk
import pandas as pd
import pdftotext
import re
import seaborn as sns
from nltk.tokenize import sent_tokenize
from top2vec import Top2Vec
from wordcloud import WordCloud
from cleantext import clean

接下来,确保下载 punkt tokenizer:

nltk.download('punkt')

文本规范化

def normalize_text(text):
    """Normalize text by removing special characters and extra spaces,
    and applying various other cleaning options."""

    # Apply the clean function with specified parameters
    cleaned_text = clean(
        text,
        fix_unicode=True,  # fix various unicode errors
        to_ascii=True,  # transliterate to closest ASCII representation
        lower=True,  # lowercase text
        no_line_breaks=False,  # fully strip line breaks as opposed to only normalizing them
        no_urls=True,  # replace all URLs with a special token
        no_emails=True,  # replace all email addresses with a special token
        no_phone_numbers=True,  # replace all phone numbers with a special token
        no_numbers=True,  # replace all numbers with a special token
        no_digits=True,  # replace all digits with a special token
        no_currency_symbols=True,  # replace all currency symbols with a special token
        no_punct=False,  # remove punctuations
        lang="en",  # set to 'de' for German special handling
    )

    # Further clean the text by removing any remaining special characters except word characters, whitespace, and periods/commas
    cleaned_text = re.sub(r"[^\w\s.,]", "", cleaned_text)
    # Replace multiple whitespace characters with a single space and strip leading/trailing spaces
    cleaned_text = re.sub(r"\s ", " ", cleaned_text).strip()

    return cleaned_text

PDF文本提取

def extract_text_from_pdf(pdf_path):
    with open(pdf_path, "rb") as f:
        pdf = pdftotext.PDF(f)
    all_text = "\n\n".join(pdf)
    return normalize_text(all_text)

句子标记化

def split_into_sentences(text):
    return sent_tokenize(text)

处理多个文件

def process_files(file_paths):
    authors, titles, all_sentences = [], [], []
    for file_path in file_paths:
        file_name = os.path.basename(file_path)
        parts = file_name.split(" - ", 2)
        if len(parts) != 3 or not file_name.endswith(".pdf"):
            print(f"Skipping file with incorrect format: {file_name}")
            continue

        year, author, title = parts
        author, title = author.strip(), title.replace(".pdf", "").strip()

        try:
            text = extract_text_from_pdf(file_path)
        except Exception as e:
            print(f"Error extracting text from {file_name}: {e}")
            continue

        sentences = split_into_sentences(text)
        authors.append(author)
        titles.append(title)
        all_sentences.extend(sentences)
        print(f"Number of sentences for {file_name}: {len(sentences)}")

    return authors, titles, all_sentences

将数据保存到 CSV

def save_data_to_csv(authors, titles, file_paths, output_file):
    texts = []
    for fp in file_paths:
        try:
            text = extract_text_from_pdf(fp)
            sentences = split_into_sentences(text)
            texts.append(" ".join(sentences))
        except Exception as e:
            print(f"Error processing file {fp}: {e}")
            texts.append("")

    data = pd.DataFrame({
        "Author": authors,
        "Title": titles,
        "Text": texts
    })
    data.to_csv(output_file, index=False, quoting=1, encoding='utf-8')
    print(f"Data has been written to {output_file}")

加载停用词

def load_stopwords(filepath):
    with open(filepath, "r") as f:
        stopwords = f.read().splitlines()
    additional_stopwords = ["able", "according", "act", "actually", "after", "again", "age", "agree", "al", "all", "already", "also", "am", "among", "an", "and", "another", "any", "appropriate", "are", "argue", "as", "at", "avoid", "based", "basic", "basis", "be", "been", "begin", "best", "book", "both", "build", "but", "by", "call", "can", "cant", "case", "cases", "claim", "claims", "class", "clear", "clearly", "cope", "could", "course", "data", "de", "deal", "dec", "did", "do", "doesnt", "done", "dont", "each", "early", "ed", "either", "end", "etc", "even", "ever", "every", "far", "feel", "few", "field", "find", "first", "follow", "follows", "for", "found", "free", "fri", "fully", "get", "had", "hand", "has", "have", "he", "help", "her", "here", "him", "his", "how", "however", "httpsabout", "ibid", "if", "im", "in", "is", "it", "its", "jstor", "june", "large", "lead", "least", "less", "like", "long", "look", "man", "many", "may", "me", "money", "more", "most", "move", "moves", "my", "neither", "net", "never", "new", "no", "nor", "not", "notes", "notion", "now", "of", "on", "once", "one", "ones", "only", "open", "or", "order", "orgterms", "other", "our", "out", "own", "paper", "past", "place", "plan", "play", "point", "pp", "precisely", "press", "put", "rather", "real", "require", "right", "risk", "role", "said", "same", "says", "search", "second", "see", "seem", "seems", "seen", "sees", "set", "shall", "she", "should", "show", "shows", "since", "so", "step", "strange", "style", "such", "suggests", "talk", "tell", "tells", "term", "terms", "than", "that", "the", "their", "them", "then", "there", "therefore", "these", "they", "this", "those", "three", "thus", "to", "todes", "together", "too", "tradition", "trans", "true", "try", "trying", "turn", "turns", "two", "up", "us", "use", "used", "uses", "using", "very", "view", "vol", "was", "way", "ways", "we", "web", "well", "were", "what", "when", "whether", "which", "who", "why", "with", "within", "works", "would", "years", "york", "you", "your", "suggests", "without"]
    stopwords.extend(additional_stopwords)
    return set(stopwords)

从主题中过滤停用词

def filter_stopwords_from_topics(topic_words, stopwords):
    filtered_topics = []
    for words in topic_words:
        filtered_topics.append([word for word in words if word.lower() not in stopwords])
    return filtered_topics

词云生成

def generate_wordcloud(topic_words, topic_num, palette='inferno'):
    colors = sns.color_palette(palette, n_colors=256).as_hex()
    def color_func(word, font_size, position, orientation, random_state=None, **kwargs):
        return colors[random_state.randint(0, len(colors) - 1)]

    wordcloud = WordCloud(width=800, height=400, background_color='black', color_func=color_func).generate(' '.join(topic_words))
    plt.figure(figsize=(10, 5))
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.axis('off')
    plt.title(f'Topic {topic_num} Word Cloud')
    plt.show()

主要执行

file_paths = [f"/home/roomal/Desktop/Dreyfus-Project/Dreyfus/{fname}" for fname in os.listdir("/home/roomal/Desktop/Dreyfus-Project/Dreyfus/") if fname.endswith(".pdf")]

authors, titles, all_sentences = process_files(file_paths)

output_file = "/home/roomal/Desktop/Dreyfus-Project/Dreyfus_Papers.csv"
save_data_to_csv(authors, titles, file_paths, output_file)

stopwords_filepath = "/home/roomal/Documents/Lists/stopwords.txt"
stopwords = load_stopwords(stopwords_filepath)

try:
    topic_model = Top2Vec(
        all_sentences,
        embedding_model="distiluse-base-multilingual-cased",
        speed="deep-learn",
        workers=6
    )
    print("Top2Vec model created successfully.")
except ValueError as e:
    print(f"Error initializing Top2Vec: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")

num_topics = topic_model.get_num_topics()
topic_words, word_scores, topic_nums = topic_model.get_topics(num_topics)
filtered_topic_words = filter_stopwords_from_topics(topic_words, stopwords)

for i, words in enumerate(filtered_topic_words):
    print(f"Topic {i}: {', '.join(words)}")

keywords = ["heidegger"]
topic_words, word_scores, topic_scores, topic_nums = topic_model.search_topics(keywords=keywords, num_topics=num_topics)
filtered

_search_topic_words = filter_stopwords_from_topics(topic_words, stopwords)

for i, words in enumerate(filtered_search_topic_words):
    generate_wordcloud(words, topic_nums[i])

for i in range(reduced_num_topics):
    topic_words = topic_model.topic_words_reduced[i]
    filtered_words = [word for word in topic_words if word.lower() not in stopwords]
    print(f"Reduced Topic {i}: {', '.join(filtered_words)}")
    generate_wordcloud(filtered_words, i)

Topic Wordcloud

减少主题数量

reduced_num_topics = 5
topic_mapping = topic_model.hierarchical_topic_reduction(num_topics=reduced_num_topics)

# Print reduced topics and generate word clouds
for i in range(reduced_num_topics):
    topic_words = topic_model.topic_words_reduced[i]
    filtered_words = [word for word in topic_words if word.lower() not in stopwords]
    print(f"Reduced Topic {i}: {', '.join(filtered_words)}")
    generate_wordcloud(filtered_words, i)

Hierarchical Topic Reduction Wordcloud

版本声明 本文转载于:https://dev.to/roomals/topic-modeling-with-top2vec-dreyfus-ai-and-wordclouds-1ggl?1如有侵犯,请联系[email protected]删除
最新教程 更多>
  • 如何在GO中有效初始化数组,类似于C ++的Memset?
    如何在GO中有效初始化数组,类似于C ++的Memset?
    在go中有等效的memset吗? 在C中,MEMSET函数允许具有特定值的数组的有效初始化。在GO中,尽管没有直接等效的词,但几种技术可以实现相似的结果。 最简单的方法是使用循环来设置每个元素对所需值的数组。 = v } } repoyed copy() int,v int){ ...
    编程 发布于2025-02-06
  • 为什么使用固定定位时,为什么具有100%网格板柱的网格超越身体?
    为什么使用固定定位时,为什么具有100%网格板柱的网格超越身体?
    网格超过身体,用100%grid-template-columns 问题:考虑以下CSS和HTML: position:fixed; grid-template-columns:40%60%; grid-gap:5px; 背景:#eee; 当位置未固定时,网格将正确显示。但是,当...
    编程 发布于2025-02-06
  • React中的异步使用效应功能是否需要清理功能?
    React中的异步使用效应功能是否需要清理功能?
    async functions for async functions:导航清理困境在将useeffect hook与async函数中使用时,开发人员可能会遇到以下以下的访问警告:此警告源于在组件未填充时清理async函数使用的资源的需求。没有清理功能,在删除组件后可能会继续进行长期运行的异步任...
    编程 发布于2025-02-06
  • 如何在Java列表中有效计算元素的发生?
    如何在Java列表中有效计算元素的发生?
    计数列表中的元素出现在列表 中,在java编程中,列举列表中列举元素出现的任务来自列表。为此,收集框架提供了全面的工具套件。在这种情况下,Batocurrences变量将保持值3,代表动物列表中的“ BAT”出现的数量。 &&& [此方法是简单的,可以得出准确的结果,使其成为计算列表中元素出现的理...
    编程 发布于2025-02-06
  • 深副本与浅副本与Java中的克隆:有什么区别,我什么时候应该使用?
    深副本与浅副本与Java中的克隆:有什么区别,我什么时候应该使用?
    复制值与复制对象在讨论复制类型之前,对于区分 copy values 和复制对象: 复制一个值:复制参考类型的值涉及分配对象引用,类似于复制integer。 && && && && &&&华复制一个对象:创建一个具有自己身份的新对象,涉及使用“新”显式或隐式。对象的深拷贝 浅复制:一个新对象的值与...
    编程 发布于2025-02-06
  • JavaScript的伴侣
    JavaScript的伴侣
    [2 了解JavaScript承诺 承诺是JavaScript中的一个强大功能,可以简化处理异步操作的处理。它们提供了一种更清洁,更直观的方式来处理异步代码,避免了诸如“回调地狱”之类的问题。 什么是诺言? 是一个代表异步操作的最终完成(或失败)及其结果值的对象。它使...
    编程 发布于2025-02-06
  • 如何在整个HTML文档中设计特定元素类型的第一个实例?
    如何在整个HTML文档中设计特定元素类型的第一个实例?
    [2单独使用CSS,整个HTML文档可能是一个挑战。 the:第一型伪级仅限于与其父元素中类型的第一个元素匹配。 以下CSS将使用添加的类样式的第一个段落: }
    编程 发布于2025-02-06
  • 如何使用Flexbox将元素与容器的底部对齐?
    如何使用Flexbox将元素与容器的底部对齐?
    在提供的方案中使用FlexBox 在提供的方案中,您有一个带有各种子元素的div容器。您的目的是实现一个布局,而元素垂直堆叠,无论文本的高度如何。 flexbox通过自动保证金提供了解决此问题的解决方案。自动利润率使剩余空间在对齐之前的元素中分布到具有自动边缘的元素。实现所需布局的一种方法是使用以...
    编程 发布于2025-02-06
  • 如何精确测量.NET中的方法执行时间?
    如何精确测量.NET中的方法执行时间?
    .NET方法执行时间的精确计算 引言: 确定方法的执行时间对于性能优化至关重要。有多种方法可以测量此指标,每种方法都有其优点和缺点。 最佳方法:Stopwatch .NET 中的Stopwatch功能专门用于测量执行时间,被认为是最准确和最直接的方法。使用方法如下: var watch = Sys...
    编程 发布于2025-02-06
  • 如何使用char_length()在mySQL中按字符串长度对数据进行排序?
    如何使用char_length()在mySQL中按字符串长度对数据进行排序?
    [2使用内置的char_length()function。 char_length()和length() 此查询将从指定的表中检索所有行,并基于上升顺序对它们进行排序指定列的字符长度。带有更长字符串的行将出现在结果的底部。
    编程 发布于2025-02-06
  • 如何使用Python的记录模块实现自定义处理?
    如何使用Python的记录模块实现自定义处理?
    使用Python的Loggging Module 确保正确处理和登录对于疑虑和维护的稳定性至关重要Python应用程序。尽管手动捕获和记录异常是一种可行的方法,但它可能乏味且容易出错。解决此问题,Python允许您覆盖默认的异常处理机制,并将其重定向为登录模块。这提供了一种方便而系统的方法来捕获和...
    编程 发布于2025-02-06
  • 哪种哈希算法最适合PHP中的安全密码存储?
    哪种哈希算法最适合PHP中的安全密码存储?
    安全密码存储:SHA1 vs MD5 VS SHA256 vs bcrypt bcrypt:首选选择 通过password_hash()函数: //创建哈希 $ hash = password_hash($ password,password_default,['cost'=> ...
    编程 发布于2025-02-06
  • 如何使用不同数量列的联合数据库表?
    如何使用不同数量列的联合数据库表?
    合并列数不同的表 当尝试合并列数不同的数据库表时,可能会遇到挑战。一种直接的方法是在列数较少的表中,为缺失的列追加空值。 例如,考虑两个表,表 A 和表 B,其中表 A 的列数多于表 B。为了合并这些表,同时处理表 B 中缺失的列,请按照以下步骤操作: 确定表 B 中缺失的列,并将它们添加到表的末...
    编程 发布于2025-02-06
  • 如何使用shell_exec()从php执行mySQL *.sql文件?
    如何使用shell_exec()从php执行mySQL *.sql文件?
    在PHP 中执行mysql *.sql文件时,在创建网站数据库时,您可能会遇到需要执行的方案。 SQL文件从PHP到自动化站点生成。虽然zend_framework可能是有益的,但由于SQL语句中的不一致而直接运行。推荐的方法是使用Shell_exec()调用MySQL工具以执行您的 *.sql脚...
    编程 发布于2025-02-06
  • 为什么C#没有用于IEnumerable的for Extension方法?
    为什么C#没有用于IEnumerable的for Extension方法?
    [2 [2 最近关于C#缺乏扩展方法的扩展方法] 界面? 有趣的是,只有 list 包括此功能。 [2 这是遗漏的性能优化吗?还是有更深的根本原因? 几种理论试图解释这一明显的差距。 [2 一个观点表明,C#的内置 [2 { item.dosomething(); } 这与扩展方法...
    编程 发布于2025-02-06

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3