#!/usr/bin/perl -SD
# 地名至漢語拼音
# topy -- place names Chinese to ASCII Hanyu Pinyin translator
# Copyright       : http://www.fsf.org/copyleft/gpl.html
# Author          : 積丹尼 Dan Jacobson http://jidanni.org/lang/pinyin/taizhong
# Created On      : Sun Oct 31 07:51:26 2004
# Last Modified On: Sun May 11 07:17:27 2008
# Update Count    : 727
#本程式已過錯綜復雜 This program is already a too complicated mess.
#It is the wrong approach. A bigger and bigger unwieldy mess.
#make sure you test the side effects on any change on all the other
#files that depend on this program!
#作者專長音譯非意譯！The author concentrates on romanization, not translation!
use strict;
use warnings;
use utf8;
binmode STDIN, ":utf8";
binmode STDOUT, ":utf8";
use Text::Unidecode;
my $SHAME = $ENV{SHAME};    ##2006年中央政策
while (<>) {
    s/\s//g;                #clean sloppy input and newline
    next if /^$/;
    s/袓/祖/;             #fix error
    s/為(修平)/$1/;
    s/陜/陝/;             #unify
    s/八O五/八○五/;
##  s/^部子/\x{5ECD}子/;#广+部, but not big5
    print;                  #print input
    print ",";
    ##Translations before saving first two chars:
    ##s/^南大雅/S. Daya /;
    s/亞洲大學/Asia University /;
    s/頂車站/Pre-Station /;
    s/(分駐|派出)所/Police Station /;
    s/番石榴/$& /;
    s/^..厝/$& /;
    s/水利會/Irrigation Board /;
    s/后里/ Houli /;
    s/^(\d+)鄰/Neighborhood $1/;
    s/公里/ Km./ && nums();
    /^.區/        && nums();
    s/^(...)(崁)$/$1 $2/;
    s/^(...)頂$/$1 Ding/;
    s/^..山(?!莊)/$&\@/;
    s/保養(場|廠)/Maintenance Plant /;
    s/印刷/Print /;
    s/嵙/Ke /g;
    s/清真寺/Mosque /;
    s/永豐棧/$&\@/;
    s/(市|商)場/Market / && s/口/ Entrance/;
    s/(門|入)口/Entrance /;
    s/馬場/Stable /;
    s/高速公路|國道/Expressway /;
    s/^省(二|三)/  $1 Provincial / ||    #2 blanks at front
      s/^省/Provincial /;
    s/中央健保局/National Health Insurance /;
    s/市警局/Police Headquarters /;
    s/車站前/Station Front /;
    s/航空站|機場/Airport /;
    s/菸葉?廠/Tobacco Factory /;#else 葉:Xie!
    s/鐵路/Railway /;
    s/市立殯儀館/City Funeral Home /;
    s/^中國/China /;
    s/\(有?(經|停靠)/(Via /;
    s/(下| )大里/$1 Dali /;
    s/啤?酒廠/Brewery /;
    s/終點/Terminal /;
    s/賓館|招待所/Guest House /;
    s/玩具反斗城/Toy Store /;
    s/三越/Sanyue /;
    s/幼稚園/Kindergarten /;
    s/(.*)餐廳/$1\@Restaurant /;
    s/.*(SOGO)/$1 /;
    s/後備/Reserve /;
    s/天主/Catholic /;
    s/教(會|堂)/ Church /;
    s/台電/Electric Co. /;
    s/職訓/Vocational Training /;
    s/郵局/Post Office /;
    s/卡多里/Kadoli /;
    s/戶政所/Household Bureau /;
    s/休息/Rest /;
    s/經補庫/Supply Depot /;
    s/日僑/Japanese /;
    s/^(..)(\D[^\d一二三四五六七八九十])路口$/$1\/$2 Rds./;
    s/工業區/Industrial Park /;
    s/加工(出口)?區/Export Processing Zone /;
    s/高鐵/High Speed Railway /;
    s/彰化/Changhua / if $SHAME;
##  s/中港路/台中港路/;
    s/台中(?!(港?路))/Taichung / if $SHAME;
    s/^中市/Taichung /             if $SHAME;
    s/兒童/Children's /;
    s/^[部\x{5ECD}]子(坑?)/Puzi $1\@/;
    s/八二三/823 /;
    s/八○三/803 /;
    s/八○五/805 /;
    s/^中區/Central District /;
    s/地方/Local /;
    s/叉路口/Junction /;
    s/^CCK/$& /;    #QQG! SHAME
    s/(鄉|鎮)公所/Township Hall /;
    s/美術館/Art Museum /;
    s/國際(?!街)/International /;
    s/捐血/Blood Donation /;
    s/^.區/\@\@$&/;
    s/文化(?!街)/Culture /;
    s/市農會/City Farmers' Association /
      || s/農會/Farmers' Association /;
    s/市政?府/City Government /;
    s/(政| )府/ Government /;
    s/消防隊/Fire Dept./;
    s/水廠/Water Plant /;
    s/法庭/Court /;
    s/肉品/Meat /;
    s/生產力/Productivity /;
    s/籌備/Preparatory /;
    s/少年觀護所/Youth Guidance Facility /;
    s/縣?市界/City Limits /;
    s/^(縣)界/$1 Limits /;
    s/地政(事務)?所/Land Bureau /;
    s/縣/County /;
    s/簡易/Simplified /;
    s/議會/Assembly /;
    s/^舊(.*) /Old $1/;    #hack city
    s/立體停車場/Parking Building /;
    s/教育廳/Education Dept./;
    s/檢(查|驗)/Inspection /;
    s/活動/Activity /;
    s/教(師|育)/Teachers' /;
    s/信用|合作(金庫|社)/Co-op Bank /;
    s/真耶穌/True Jesus /;
    s/觀光(?!路)/Tourist /;
    s/變電所|發電廠/Power Station /;
    s/空軍/Air Force /;
    s/榮總(醫院)?/Veterans' Hospital /;
    s/公園口/Park /;
    s/警察局$/Police Dept. /;
    s/(警察)?分局/Police Division /;
    s/警察局?/Police /;
    s/高等/High /;
    s/監理(所|站)/Drivers' Services /;
    s/水壩$/Dam /;
    s/^後(.*站)/Rear \@$1/;
    s/加油站/Gas Station /;
    s/收費站/Toll Station /;
    s/仁愛之家/Old Folks' Home /;
    s/中小企銀/Small Business Bank/;
    s/老人/Senior Citizen /;
    s/老虎城/Tiger City /;
    s/瓦斯/Gas /;
    s/科學/Science /;
    if ( s/第// || s/^(..?)號/$1 / ) { nums() }
    s/衛生所/Health Dept. /;
    s/電力/Electric /;
    s/火車/Railway /;
    s/中華電信|電信局/Telecom Office /;    #"Chunghwa" too dumb
    s/陳列館/Exhibition  Hall /;
    s/集會(?=所)/Meeting /;
    s/電信/Telecom /;
    s/航發/Aviation Development /;
    s/分院/Branch /;
    s/收費/Toll /;
    s/精神科/Mental Health Dept./;
    s/急診部/Emergency Dept. /;
    s/(南|北|東|西)院/$1 Division /;
    s/上七張犁/Shang Qizhangli /;
    ##Save first two chars:
    m/^..路/
      || s/^(頂|上|下)(...)$/$1 @ $2 /;        #中: harder
    my $front = "";
    if ( !/\@/ ) {
        /(.?.?)/ && ( $front = $1 ) && s/(.?.?)//;
    }
    s/(路|街|巷|里)口/$1 Jct./;    #同一路線有無口之分！
    s/(營區|村)口/$1/;              #英語「口」多不翻譯
    s/(\d)(街|路)/$1 $2/;
    s/路/Rd. /;
    s/街/St. /;
    s/巷/Ln. /;
    s/弄/Aly. /;
    s/東([^山興])/E. $1/;            # keep e.g., 水流東

    if ( !/屯|頂/ ) {
        s/南(?=.)/S. /;
        s/西(?=.)/W. /;
        s/北(?=.)/N. /;
    }
    nums() unless /\@/;                 ## again
    s/(道)?路/Rd. /;
    s/(園|大)道/Blvd. /;
    s/鄰/Neighborhood /;
    s/停車場/Parking Lot /;
    m1();
    s/^[^[:ascii:]]{2}/ $&/ unless /\@/;
    $_ = $front . $_;                   ##put back first two chars
    s/^(..) (.)(口)$/$1$2 $3 /;
    s/^(頂|下)(?!厝)/$&@ /;
    !/東山/ && s/ (.(仔|山|岡))/$1/;
    s/[^[:ascii:]]+/(pin($&))[0]/eg;
    tr/@//d;
    s/^ +| +$//g;
    s/  +/ /g;
    s/ \)/)/;
    s@ /@/@;
    print;
    print "\n";
}

sub nums {
    s/十一/11th /;
    s/十二/12th /;
    s/十三/13th /;
    s/十四/14th /;
    s/十五/15th /;
    s/十六/16th /;
    s/十七/17th /;
    s/十八/18th /;
    s/十九/19th /;
    s/一/1st /;
    s/二/2nd /;
    s/三/3rd /;
    s/四/4th /;
    s/五/5th /;
    s/六/6th /;
    s/七/7th /;
    s/八/8th /;
    s/九/9th /;
    s/十/10th /;
    ##But 國際街一巷,Guoji St. 1st Ln. or Ln. 1?
}

sub pin {
    my @parms = @_;
    for (@parms) {
        s/一/Yi /g;    #unidecode bug fix
##      s/茄(冬|苳)/Jiadong /;
        s/寧/Ning /g;        #fix Unicode bug
        s/陜|陝/Shan /g;    #unidecode bug fix
        s/埔/Pu /g;          #not Bu, Taiwanism
        s/堤/Ti /g;          #not Di
        s/圳/Jun /g;         #not Zhen
##      s/埤/...Pi already OK?
        s/朝(?!陽)/Chao /g; #not Zhao mostly
        s/重/Chong /g;       #重慶路,三重...
##      s/仔/Zai /g; #if not Zi...
        $_ = unidecode $_;
        s/ ([AEO])/'$1/g;
##http://www.pinyin.info/romanization/hanyu/apostrophes.html
        s/ //g;
        s/$/ /;
        s/[\w']+/ucfirst lc $&/e;
        return @parms;
    }
}

sub m1 {
    s/附(中|小)/$1學/;    #cheating!
    s/家商/Home Economics and Commercial High School /;
    s/青年之家/Youth Home /;
    s/仁愛之家/Old Folks Home /;
    s/司令部/Headquarters /;
    s/紀念/Memorial /;
    s/自來水/Water /;
    s/吊橋/ Suspension Bridge /;
    s/加壓站/Pressure Station /;
    s/農藥/Agricultural Chemical /;
    s/紙(?=廠)/Paper /;
    s/研習會|訓練班/Training Facility /;
    s/營運/Operations /;
    s/登山步道口/Trail /;
    s/勞工/Labor /;
    s/高農/Agricultural Senior High School /;
    s/高商|商專/Commercial School /;
    s/學苑/China Youth Corps /;
    s/服務/Service /;
    s/標頻台/Transmitter /;
    s/技訓/Technical Training /;
    s/職訓/Job Training /;
    s/交通部/Min. of Transportation and Communications /;
    s/內政部/Min. of Interior /;
    s/船岸/Ship-To-Shore /;
    s/消費/Consumer /;
    s/古建築藝術館/Old Architecture Museum /;
    s/縱貫鐵路/N - S Railway /;
    s/高速鐵路/High Speed Railway /;
    s/快速道路/Freeway /;
    s/福利(站|社)/Commodity Center/;
    s/創意/Creativity /;
    s/塑膠/Plastics /;
    s/遊憩/Recreational /;
    s/學院/College /;
    s/科技園區/Science Park /
      || s/科技$/Technology /
      || s/技術|科技/Technical /;
    s/園區|公園/Park /;
    s/墓園/Cemetery /;
    s/寶塔/Pagoda /;
    s/農業/Agriculture /;
    s/(社區|新村|新?城)+/Community /;
    s/同仁/Employee /;
    s/宿舍/Dormitory /;
    s/安養院/Care Home /;
    s/豬場/Swine Farm /;
    s/飼料場/Feed Lot /;
    s/看守所/Jail /;
    s/監獄/Prison /;
    s/都會/Metropolitan /;
    s/露營區/Campground /;
    s/營區/Army Base /;
    s/棒球場/Baseball Field /;
    s/紡織/Textiles /;
    s/法院/Court /;
    s/高爾夫球場/Golf Course /;
    s/(畜)?(農|牧)+場|果園|農莊/Farm /;
    s/(大)?賣場/Shopping Center /;
    s/(汽車駕訓|駕駛)班/Driving Academy /;
    s/側門/Side Entrance /;
    s/水利會/Irrigation Dept. /;
    s/通信微波站/Microwave Station /;
    s/(涼)?亭/Pavilion /;
    s/工作站/Work Station /;
    s/乳牛/Milk /;
    s/休閒/Leisure /;
    s/交流道/Interchange /;
    s/斷層/Fault /;
    s/(禪|祖)?(廟|宮|殿|寺|祠堂|祠)/Temple /;    #Rough
    s/釣魚/Fishing /;
    ## s/陂$/Pond /; # 埤 too?
    ## s/魚池/Fish Pond /; #but 大魚池 ?
    ## s/池(塘)?/Pond /;
    s/購物/Shopping /;
    s/研究所|試驗所|訓練所/Institute /;
    s/中心/Center /;
    s/國宅/Residences /;
    s/茶(葉|藝)/Tea /;
    s/藥[方房]/Pharmacy /;
    s/渡假/Vacation /;
    s/資源回收站/Recycling Station /;
    s/招待所/Residence /;
    s/垃圾處理場/Garbage Dump /;
    s/保齡球館/Bowling Alley /;
    s/網球場/Tennis Court /;
    s/博物館/Museum /;
    s/花園/Garden /;
    s/茶園/Tea Garden /;
    s/有機/Organic /;
    s/山莊|別墅/Estate /;
    s/客運/Bus /;
    s/立?醫院/Hospital /;
    s/(酒|飯)店/Hotel /;
    s/中醫/Chinese Hospital /;
    s/稅捐處/Tax Office /;
    s/消防隊/Fire Dept. /;
    s/戶政事務所/Household Bureau /;
    s/衛生局/Health Office /;
    s/圖書館/Library /;
    s/國小|小學/Elementary School /;
    s/女中/Girls' Middle School /;
    s/國中|中學/Middle School /;
    s/高工/Technical High School /;
    s/農工/Agricultural Technical School /;
    s/世貿/World Trade /;
    s/師院/Teacher's College /;
    s/互動/Mutual /;
    s/工專/Technical School /;
    s/(\d.*)中$/$1 High School /;
    s/高中/High School /;
    s/學校/School /;
    s/活動/Activity /;
    s/靶場/Target Range /;
    s/日報/Daily News /;
    s/教會/Church /;
    s/系統/System /;
    s/東西向/E-W /;
    s/南北向/N-S /;
    s/會館/Association Hall /;
    s/生態/Habitat /;
    s/商銀/Commercial Bank /;
    s/銀(行)?/Bank /;
    s/行(前)/Co. $1/;    #else Xing
    s/哨/Post /;
    s/股/Dept. /;
    s/處/Office /;
    s/鐵工/ Iron /;
    s/工?廠/Factory /;
    s/工商|商工/Academy /;
    s/漁村/Fisherman's Village /;
    s/里|村/Village /;
    s/區/District /;
    s/(大)?橋/Bridge /;
    s/市/City /;
    s/段/Sec. /;          #what about 上段?
    s/(\d+) +(Sec\.)/$2 $1 /;
    s/河|溪/River /;     #Rough
    s/(鎮|鄉|市)民代表會/Council /;
    s/(.*)(公所|堂|館)$/$1 Hall /;
    s/咖啡/Cafe /;
    s/終站/Terminus /;
    s/(\d.*)站$/$1Stop /
      || s/車?站/Station / && s/總/Main /;
    s/戲院/Theater /;
    s/圓環/Circle /;
    s/廣場/Plaza /;
    s/電台/Radio Station /;
    s/天廈|大樓/Building /;
    s/行政/Administration /;
    s/轉(乘|運)/Transfer /;
    s/百貨(公司)?/Dept. Store /;
    s/體育場/Stadium /;
    s/體育/Sports /;
    s/游泳池/Pool /;
    ##油壓, Youya
    ##薄膜 Bomo
    s/公司/Company /;
    s/大學/University /;
    s/前$/Front /;
    !/庄頭/&& s/(...)頭$/$1 Head /;
    s/醫(學|藥)/Medical /;
    s/樂園/Amusement Park/;
    ##s/坑$/Valley /;
    s/所/Facility /;
    s/０/0/g;
    s/１/1/g;
    s/２/2/g;
    s/３/3/g;
    s/４/4/g;
    s/５/5/g;
    s/６/6/g;
    s/７/7/g;
    s/８/8/g;
    s/９/9/g;
    s/○/0/g;
    s/號//;
}

#疑慮輸出:
#Beitun District Administration Building 北屯區行政大樓
#Beitun Administration Building 北屯行政大樓
#Yinghuada Village 櫻花大村
