我正在为一个单词识别程序创建几个词典,这些词典仅包含单词的x个前声音(此后称为ngram)。因此,我从现有词典中提取所需的单词。但是,我想自动执行此操作,即找到一个ngram的所有单词(例如ngram = 3),保存它们,增加ngram(= 4)并重复该过程。代码如下:
ngrams=$(seq 3 1 9)
for ngram in $ngrams
do
cat /Lexicon/whole_lexicon.lex | perl -ne 'chomp; @tok = split(/\s+/); $ntoprint = $#tok; if ($ngram < $ntoprint) {$ntoprint = $ngram}; for ($i = 1; $i <= $ntoprint; $i++) {printf("%s\t%s\n",join("",@tok[1..$i]),join(" ",@tok[1..$i])); }' > lexicons/lex$ngram.txt
done
不幸的是,perl无法识别值$ngram
,并且该命令无法正常工作。为了进行比较,此脚本有效:
ngram=3
cat /Lexicon/whole_lexicon.lex | perl -ne 'chomp; @tok = split(/\s+/); $ntoprint = $#tok; if (3 < $ntoprint) {$ntoprint = 3}; for ($i = 1; $i <= $ntoprint; $i++) {printf("%s\t%s\n",@tok[1..$i])); }' > lexicons/lex$ngram.txt
经过研究,我现在知道我可以编写一个perl脚本,然后将变量值$ngram
传递给该脚本,在这里我可以将其与@ARGV
一起使用。但是,我正在寻找一种解决方案,以便可以在终端中运行命令。