城市航空資訊字典

來源:互聯網
上載者:User

這個程式沒什麼可擴充性和使用價值,但是也算是個小小小小迷你的搜尋引擎,其中字典的產生過程也許大家願意參考一下,所以粘出來,裡面有很多垃圾的地方,歡迎吐槽!

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import java.util.ArrayList;import java.util.Iterator;import jxl.Cell;import jxl.Sheet;import jxl.Workbook;import jxl.read.biff.BiffException;public class CityDictionary {//字典的程式部分在這邊實現Dictionary dic;private final int mapNum = 82;CityDictionary(){dic = new Dictionary();try {FileInputStream fis = new FileInputStream("D:\\城市字典.dic");ObjectInputStream ois = new ObjectInputStream(fis);dic = (Dictionary)ois.readObject();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private Ditem insertCity(String cityName, String code) {String[] codes = code.split("/");Ditem item = inquireByChinese(cityName);for (int i = 0; i < codes.length; ++i) {//if(cityName.contains("上海"))//System.out.println(item);if (item == null)// 如果這個城市在字典中還沒存在{item = new Ditem(cityName, codes[i]);insert(item);insertCode(codes[i],item);return item;} else// 如果這個城市已經存在在字典中,判斷三字代碼是否存在,沒有則添加{int j;for (j = 0; j < item.codeNum; ++j) {if (item.code[j].contentEquals(codes[i])) {//System.out.println("重複了");//if(cityName.contains("上海"))//System.out.println(item);break;// 已存在,放棄}}if (j >= item.codeNum)// 不存在{// 如果這個城市已經存在,但是這個三字代碼不存在,插入三字代碼//if(cityName.contains("上海"))//System.out.println(item);item.insertCode(codes[i]);insertCode(codes[i],item);}}}return item;}private void insertCode(String code,Ditem item)//建立狀態時使用{dic.codeList[code.toUpperCase().codePointAt(0)-65][code.toUpperCase().codePointAt(1)-65].add(item);}public Ditem inquireByCode(String code){Iterator<Ditem> iter = dic.codeList[code.toUpperCase().codePointAt(0)-65][code.toUpperCase().codePointAt(1)-65].iterator();Ditem item = null;while(iter.hasNext()){item = iter.next();for(int i=0;i<item.codeNum;++i){if(item.code[i].contains(code))return item;}}return null;}private void bliudDictionary()//已知城市三字代碼4297個{int no = 0;//輸入資料的位置D:\input\input.txtString line = null;String cityName = null;String code = null;try {BufferedReader reader = new BufferedReader(new FileReader("D:\\input\\input2.txt"));while((line=reader.readLine())!=null){String[] parts = line.split("\t");//StringTokenizer st = new StringTokenizer(line);//cityName = (String) st.nextElement();cityName = parts[0];//if(st.hasMoreElements())//code = (String) st.nextElement();if(parts.length==1)continue;code = parts[1];if(code.codePointAt(0)-64>0 && !code.contains("NULL"))//是可使用三字代碼{//將城市名按'\'劃分開,再去掉括弧中的內容String cityNames[] = cityName.split("/");for(int cityi=0;cityi<cityNames.length;++cityi){if(cityNames[cityi].contains("("))//刪除括弧中的內容{int beginIndex = cityNames[cityi].indexOf("(");cityNames[cityi] = (String) cityNames[cityi].subSequence(0,beginIndex);}if(cityNames[cityi].contains("("))//刪除括弧中的內容{int beginIndex = cityNames[cityi].indexOf("(");cityNames[cityi] = (String) cityNames[cityi].subSequence(0,beginIndex);}insertCity(cityNames[cityi],code);}}}} catch (FileNotFoundException e) {// TODO Auto-generated catch blockSystem.out.println("沒有找到input.txt");e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blockSystem.out.println("讀取行遇到錯誤");e.printStackTrace();}//將資料先用分析器分解開,每部分用'\'做劃分,和另一部分做叉乘//先尋找這個城市是否在字典中已經存在,若不存在則插入字典中}private int row(String cityName){int row = (cityName.codePointAt(0)>>8)-78;//Ditem item = new Ditem(cityName,code);//if(cityName.codePointAt(0)<19969)//如果城市是英文名稱if(row<0||row>mapNum-1)row = 81;return row;}public Ditem inquireByChinese(String ChineseName){Ditem item ;int row=row(ChineseName);Iterator<Ditem> iter = dic.chineseList[row].listIterator();while(iter.hasNext()){item = iter.next();if(item.ChineseName.contentEquals(ChineseName))return item;}return null;}private void insert(Ditem newItem){dic.chineseList[row(newItem.ChineseName)].add(newItem);}public String searchForCode(String ChineseName){int namelength = ChineseName.length();Ditem item = null;if(namelength > 0){item = inquireByChinese(ChineseName);}if(item!=null){return item.code[0];}return null;}private  void inputCountry(){File file = new File("D:\\input\\城市資訊20130109.xls");Workbook wb = null;try{wb = Workbook.getWorkbook(file);if(wb ==null){System.out.println("城市資訊20130109.xls沒有放在指定的路徑下");}Sheet[] sheets = wb.getSheets();if(sheets!=null && sheets.length>0){//long all = 0;//System.out.println("一共有"+sheets.length+"張表");for(int i=0;i<sheets.length;++i){int rowNum = sheets[i].getRows();for(int j=1;j<rowNum;++j){Cell[] cells = sheets[i].getRow(j);//實驗證明用三字碼作為查詢關鍵字更快一點,已耗用時間:1201(三字碼),1575(漢語名稱)//long startTime = System.nanoTime();  //開始時間Ditem item = inquireByCode(cells[0].getContents());Ditem item1 = inquireByChinese(cells[1].getContents());//Ditem item = inquireByChinese(cells[1].getContents());//long consumingTime = System.nanoTime() - startTime; //消耗時間        //System.out.println(consumingTime);        //System.out.println(consumingTime/1000+"微秒");       // all+=consumingTime;//System.out.println(item);if(item1 == null || item ==null || item!=item1)//出現了新名字或新代碼,或者出現了名字與代碼的交錯{//System.out.println(item);System.out.println(item1);if((item1 != null) && (item != null)){//全都不空,代表城市碼出現交叉(只有在同國家情況下出現),所以把國家資訊全部存入這兩個item.contry = cells[3].getContents();item.contryCode = cells[2].getContents();item1.contry = cells[3].getContents();item1.contryCode = cells[2].getContents();}else{//哪個空插入哪個,寧可冗餘也不丟失if(item != null)item = item1;item = insertCity(cells[1].getContents(),cells[0].getContents());item.contry = cells[3].getContents();item.contryCode = cells[2].getContents();}}else//正常情況直接插入國家資訊{item.contry = cells[3].getContents();item.contryCode = cells[2].getContents();}//System.out.println(item);}}//System.out.println("平均已耗用時間:"+all/3926);}wb.close();} catch(BiffException e){e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}private void inputRegion(){File file = new File("D:\\input\\國家資訊20130117.xls");Workbook wb = null;try{wb = Workbook.getWorkbook(file);if(wb ==null){System.out.println("國家資訊20130117.xls沒有放在指定的路徑下");}Sheet[] sheets = wb.getSheets();for (int i = 0; i < mapNum; ++i)for (Iterator<Ditem> iter = dic.chineseList[i].listIterator(); iter.hasNext();)// int j=0;j<dicList[0].size();++j){Ditem item = iter.next();Cell c = sheets[0].findCell(item.contryCode);//System.out.println(item.contryCode);if(c != null){//System.out.println(sheets[0].getRow(c.getRow())[6].getContents());item.region = sheets[0].getRow(c.getRow())[6].getContents().concat("(").concat(sheets[0].getRow(c.getRow())[7].getContents()).concat(")");//System.out.println(item.region);}}}catch(Exception e){System.out.println(e);e.printStackTrace();}} private void inputAC()//錄入每個城市可用的航空公司{File file = new File("D:\\input\\城市地區20130117.xls");Workbook wb = null;try{wb = Workbook.getWorkbook(file);if(wb ==null){System.out.println("城市地區20130117.xls沒有放在指定的路徑下");}Sheet[] sheets = wb.getSheets();for(int i=1;i<sheets[0].getRows();++i){Cell cells[] = sheets[0].getRow(i);String s = cells[4].getContents();String citys[] = s.split("/");for(int j=0;j<citys.length;++j){Ditem item = inquireByCode(citys[j]);if(item == null)continue;boolean exist = false;for(String ss : item.company){//如果家公司已經儲存在這個城市了if(ss.contains(cells[0].getContents())){exist = true;break;}}if(exist == false){item.company.add(cells[0].getContents());}}}//System.out.println(sheets[0].getRows());wb.close();}catch(Exception e){System.out.println(e);e.printStackTrace();}}//public static String readExcel(File file)//{int no=0;//StringBuffer sb = new StringBuffer();//Workbook wb = null;//try {////構造Workbook(活頁簿)對象//wb = Workbook.getWorkbook(file);//} catch(BiffException e) {//e.printStackTrace();//} catch(IOException e) {//e.printStackTrace();//}////if(wb == null)//return null;////獲得了Workbook對象之後,就可以通過它得到Sheet(工作表)對象了//Sheet[] sheet = wb.getSheets();////if(sheet!=null&&sheet.length>0){////對每個工作表進行迴圈//for(int i=0;i<sheet.length;++i)//{   //得到當前工作表的行數////System.out.println(++no);//int rowNum = sheet[i].getRows();//for(int j=0;j<rowNum;++j)//{//得到當前的所有儲存格////Cell[] cells = sheet[i].getRow(j);//if(cells!=null&&cells.length>0) {////對每個儲存格進行迴圈//for(int k=0;k<cells.length;++k)//{//String cellValue = cells[k].getContents();//sb.append(cellValue+"\t");//}//}//sb.append("\r\n");//}//sb.append("\r\n");//}//}////最後關閉資源,釋放記憶體//wb.close();//return sb.toString();//}//public static void main(String[] args) {// TODO Auto-generated method stub//String a = "A";//System.out.println(a.codePointAt(0));CityDictionary dic = new CityDictionary();//dic.bliudDictionary();//System.out.println(dic.searchForCode("天津"));//dic.displayOnCode();//dic.inputCountry();//dic.inputRegion();//dic.inputAC();//dic.display();//dic.output();//System.out.println(dic.inquireByCode("HJJ"));}private void output(){FileOutputStream fos;try {fos = new FileOutputStream("d:\\城市字典.dic");ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(dic);oos.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blockSystem.out.println("未發現輸出檔案錯誤");e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("object流錯誤");}}public void display()//顯示字典所有項,用於編程者研究記憶體結果{int no =0;System.out.println("字典內容:");for (int i = 0; i < mapNum; ++i)for (Iterator<Ditem> iter = dic.chineseList[i].listIterator(); iter.hasNext();)// int j=0;j<dicList[0].size();++j){Ditem item = iter.next();////System.out.print(item.ChineseName);//for (int k = 0; k < item.codeNum; ++k)//System.out.print("," + item.code[k]);//System.out.println();System.out.println(item);++no;}System.out.println("總共有"+no+"個城市的資訊");}public void displayOnCode(){for(int i=0;i<26;++i){for(int j=0;j<26;++j){for(Iterator<Ditem> iter = dic.codeList[i][j].listIterator();iter.hasNext();){Ditem item = iter.next();for(int k=0;k<item.codeNum;++k){if((item.code[k].codePointAt(0)-65==i)&&(item.code[k].codePointAt(1)-65==j))System.out.println(item.code[k]+"\t"+item.ChineseName);}}}}}}//Dictionary中儲存了兩個指標表,一個表由漢語名map出指標,另一個表用三字代碼map出指標class Dictionary implements Serializable{//字典的資料部分在這邊實現private final int mapNum = 82;public ArrayList<Ditem>[] chineseList;public ArrayList<Ditem>[][] codeList;Dictionary(){chineseList = new ArrayList[mapNum];for(int i=0;i<mapNum;++i){chineseList[i] = new ArrayList();}codeList = new ArrayList[26][26];for(int i=0;i<26;++i){codeList[i] = new ArrayList[26];for(int j=0;j<26;++j){codeList[i][j] = new ArrayList();}}}}//Ditem中儲存了城市的中文名稱、三字代碼、相關航空公司、國家、所屬地區class Ditem extends Object implements Comparable<Ditem>,Serializable {public String ChineseName;//城市的漢語名稱public int codeNum = 0;//城市的三字代碼的個數String[] code = new String[10];//三字代碼,一個城市最多有10個不同的三字代碼ArrayList<String> company = new ArrayList();//每個城市可飛的航空公司String region = null;String contry = null;String contryCode = null;Ditem(String chn)//用於{ChineseName = chn;}Ditem(String chn,String code)//用於新插入一個城市和三字代碼,建立字典時使用{ChineseName = chn;this.code[0] = code;codeNum = 1;}public void insertCode(String newcode){code[codeNum] = newcode;++codeNum;}public int compareTo(Ditem other) {// TODO Auto-generated method stubif(ChineseName.codePointAt(0)>other.ChineseName.codePointAt(0))return 1;if(ChineseName.codePointAt(0)<other.ChineseName.codePointAt(0))return -1;return 0;}public String toString(){String s = ChineseName;s=s.concat(":");for(int i=0;i<codeNum;++i){s=s.concat(code[i]).concat(",");}if(contry != null){s = s.concat(contry).concat(",").concat(contryCode);}if(region !=null){s = s.concat(",").concat(region).concat(",");}s=s.concat(company.toString());return s;}public void insertCompany(String com){company.add(com);}}

 

readme.txt使用方法:1、將“城市字典.dic”複製到d盤根目錄2、將程式複製到本地項目的類中或者import入項目3、在需要查詢城市資訊的地方執行個體化一個城市字典,如: CityDictionary dic = new CityDictionary();4、可以調用的方法:(1)通過三字代碼尋找一個城市的全部資訊。 Ditem item = inquireByCode("SHA");(可以忽略大小寫) 其中item點後會出現城市的多種資訊,直接使用即可;(2)通過城市名稱尋找一個城市的全部資訊。 Ditem
item = inquireByChinese("上海");(可以忽略大小寫) 其中item點後會出現城市的多種資訊,直接使用即可;(3)查詢一個漢語名稱對應的三字代碼 String code = searchForCode("上海");註:千萬不要改動和Ditem類與Dictionary類相關的任何代碼,否則需要使用bliudDictionary()方法重建“城市字典.dic”檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.