2016-05-31 44 views
0

中将英国英语转换为美国拼写的Bash脚本/实用程序我正在寻找一个快速Bash脚本,将英国/新西兰拼写转换为TeX文档中的美国语(用于与美国学者和期刊提交工作)。这是一份正式的数学生物学论文,其中很少有区域术语或语法:先前的工作是以公式而不是引号的形式给出的。在TeX文档

例如,

Generalise - >Generalize

Colour - >Color

Centre - >Centre

图必须有sedawk基于脚本替换最常见的拼写的差异。

有关更多详细信息,请参阅相关的TeX论坛问题。

https://tex.stackexchange.com/questions/312138/converting-uk-to-us-spellings

注:我目前在Ubuntu 16.04或Elementary OS 0.3 Freya上使用kile编译PDFLaTeX,但如果其他地方有内置修复程序,我可以使用另一个TeX编译器/软件包。

感谢您的协助。

+0

“替换” 不会为你做的工作? –

+0

该脚本是微不足道的。数据,但是...你能提供一个合适的替代列表吗? –

+0

那么,我可以使用'sed'或'awk'来分别替换每个案例。我希望有人已经为普通情况准备了一个循环或脚本。事实上,找到一个通用替代品列表是另一个挑战。如果要自己做,我会在github上设置它,以便在遇到新病例时进行更新。 –

回答

0

我认为你需要有一个方便的替代品清单,并将其命名为翻译。你将不得不丰富你的字典文件来有效地翻译文本文件。

sourceFile=$1 
dict=$2 

while read line 
    do 
    word=$(echo $line |awk '{print $1}') 
    updatedWord=$(grep -i $word $dict|awk '{print $2}') 

    sed -i "s/$word/$updatedWord/g" $sourceFile 2 > /dev/null 

    done < $dict 

运行像上面的脚本:

./scriptName source.txt dictionary.txt 

下面是我用一个样本字典:

>cat dict 
characterize characterise 
prioritize prioritise 
specialize specialise 
analyze analyse 
catalyze catalyse 
size size 
exercise exercise 
behavior behaviour 
color colour 
favor favour 
contour contour 
center centre 
fiber fibre 
liter litre 
parameter parameter 
ameba amoeba 
anesthesia anaesthesia 
diarrhea diarrhoea 
esophagus oesophagus 
leukemia leukaemia 
cesium caesium 
defense defence 
practice practice 
license licence 
defensive defensive 
advice advice 
aging ageing 
acknowledgment acknowledgement 
judgment judgement 
analog analogue 
dialog dialogue 
fulfill fulfil 
enroll enrol 
skill, skillful skill, skilful 
labeled labelled 
signaling signalling 
propelled propelled 
revealing revealing 

执行结果:

cat source 
color of this fiber is great and we should analyze it. 

./ScriptName source.txt dict.txt 

cat source 
colour of this fibre is great and we should analyse it. 
+0

谢谢,这正是我所想的非常有帮助。该词典是在文件或网络中添加用例的好起点。是否有排除单词的方法,如果它们用于代码例如'\ color''xcolor''color {'所以切换不会弄乱LaTeX标签?通常我会写英国拼写,并需要保留代码或乳胶的美国拼写。如果我(或其他人)未来需要将美国转换为英国拼写,那么认为这将是有益的。 –

+0

使用shell'while read'在每行输入上执行文本转换是一个反模式。相反,你应该看看Awk。 (其他一些脚本语言也可以。) – tripleee

0

这里是我的解决办法awk,我认为比sed更灵活。 此prg。离开LaTeX命令(当单词以“\”开始时)并且将保留第一个大写字母。 LaTeX命令(和普通文本)的参数将被字典文件替代。 当[rev]程序的第三个参数打开时,它将通过相同的字典文件进行反向替换。 任何非alpha-beta字符都可作为字词分隔符(这在LaTeX源文件中是必需的)。 prg将其输出写入屏幕(stdout),因此您需要使用重定向到文件(> output_f)。 (我认为你的LaTeX源的inputencoding是1字节/字符。)

> cat dic.sh 
#!/bin/bash 
(($#<2))&& { echo "Usage $0 dictionary_file latex_file [rev]"; exit 1; } 
((d= $#==3 ? 0:1)) 
awk -v d=$d ' 
BEGIN {cm=fx=0; fn="";} 
fn!=FILENAME {fx++; fn=FILENAME;} 
fx==1 {if(!NF)next; if(d)a[$1]=$2; else a[$2]=$1; next;} #read dict or rev dict file into an associative array 
fx==2 { for(i=1; i<=length($0); i++) 
      {c=substr($0,i,1);       #read characters from a given line of LaTeX source  
      if(cm){printf("%s",c); if(c~"[^A-Za-z0-9\\\]")cm=0;} #LaTeX command is occurred 
      else if(c~"[A-Za-z]")w=w c; else{pr(); printf("%s",c); if(c=="\\")cm=1;} #collect alpha-bets or handle them 
      } 
     pr(); printf("\n");        #handle collected last word in the line 
     } 
function pr( s){ # print collected word or its substitution by dictionary and recreates first letter case 
    if(!length(w))return; 
    s=tolower(w); 
    if(!(s in a))printf("%s",w); 
    else printf("%s", s==w ? a[s] : toupper(substr(a[s],1,1)) substr(a[s],2)); 
    w="";} 
' $1 $2   

字典文件:

> cat dictionary 
apple  lemon 
raspberry cherry 
pear  banana 

LaTeX的输入源:

> cat src.txt 
Apple123pear,apple "pear". 
\Apple123pear{raspberry}{pear}[apple]. 

Raspberry12Apple,pear. 

执行结果:

> ./dic.sh 
Usage ./dic.sh dictionary_file latex_file [rev] 

> ./dic.sh dictionary src.txt >out1.txt; cat out1.txt 
Lemon123banana,lemon "banana". 
\Apple123pear{cherry}{banana}[lemon]. 

Cherry12Lemon,banana. 

> ./dic.sh dictionary out1.txt >out2.txt rev; cat out2.txt 
Apple123pear,apple "pear". 
\Apple123pear{raspberry}{pear}[apple]. 

Raspberry12Apple,pear. 

> diff src.txt out2.txt # they are identical