python - 在触发词之后剪切句子

标签 python

我有以下带有方法的类:

class Trigger():

    def getRidOfTrashPerSentence(self, line, stopwords):
        countWord = 0
        words = line.split()
        for word in words:
            if countWord == 0:
                if word in stopwords:
                    sep = word
                    lineNew = line.split(sep, 1)[0]
                    countWord = countWord + 1
                    return(lineNew)

    stopwords = ['regards', 'Regards']

    def getRidOfTrash(self, aTranscript):
        result = [self.getRidOfTrashPerSentence(line, self.stopwords) for  line in aTranscript]
        return(result)

我想用它实现的是在某些触发词(如['regards', 'Regards'])之后删除句子中的“垃圾”

所以当我插入这样的 block 时:

aTranScript = [ "That's fine, regards Henk", "Allright great"]

我正在寻找这样的输出:

aTranScript = [ "That's fine, regards", "Allright great"]

但是当我这样做时:

newFile = Trigger()
newContent = newFile.getRidOfTrash(aTranScript)

我只得到“没关系”

关于如何获得两个字符串的任何想法

最佳答案

这是一个简单的解决方案:

yourString = 'Hello thats fine, regards Henk'
yourString.split(', regards')[0]

此代码将返回:“你好,很好”

如果需要,您可以在末尾添加“问候”:

yourString.split(', 问候')[0]+', 问候'

关于python - 在触发词之后剪切句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42221457/

相关文章:

python - 操作二维列表区域的更短方法

python - 了解将密集层连接到 LSTM

python - 如何将列表的内容保存到文件中?

python - 使用 matplotlib 绘制热图

python - 我正在尝试使用 python 请求创建用于网络抓取的 POST 数据

python - 纺织品解析 YAML

python - 如何使用 QPainterPath 裁剪图像而不保存图像的其余部分

python - sympy:规范化右侧为常数的线性等式

python - 我如何设计一个扭曲的工厂来处理断开连接?

python - 有没有办法从 Django 的任何地方访问上下文?