You can download this code by clicking the button below.
This code is now available for download.
The function extracts keywords from a text using regular expressions and removes common English stopwords.
Technology Stack : Regular expressions, set
Code Type : Function
Code Difficulty : Intermediate
import os
import re
import json
import math
import random
def extract_keywords(text):
# 使用正则表达式提取文本中的关键词
keywords = re.findall(r'\b\w+\b', text)
# 去除常见的英文停用词
stopwords = set(['the', 'and', 'is', 'in', 'to', 'of', 'for', 'on', 'with', 'as', 'by', 'that', 'this', 'an', 'be', 'are', 'or', 'at', 'from', 'it', 'not', 'which', 'also', 'such', 'can', 'has', 'have', 'had', 'will', 'would', 'shall', 'should', 'may', 'might', 'must', 'do', 'does', 'did', 'but', 'if', 'then', 'else', 'when', 'who', 'whom', 'whose', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', 'should', 'now', 'my', 'i', 'you', 'he', 'she', 'it', 'we', 'they', 'their', 'them', 'ours', 'ourselves', 'your', 'yours', 'his', 'him', 'her', 'hers', 'it', 'its', 'my', 'mine', 'our', 'ours', 'your', 'yours', 'his', 'him', 'her', 'hers', 'the', 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'being', 'been', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once'])
keywords = [word for word in keywords if word.lower() not in stopwords]
return keywords