【问题标题】:How can I tokenize this text into sentences with Regex如何使用正则表达式将此文本标记为句子
【发布时间】:2017-05-27 04:36:55
【问题描述】:

“亲爱的华生,你来得正是时候,” 他亲切地说。 “等待是不值得的,”她说。 on。“你可以穿过门,没有人阻碍。” 然后,她见我笑着摇了摇头,突然把她扔到一边。 紧握着双手向前迈了一步。

查看突出显示的区域。我怎么可能区分'"'后跟句号(.)来结束句子的情况和句号(.)后跟'"'的情况

我已经为分词器尝试了这件作品。除了那一部分,它运行良好。

(([^।\.?!]|[।\.?!](?=[\"\']))+\s*[।\.?!]\s*)

编辑:我不打算使用任何 NLP 工具包来解决这个问题。

【问题讨论】:

  • 您是否意识到这不是单个正则表达式的任务?对此的解决方案比您“尝试”的要多得多。
  • Wiktor 这是一个简单的 Hackerrank 挑战,它只允许 Python 的 re 模块。
  • 所以,没有实用价值。

标签: python regex tokenize


【解决方案1】:

这里使用NLTK代替正则表达式:

from nltk import sent_tokenize
parts = sent_tokenize(your_string)
# ['"You could not possibly have come at a better time, my dear Watson," he said cordially.', "'It is not worth your while to wait,' she went on.", '"You can pass through the door; no one hinders."', 'And then, seeing that I smiled and shook my head, she suddenly threw aside her constraint and made a step forward, with her hands wrung together.']

【讨论】:

    【解决方案2】:

    不久前发现了这个功能

    def split_into_sentences(text):
    
    caps = u"([A-Z])"
    prefixes = u"(Mr|St|Mrs|Ms|Dr)[.]"
    suffixes = u"(Inc|Ltd|Jr|Sr|Co)"
    starters = u"(Mr|Mrs|Ms|Dr|He\s|She\s|It\s|They\s|Their\s|Our\s|We\s|But\s|However\s|That\s|This\s|Wherever)"
    acronyms = u"([A-Z][.][A-Z][.](?:[A-Z][.])?)"
    websites = u"[.](com|net|org|io|gov|mobi|info|edu)"
    
    if not isinstance(text,unicode):
        text = text.decode('utf-8')
    
    text = u" {0} ".format(text)
    
    text = text.replace(u"\n",u" ")
    text = re.sub(prefixes,u"\\1<prd>",text)
    text = re.sub(websites,u"<prd>\\1",text)
    if u"Ph.D" in text: text = text.replace(u"Ph.D.",u"Ph<prd>D<prd>")
    text = re.sub(u"\s" + caps + u"[.] ",u" \\1<prd> ",text)
    text = re.sub(acronyms+u" "+starters,u"\\1<stop> \\2",text)
    text = re.sub(caps + u"[.]" + caps + u"[.]" + caps + u"[.]",u"\\1<prd>\\2<prd>\\3<prd>",text)
    text = re.sub(caps + u"[.]" + caps + u"[.]",u"\\1<prd>\\2<prd>",text)
    text = re.sub(u" "+suffixes+u"[.] "+starters,u" \\1<stop> \\2",text)
    text = re.sub(u" "+suffixes+u"[.]",u" \\1<prd>",text)
    text = re.sub(u" " + caps + u"[.]",u" \\1<prd>",text)
    if u"\"" in text: text = text.replace(u".\"",u"\".")
    if u"!" in text: text = text.replace(u"!\"",u"\"!")
    if u"?" in text: text = text.replace(u"?\"",u"\"?")
    text = text.replace(u".",u".<stop>")
    text = text.replace(u"?",u"?<stop>")
    text = text.replace(u"!",u"!<stop>")
    text = text.replace(u"<prd>",u".")
    sentences = text.split(u"<stop>")
    sentences = sentences[:-1]
    sentences = [s.strip() for s in sentences]
    return sentences
    

    【讨论】:

    • 但是当 NLTK 不是选项时它可以完成工作,而且如果你对 NLTK 计时它会更快
    • 我真的不知道为什么人们如此反对寻找针对特定工具包的替代品!
    • 然而这仍然不能解决问题 这是输出“亲爱的华生,你来的时间再合适不过了,”他亲切地说。 \n “等待是不值得的,”她继续说。 \n '你可以通过门;没有人阻碍。 \n'然后,见我笑着摇摇头,她忽然抛开束缚,双手紧握着向前迈了一步。
    • @Djokester 可能是因为正则表达式不是最好的解决方案。
    猜你喜欢
    • 2014-11-02
    • 2016-08-04
    • 1970-01-01
    • 2014-05-05
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    相关资源
    最近更新 更多