[英]Count how many times a list of characters appear in a string - Python (no count or counter)
[英]Python - Count how many times keywords stored in a list appear in text
我有一個包含 20k+ 個關鍵字的 KeywordList 列表。 我想檢查 KeywordList 中有多少關鍵字出現在多個單獨的文本文件中。 我還想知道文本文件中出現的關鍵字的總體頻率。 做這個的最好方式是什么?
我會使用詞袋方法:參見https://en.wikipedia.org/wiki/Bag-of-words_model
這是我幾年前的一個例子,從 pandas dataframe 中提取字數:
all_words = df['keywords'].str.split(expand=True).unstack().value_counts()
這為您提供了唯一單詞及其計數的鍵值對。 遍歷您的文件,您應該擁有所有單詞及其計數
從那里您可以將關鍵字和關鍵字列表轉換為集合並使用intersection
function。這將為您生成一組關鍵字列表中的所有關鍵字
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.