2016-05-31 51 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