Perl非英语字符

Perl非英语字符,perl,encoding,Perl,Encoding,请参阅这段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 f

请参阅这段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"; 
#/usr/bin/perl-w-CS
使用“unicode_字符串”功能;
在“,”wikicorpus.txt”中打开;
binmode(IN':utf8');
binmode(输出“:utf8”);
##将纯文本英语句子或单词列表调整为适合构建词汇和语言模型的形式
而(){
#移除起始和尾随标签(例如)
#s/\//g;
#删除椭圆
s/\.\.\.//g;
#删除unicode 2500(十六进制E2 94 80),用作单词之间的m-破折号
#Unicode 2026(水平省略号)
#Unicode 2013和2014(m-和n-破折号)
s/[\x{2500}\x{2026}\x{2013}\x{2014}]//g;
#删除由空格包围的破折号(例如短语-短语)
s/\s-+\s//g;
#删除不带空格的单词之间的破折号(例如word--word)
s/([A-Za-z0-9])\-\-([A-Za-z0-9])/$1$2/g;
#删除单词末尾的破折号(例如三到五年)
s/(\w)-\s/$1/g;
#去掉一些标点符号
s/([\“\?,;:%?!()\[\]{}\.])//g;
#删除引号
s/[\p{Initial_标点}\p{Final_标点}]//g;
#删除尾随空格
s/$/;
#删除双单引号
s/''//g;
s/''//g;
#为了与CMU发音词典保持一致,将重音e替换为正常e
s/?/e/g;
#删除用作引号的单引号(例如某些“引号中的短语”)
s/\s'([\w\s]+[\w])”\s/$1/g;
#删除双空格
s/\s+//g;
#删除前导空格
s/^\s+/;
咀嚼(美元);
打印出uc($)。“\n”;
#打印uc($)”;
}打印出“\n”;
第49行似乎有一个非英语字符,即
s/?/e/g;
。 所以,当我运行这个命令时,警告出现了,
量词在regex;
中不跟任何内容

如何处理这个问题?如何让perl识别字符?我必须用Perl5.10运行这段代码

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


多谢大家。

我认为您的问题在于编辑器不能处理unicode字符,因此程序在进入perl之前就被破坏了,而且由于这显然不是您的程序,所以它可能在进入您的程序之前就被破坏了


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

根据错误行前的注释行,要替换的字符s是一个带重音的“e”;大概意思是带有尖锐重音的“e”。假设您的输入是Unicode,它可以在Perl中表示为
\x{00E9}
。另请参见


我猜您是从服务器上的网页复制/粘贴了此脚本,该服务器未正确配置为显示所需的字符编码。另请参见

文件中的
不是最初编写的标记,该文件可能因某个地方的字符集转换失败而损坏。
>-CS
意味着STDOUT、STDERR和STDIN被假定为utf-8@OmnipotentEntity请看解释,我猜?应该是重音e。我如何修改它?@omnipotenentity为什么它警告
对“-CS来说太晚了“第1行的选项”
?是的,当编码问题出现时,它会变得很痛苦。你的解释令人鼓舞。谢谢你。复制和粘贴是一场噩梦。