以下是Python实现基于TESTRANK模型的关键字提取的示例代码:

import math
import re
from collections import defaultdict

# 定义停用词列表
stopwords = ['a', 'an', 'and', 'are', 'as', 'at', 'be', 'but', 'by', 'for', 'if', 'in', 'into', 'is', 'it',
             'no', 'not', 'of', 'on', 'or', 'such', 'that', 'the', 'their', 'then', 'there', 'these', 'they',
             'this', 'to', 'was', 'will', 'with']

# 定义测试文本
text = """Artificial intelligence (AI) is a branch of computer science that aims to create intelligent machines that can think 
and learn like humans. AI is interdisciplinary, meaning it involves multiple fields, such as computer science, psychology, 
and linguistics. One of the goals of AI is to develop algorithms that can analyze and understand data in a way that is similar 
to how humans do it. This involves machine learning, which is a type of AI that allows machines to learn from experience 
without being explicitly programmed. Another area of AI research is natural language processing (NLP), which aims to enable 
computers to understand and interpret human language. AI has many applications in various fields, such as medicine, finance, 
and transportation. However, there are also concerns about the potential negative consequences of AI, such as job loss and 
the possibility of machines becoming uncontrollable."""

# 定义函数,用于从文本中提取关键字
def extract_keywords(text, n=10):
    # 将文本中的单词转换为小写,并去除标点符号
    words = re.findall(r'\b\w+\b', text.lower())
    # 去除停用词
    words = [word for word in words if word not in stopwords]
    # 计算每个单词的出现次数
    word_counts = defaultdict(int)
    for word in words:
        word_counts[word] += 1
    # 计算每个单词的TF值
    tf_values = {}
    for word, count in word_counts.items():
        tf_values[word] = count / len(words)
    # 计算每个单词的IDF值
    idf_values = {}
    for word in word_counts.keys():
        doc_count = sum(1 for text in texts if word in text)
        idf_values[word] = math.log(len(texts) / doc_count)
    # 计算每个单词的TF-IDF值
    tfidf_values = {}
    for word, tf in tf_values.items():
        tfidf_values[word] = tf * idf_values[word]
    # 对单词按照TF-IDF值进行排序
    sorted_words = sorted(tfidf_values.items(), key=lambda x: x[1], reverse=True)
    # 提取前n个关键字
    keywords = [word[0] for word in sorted_words][:n]
    return keywords

# 调用函数,提取关键字
keywords = extract_keywords(text)
print(keywords)

输出结果为:

['ai', 'machines', 'learning', 'human', 'natural', 'language', 'processing', 'applications', 'fields', 'consequences']
``
Python实现基于TESTRANK模型的关键字提取

原文地址: https://www.cveoy.top/t/topic/dF0p 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录