【问题标题】:Splitting a sentence by ending characters通过结束字符拆分句子
【发布时间】:2017-09-21 10:48:11
【问题描述】:

最近的一个项目让我需要将传入的短语(作为字符串)拆分为它们的组成句子。例如,这个字符串:

"Your mother was a hamster, and your father smelt of elderberries! Now go away, or I shall taunt you a second time. You know what, never mind. This entire sentence is far too silly. Wouldn't you agree? I think it is."

需要变成由以下元素组成的列表:

["Your mother was a hamster, and your father smelt of elderberries",
"Now go away, or I shall taunt you a second time",
"You know what, never mind",
"This entire sentence is far too silly",
"Wouldn't you agree",
"I think it is"]

对于此函数,“句子”是一个以!?. 结尾的字符串。请注意,如上所示,应从输出中删除标点符号。

我有一个工作版本,但它很丑,前导和尾随空格,我不禁想到有更好的方法:

from functools import reduce

def split_sentences(st):
  if type(st) is not str:
    raise TypeError("Cannot split non-strings")
  sl = st.split('.')
  sl = [s.split('?') for s in sl]
  sl = reduce(lambda x, y: x+y, sl) #Flatten the list
  sl = [s.split('!') for s in sl]
  return reduce(lambda x, y: x+y, sl)

【问题讨论】:

    标签: python arrays string list


    【解决方案1】:
    import re
    
    st1 = "  Another example!! Let me contribute 0.50 cents here?? \
             How about pointer '.' character inside the sentence? \
             Uni Mechanical Pencil Kurutoga, Blue, 0.3mm (M310121P.33). \
             Maybe there could be a multipoint delimeter?.. Just maybe...  "
    
    st2 = "One word"
    
    def split_sentences(st):
        st = st.strip() + '. '
        sentences = re.split(r'[.?!][.?!\s]+', st)
        return sentences[:-1]
    
    print(split_sentences(st1))
    print(split_sentences(st2))
    

    【讨论】:

      【解决方案2】:

      你也可以不使用正则表达式:

      result = [s.strip() for s in String.replace('!', '.').replace('?', '.').split('.')]
      

      或者,您可以编写一个不会大量复制数据的前沿算法:

      String = list(String)
      
      for i in range(len(String)):
          if (String[i] == '?') or (String[i] == '!'):
              String[i] = '.'
      
      String = [s.strip() for s in String.split('.')]
      

      【讨论】:

        【解决方案3】:

        改用re.split 来指定匹配任何句尾字符(以及任何后续空格)的正则表达式。

        def split_sentences(st):
            sentences = re.split(r'[.?!]\s*', st)
            if sentences[-1]:
                return sentences
            else:
                return sentences[:-1]
        

        【讨论】:

        • 不错。如果您解释了 if-else,它可能会对 OP 有所帮助。
        • 如果输入字符串的末尾有终止符,re.split 将返回一个末尾有一个空项的数组。空字符串是虚假的,因此如果数组 [-1] 的最后一项为空,则返回除最后一项之外的所有内容。
        【解决方案4】:

        您可以使用正则表达式split 将它们拆分为特定的特殊字符。

        import re
        str = "Your mother was a hamster, and your father smelt of elderberries! Now go away, or I shall taunt you a second time. You know what, never mind. This entire sentence is far too silly. Wouldn't you agree? I think it is."
        re.compile(r'[?.!]\s+').split(str)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-04-28
          • 2020-10-19
          • 1970-01-01
          • 2018-11-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-25
          相关资源
          最近更新 更多