gpt4 book ai didi

Python - 从字符串中删除停用词

转载 作者:太空宇宙 更新时间:2023-11-03 13:46:05 24 4
gpt4 key购买 nike

我在创建从字符串输入中删除停用词的代码时遇到问题。目前,这是我的代码:

stopWords = [ "a", "i", "it", "am", "at", "on", "in", "to", "too", "very", \
"of", "from", "here", "even", "the", "but", "and", "is", "my", \
"them", "then", "this", "that", "than", "though", "so", "are" ]
stemEndings = [ "-s", "-es", "-ed", "-er", "-ly" "-ing", "-'s", "-s'" ]
punctuation = [ ".", ",", ":", ";", "!", "?" ]
line = raw_input ("Type in lines, finish with a . at start of line only:")
while line != ".":
def remove_punctuation(input): #removes punctuation from input
output = ""
text= 0
while text<=(len(input)-1) :
if input[text] not in punctuation:
output=output + input[text]
text+=1
return output
newline= remove_punctuation(line)
newline= newline.lower()

可以添加什么代码来根据上面的停用词列表从字符串中删除停用词?提前谢谢你。

最佳答案

正如格雷格所建议的,您应该使用 for循环而不是 while因为它更像 pythonic 且易于理解代码。此外,您应该在 while 之前声明您的函数。循环输入,这样 python 解释器就不会每次都重新定义函数!

此外,如果需要,您可以将标点符号设置为 string而不是 list (为了可读性和易用性)

stopWords = [ "a", "i", "it", "am", "at", "on", "in", "to", "too", "very", \
"of", "from", "here", "even", "the", "but", "and", "is", "my", \
"them", "then", "this", "that", "than", "though", "so", "are" ]
stemEndings = [ "-s", "-es", "-ed", "-er", "-ly" "-ing", "-'s", "-s'" ]
punctuation = ".,:;!?"

def remove_punctuation(input_string):
for item in punctuation:
input_string = input_string.replace(item, '')
return input_string

line = raw_input ("Type in lines, finish with a . at start of line only:")

while not line == ".":
newline = remove_punctuation(line)
newline = newline.lower()

关于Python - 从字符串中删除停用词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20301406/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com