zoukankan      html  css  js  c++  java
  • 【问题和解决《NLTK PYTHON》自然语言处理中文翻译版中的一处代码错误

        我也是一名NLP的新手,导师给了我们入门的学习资料,便是《Natural Language Processing with Python》的国内爱好者免费翻译的中文版。在中文版中,难免有一些小错误,大部分错误经过自己的稍微仔细研读可以改正过来。

        在这里发现了一处代码上的小错误,供大家分享。

    在11.3数据采集中的“处理濒危语言时特别注意事项”小节里面,有一处将辅音字母顺序规范化的代码。这段代码其实并不难理解。但是在中文版的翻译中,可能是由于排版者的不小心疏忽,造成了排版错误。

    在中文版的当中,这段代码是这样的:

    >>>mappings= [('ph', 'f'), ('ght', 't'), ('^kn', 'n'), ('qu', 'kw'),
    ... ('[aeiou]+', 'a'), (r'(.)\1', r'\1')]
    >>>def signature(word):
    ... for patt,repl in mappings:
    ... word= re.sub(patt, repl, word)
    ... pieces= re.findall('[âeiou]+', word)
    ... return ''.join(char for piecein pieces for charin sorted(piece))[:8]
    >>>signature('illefent')
    'lfnt'
    >>>signature('ebsekwieous')
    'bskws'
    >>>signature('nuculerr')
    'nclr'

    关注pieces=re.findall…这一行,发现a上面多了一个^符号,这里是不正确的。

    应该把这句代码写成pieces= re.findall('[^aeiou]+', word),学过正则表达式和读过这一段的读者们应该都知道为什么要这样改。

    完整的代码如下:

    >>> mappings = [('ph', 'f'), ('ght', 't'), ('^kn', 'n'), ('qu', 'kw'),
    ...             ('[aeiou]+', 'a'), (r'(.)\1', r'\1')]
    >>> def signature(word):
    ...     for patt, repl in mappings:
    ...         word = re.sub(patt, repl, word)
    ...     pieces = re.findall('[^aeiou]+', word)
    ...     return ''.join(char for piece in pieces for char in sorted(piece))[:8]
    >>> signature('illefent')
    'lfnt'
    >>> signature('ebsekwieous')
    'bskws'
    >>> signature('nuculerr')
    'nclr'

    再次感谢热心翻译作者为我们带来的入门经典作品!

  • 相关阅读:
    hadoop2 作业执行过程之reduce过程
    hadoop2 作业执行过程之map过程
    hadoop2 作业执行过程之yarn调度执行
    scala的下划线
    tomcat 配置系列1
    Windows Server 2008 __ Windows Server 2008R2
    dell技术中心
    戴尔服务器启动和raid设置(以dell r420为例)
    WinPE安装windows(dell r420)
    Red Hat enterprise linux 5.4 x64 + DELL R420 安装网卡驱动 (broadcom 5270)
  • 原文地址:https://www.cnblogs.com/createMoMo/p/3087711.html
Copyright © 2011-2022 走看看