2012-08-16 119 views
0

见这一块的Perl代码:Perl的非英语字符

#!/usr/bin/perl -w -CS 

use feature 'unicode_strings'; 

open IN, "<", "wiki.txt"; 
open OUT, ">", "wikicorpus.txt"; 

binmode(IN, ':utf8'); 
binmode(OUT, ':utf8'); 

## Condition plain text English sentences or word lists into a form suitable for constructing a vocabulary and language model 

while (<IN>) { 

    # Remove starting and trailing tags (e.g. <s>) 
    # s/\<[a-z\/]+\>//g; 

    # Remove ellipses 
    s/\.\.\./ /g; 

    # Remove unicode 2500 (hex E2 94 80) used as something like an m-dash between words 
    # Unicode 2026 (horizontal ellipsis) 
    # Unicode 2013 and 2014 (m- and n-dash) 
    s/[\x{2500}\x{2026}\x{2013}\x{2014}]/ /g; 

    # Remove dashes surrounded by spaces (e.g. phrase - phrase) 
    s/\s-+\s/ /g; 

    # Remove dashes between words with no spaces (e.g. word--word) 
    s/([A-Za-z0-9])\-\-([A-Za-z0-9])/$1 $2/g; 

    # Remove dash at a word end (e.g. three- to five-year) 
    s/(\w)-\s/$1 /g; 

    # Remove some punctuation 
    s/([\"\?,;:%???!()\[\]{}<>_\.])/ /g; 

    # Remove quotes 
    s/[\p{Initial_Punctuation}\p{Final_Punctuation}]/ /g; 

    # Remove trailing space 
    s/ $//; 

    # Remove double single-quotes 
    s/''//g; 
    s/ ''/ /g; 

    # Replace accented e with normal e for consistency with the CMU pronunciation dictionary 
    s/?/e/g; 

    # Remove single quotes used as quotation marks (e.g. some 'phrase in quotes') 
    s/\s'([\w\s]+[\w])'\s/ $1 /g; 

    # Remove double spaces 
    s/\s+/ /g; 

    # Remove leading space 
    s/^\s+//; 

    chomp($_); 

    print OUT uc($_) . "\n"; 
# print uc($_) . " "; 
} print OUT "\n"; 

似乎有上线49非英文字符,即行s/?/e/g;。 所以当我运行这个,警告出来Quantifier follows nothing in regex;

我该如何处理这个问题?如何让Perl识别角色?我必须用perl 5.10来运行这段代码。

另一个小问题是,第一行中“-CS”的含义是什么。

感谢所有。

+1

'?'不是?在文件中标记为最初写入的文件时,该文件可能以某种方式由于某处失败的字符集转换而损坏。 – OmnipotentEntity 2012-08-16 05:08:29

+1

'-CS'表示STDOUT,STDERR和STDIN被假定为utf-8 – OmnipotentEntity 2012-08-16 05:12:35

+0

@OmnipotentEntity请参阅说明,我猜?应该是重音e。我该如何修改? – Denzel 2012-08-16 05:16:44

回答

1

我认为你的问题在于你的编辑器没有处理unicode字符,所以程序在它进入perl之前就被破坏了,因为这显然不是你的程序,它可能会在它到达你之前被破坏。

在整个工具链正确处理unicode之前,必须小心地以保留它们的方式编码非ascii字符。这是一种痛苦,并不存在简单的解决方案。请参阅perl手册以了解如何安全地嵌入unicode字符。

+0

是的,当编码问题到来时,它会变得痛苦。你的解释是鼓舞人心的。谢谢 – Denzel 2012-08-16 05:18:58

1

根据错误行之前的注释行,要替换的字符是带重音的“e”;大概是什么意思,带有尖锐的口音:“é”。假设你的输入是Unicode,它可以用Perl表示为\x{00E9}。另请参阅http://www.fileformat.info/info/unicode/char/e9/index.htm

我想你从一个服务器上的网页复制/粘贴这个脚本,这个服务器没有正确配置以显示所需的字符编码。另请参阅http://en.wikipedia.org/wiki/Mojibake

+0

没错。复制粘贴是一场噩梦。 – Denzel 2012-08-16 19:10:03