def __DoGet_Admin_name(self, code_dict, code_country_dict): self.log.info('begin admin_name ..') sqlcmd = ''' select id, name,country_name from ( select code as id, "name" as name,"name" as country_name from temp_country union select a.code as id,a."name" as name,b."name" as country_name from temp_state as a left join temp_country as b on a.country_code = b.code union select a.code as id, a."name" as name, c."name" as country_name from temp_city as a left join temp_state as b on a.state_code = b.code left join temp_country as c on b.country_code = c.code )as temp_all where name is not null order by id, name, country_name; ''' #test set_language self.CreateTable2('temp_admin_name') MultiLangName.set_language_code(code_dict) temp_file_obj = cache_file.open('temp_admin_name_new') rows = self.get_batch_data(sqlcmd) for row in rows: id = row[0] name = row[1] country_name = row[2] if name: ml_name = MultiLangName(code_country_dict[country_name], name, NAME_TYPE_OFFICIAL) json_name = ml_name.json_format_dump() if not json_name: self.log.error('Json Name is null. id=%d' % id) else: self.__store_name_to_temp_file(temp_file_obj, id, json_name, code_country_dict[country_name]) # ## 把名称导入数据库 temp_file_obj.seek(0) self.pg.copy_from2(temp_file_obj, 'temp_admin_name') self.pg.commit2() # close file #temp_file_obj.close() cache_file.close(temp_file_obj, True) self.log.info('end admin_name ..')
def _name_to_json(self, strTTS): code_dict = dict() code_dict['JPN'] = None MultiLangName.set_language_code(code_dict) ml_name = MultiLangName('JPN', strTTS, NAME_TYPE_OFFICIAL) strTTS_json = ml_name.json_format_dump() return strTTS_json
def __init__(self, lang_code, name, name_type='office_name', tts_type='not_tts', left_right='left_right'): ''' Constructor ''' MultiLangName.__init__(self, self._change_language_code(lang_code), name, name_type, tts_type, left_right)
def merge_links_name(self, link_list): road_name_list = [] road_number_list = [] self.pg.connect2() for road_names, road_numbers in self._get_links_name_number(link_list): if road_names: self._merge_name(road_name_list, road_names) if road_numbers: self._merge_number(road_number_list, road_numbers) # 把番号追加到名称的结尾 self._append_num_2_name(road_number_list, road_name_list) json_name = MultiLangName.json_format_dump2(road_name_list) json_shield = MultiLangName.json_format_dump2(road_number_list) return json_name, json_shield
def _get_link_names(self, normal_link_names): '''道路番号 + 道路名称''' # 按道路种别排序 sorted_values = sorted(normal_link_names.values(), cmp=lambda x, y: cmp(x[0], y[0])) # 求road_type等级最高的道路名称 prev_road_type = 16 rst_route_nums = [] rst_road_names = [] for (road_type, road_names) in sorted_values: if(road_type > prev_road_type and (rst_route_nums or rst_road_names)): break prev_road_type = road_type temp_road_name = None if road_names: for name_dict in road_names: if name_dict.get("type") == "route_num": # 道路番号 if name_dict not in rst_route_nums: rst_route_nums.append(name_dict) else: if not temp_road_name: # 只取第一个官名 temp_road_name = name_dict if temp_road_name and temp_road_name not in rst_road_names: rst_road_names.append(temp_road_name) # 道路番号 + 道路名称 num_names = rst_route_nums + rst_road_names if num_names: json = MultiLangName.json_format_dump2([num_names]) return json return None
def set_language_code(self): from component.default.multi_lang_name import MultiLangName if not MultiLangName.is_initialized(): self.log.info('Set language Code.') self.pg.connect2() sqlcmd = """ select language_code from language_tbl order by language_code; """ code_dict = dict() language_codes = self.get_batch_data(sqlcmd) for lang_info in language_codes: lang_code = lang_info[0] code_dict[lang_code] = None MultiLangName.set_language_code(code_dict) return 0
def _set_phoneme(self, ml_name, phoneme_dict, key): # key = ('MN', official_name) if not phoneme_dict: return phoneme_text = phoneme_dict.get(key) if phoneme_text: tts = MultiLangName(ml_name.get_lang_code(), phoneme_text, ml_name.get_name_type(), TTS_TYPE_PHONEME) ml_name.set_tts(tts)
def _set_regional_name(self, ml_name, reg_name_dict, key): if not reg_name_dict: return name_info_list = reg_name_dict.get(key) if name_info_list: for lang_code, reg_name in name_info_list: trans = MultiLangName(lang_code, reg_name, ml_name.get_name_type()) ml_name.add_trans(trans)
def _set_regional_name_fuzzy(self, ml_name, reg_name_dict, key): '''模糊搜索''' for reg_key, name_info_list in reg_name_dict.iteritems(): if (reg_key[0] == key[0] # 名称种别相同 and key[1].find(reg_key[1]) == 0): if name_info_list: for lang_code, reg_name in name_info_list: trans = MultiLangName(lang_code, reg_name, ml_name.get_name_type()) ml_name.add_trans(trans)
def _make_signpost_element(self): self.log.info('Start Make SignPost element.') sqlcmd = """ SELECT folder, link_id as out_link_id, rd_signs, l_country, gid as sign_id FROM org_processed_line AS line WHERE rd_signs IS NOT NULL AND rd_signs <> '' order by folder, out_link_id, sign_id; """ self.CreateTable2('mid_temp_signpost') temp_file_obj = cache_file.open('signpost_element') # 创建临时文件 signs = self.get_batch_data(sqlcmd) for sign_info in signs: folder = sign_info[0] # folder名/区域名 out_link_id = int(sign_info[1]) rd_signs = sign_info[2] country = sign_info[3] sign_id = sign_info[4] lang_code = LANGUAGE_CODE.get(country) link_end_pos = rd_signs.index(LIND_END_CHAR) in_link_id = int(rd_signs[:link_end_pos]) exitno_end_pos = rd_signs.index(EXIT_NO_END_CHAR) exitno = rd_signs[link_end_pos + 1:exitno_end_pos].strip() if rd_signs[exitno_end_pos + 1] == 'T': name = rd_signs[exitno_end_pos + 2:] else: self.log.warning('Unkown Name Type. rd_signs=%s' % rd_signs) if name: signpost_name = MultiLangName(lang_code, name, NAME_TYPE_OFFICIAL) else: # print folder, out_link_id signpost_name = None multi_exitno = self._get_exit_no(exitno, lang_code) sign_post = SignPostElementMmi(sign_id, folder) sign_post.set_exit_no(multi_exitno) sign_post.set_signpost_name(signpost_name) if sign_post.is_empty(): # 名称、route_no和exit_no都为空 self.log.warning('is empty folder=%s, out_link_id=%s.' % (folder, out_link_id)) continue str_info = sign_post.to_string() self._store_name_to_temp_file(temp_file_obj, in_link_id, out_link_id, str_info) # ## 把名称导入数据库 temp_file_obj.seek(0) self.pg.copy_from2(temp_file_obj, 'mid_temp_signpost') self.pg.commit2() # close file #temp_file_obj.close() cache_file.close(temp_file_obj, True) self.log.info('End Make SignPost element.') return 0
def store_language_flag(self): '''保存当前地图使用语言.''' from component.default.multi_lang_name import MultiLangName sqlcmd = """UPDATE language_tbl SET exist_flag = True WHERE language_code = %s; """ languages = MultiLangName.get_language_code() for lang_code, flag in languages.iteritems(): if flag: self.pg.execute2(sqlcmd, (lang_code, )) self.pg.commit2()
def _get_exit_no(self, exitno, lang_code): exitno = exitno.strip() if exitno in ERROR_EXIST_NOS: return None if not exitno or exitno == INVALID_EXIT_NO: return None # check字母开头 temp = re.findall('^\D', exitno) if temp: self.log.warning( "Start Char of Exit No is not digit. exist_no=%s" % exitno) return MultiLangName(lang_code, exitno, NAME_TYPE_OFFICIAL)
def _get_multi_name(self, road_name_list, road_number_list, lang_code): '''取得多语言多个名称的名称''' multi_name = None # ## 名称 for name in road_name_list: if not multi_name: multi_name = MultiLangName(lang_code, name, NAME_TYPE_OFFICIAL) else: alte_name = MultiLangName(lang_code, name, NAME_TYPE_ALTER) multi_name.add_alter(alte_name) # ## 番号 for shieldid, number in road_number_list: # ## 把番号添加名称 if number in road_name_list: continue if not multi_name: multi_name = MultiLangName(lang_code, number, NAME_TYPE_ROUTE_NUM) else: alte_name = MultiLangName(lang_code, number, NAME_TYPE_ROUTE_NUM) multi_name.add_alter(alte_name) return multi_name
def _get_multi_name(self, lang_code, str_name, name_type, org_name_types=None, phoneme_dict=None, reg_name_dict=None): ml_name = None if str_name: ml_name = MultiLangName(lang_code, str_name, name_type) if org_name_types: for org_type in org_name_types: key = (org_type, str_name.lower()) self._set_phoneme(ml_name, phoneme_dict, key) # regional name做成官方语的翻译 self._set_regional_name(ml_name, reg_name_dict, key) return ml_name
def _get_link_names_2(self, normal_link_names): '''道路番号 + 道路名称''' # 按道路种别排序 sorted_values = sorted(normal_link_names.values(), cmp=lambda x, y: cmp(x[0], y[0])) # 求road_type等级最高的道路名称 prev_road_type = 16 rst_route_nums = [] rst_road_names = [] for (road_type, road_names, road_shields) in sorted_values: if(road_type > prev_road_type and (rst_route_nums or rst_road_names)): break prev_road_type = road_type if road_shields: for shield_dict in road_shields: shield_dict = deepcopy(shield_dict) shield_num = shield_dict.get("val") # shield_id + \t + num num = (shield_num.split('\t'))[1] shield_dict["val"] = num shield_dict["type"] = "route_num" if shield_dict not in rst_route_nums: rst_route_nums.append(shield_dict) temp_road_name = None if road_names: for name_dict in road_names: if name_dict.get("type") != "route_num": # 不是道路番号 if not temp_road_name: # 只取第一个官名 temp_road_name = name_dict if temp_road_name and temp_road_name not in rst_road_names: rst_road_names.append(temp_road_name) # 道路番号 + 道路名称 num_names = rst_route_nums + rst_road_names if num_names: json = MultiLangName.json_format_dump2([num_names]) return json return None
def __DoGet_Admin_name(self,code_dict,code_country_dict,code_country_code): self.log.info('begin admin_name ..') sqlcmd = ''' select id,name,name_alt,country_name from ( select id,"name" as name,"names" as name_alt,"name" as country_name from ( select id,"name","names" from org_area where kind = '10' )as temp union select a.id,a.stt_nme as name,a.stt_alt as name_alt,b."name" as country_name from ( select stt_id as id,stt_nme,stt_alt,parent_id from org_state_region where kind = '9' group by stt_id,stt_nme,stt_alt,parent_id order by stt_id,stt_nme,stt_alt,parent_id )as a left join ( select id,"name" from org_area where kind = '10' group by id,"name" order by id,"name" )as b on a.parent_id = b.id union select dst_id as id,dst_nme as name,dst_alt as name_alt,d."name" as country_name from org_district_region as c left join ( select state.id,country."name" from ( select stt_id as id,parent_id from org_state_region where kind = '9' group by stt_id,parent_id order by stt_id,parent_id )as state left join ( select id,"name" from org_area where kind = '10' group by id,"name" order by id,"name" )as country on state.parent_id = country.id )as d on c.parent_id = d.id group by dst_id,dst_nme,dst_alt,d."name" )as temp_all where (name is not null) or (name_alt is not null) order by id,name,name_alt,country_name; ''' #test set_language MultiLangName.set_language_code(code_dict) temp_file_obj = cache_file.open('temp_admin_name_new') rows = self.get_batch_data(sqlcmd) for row in rows: id = int(row[0]) name = row[1] namealtes = row[2] country_name = row[3] if name: ml_name = MultiLangName(code_country_dict[country_name], name, NAME_TYPE_OFFICIAL) if namealtes: # ";" separation for namealte in namealtes.split(';'): alt_name = MultiLangName(code_country_dict[country_name], namealte, NAME_TYPE_ALTER) if ml_name: ml_name.add_alter(alt_name) else: ml_name = alt_name json_name = ml_name.json_format_dump() if not json_name: self.log.error('Json Name is null. id=%d' % id) else: self.__store_name_to_temp_file(temp_file_obj, id, json_name,code_country_code[country_name]) # ## 把名称导入数据库 temp_file_obj.seek(0) self.pg.copy_from2(temp_file_obj, 'temp_admin_name') self.pg.commit2() # close file #temp_file_obj.close() cache_file.close(temp_file_obj,True) self.log.info('end admin_name ..')
def _make_link_name(self): '''道路名称(包括显示用番号)''' self.log.info('Make Link Name.') sqlcmd = """ SELECT link.id::BIGINT, "name", pop_name, alt_name, namelc, routenum, phoneme.types as phoneme_types, phoneme.name_englishs as phoneme_name_engs, phoneme.name_nuances as phoneme_nuances, regional.types as regional_name_types, regional.name_englishs as regional_name_engs, name_regionals as regional_names, regional_lang_types FROM ( SELECT id, "name", base_nme, pop_name, alt_name, namelc, routenum FROM org_city_nw_gc_polyline WHERE "name" is not null or pop_name is not null or alt_name is not null or routenum is not null ) AS link FULL JOIN mid_temp_link_name_phoneme AS phoneme ON link.id = phoneme.id FULL JOIN mid_temp_link_regional_name as regional on link.id = regional.id ; """ temp_file_obj = cache_file.open('link_name') # 创建临时文件 self.CreateTable2('temp_link_name') names = self.get_batch_data(sqlcmd) for name_info in names: link_id = name_info[0] official_name = name_info[1] pop_name = name_info[2] alt_names = name_info[3] lang_code = name_info[4] routenum = name_info[5] # ## 音素 phoneme_name_types = name_info[6] # MN:official, AN: Alter phoneme_name_engs = name_info[7] phoneme_nuances = name_info[8] # ## regional regional_name_types = name_info[9] regional_name_engs = name_info[10] regional_names = name_info[11] regional_lang_types = name_info[12] if not link_id: self.log.error('Link id is None.') continue # 有其他种别的Phoneme if phoneme_name_types: s1 = set(phoneme_name_types) if len(s1) > 1: self.log.warning('Exist other name type. type=%s' % s1) # 有其他种别的小语种名称 if regional_name_types: s2 = set(regional_name_types) if len(s2) > 1: self.log.warning('Exist other regional name type. type=%s' % s2) phoneme_dict = self._get_phoneme_dict(phoneme_name_types, phoneme_name_engs, phoneme_nuances ) reg_name_dict = self._get_regional_dict(regional_name_types, regional_name_engs, regional_names, regional_lang_types ) # ## 官方名称 ml_name = self._get_multi_name(lang_code, official_name, NAME_TYPE_OFFICIAL, ['MN'], phoneme_dict, reg_name_dict, ) if official_name: # 没有做成到TTS if phoneme_nuances and not ml_name.has_tts(): self.log.warning("Can't find phoneme text. LinkId=%s" % link_id) if regional_names and not ml_name.has_trans(): self.log.warning("Can't find Regional Name. LinkId=%s" % link_id) key = ('MN', official_name.lower()) self._set_regional_name_fuzzy(ml_name, reg_name_dict, key ) # ## POP Name ml_pop = self._get_multi_name(lang_code, pop_name, NAME_TYPE_ALTER, ['MN', 'AN'], phoneme_dict, reg_name_dict, ) if ml_name: ml_name.add_alter(ml_pop) else: ml_name = ml_pop # ## Alter Name if alt_names: # Multiple names with ";" separation alt_names=sorted(alt_names.split(';')) for alt_name in alt_names: ml_alt = self._get_multi_name(lang_code, alt_name, NAME_TYPE_ALTER, ['AN'], phoneme_dict, reg_name_dict, ) if ml_name: ml_name.add_alter(ml_alt) else: ml_name = ml_alt if routenum: shield = ShieldMmi() if shield.is_name_include_number([official_name], routenum): pass else: ml_num = MultiLangName(lang_code, routenum, NAME_TYPE_ROUTE_NUM ) if ml_name: ml_name.add_alter(ml_num) else: ml_name = ml_num json_name = ml_name.json_format_dump() if not json_name: self.log.error('Json Name is none. linkid=%d' % link_id) # 存到本地的临时文件 self._store_name_to_temp_file(temp_file_obj, link_id, json_name ) # ## 把名称导入数据库 temp_file_obj.seek(0) self.pg.copy_from2(temp_file_obj, 'temp_link_name') self.pg.commit2() # close file #temp_file_obj.close() cache_file.close(temp_file_obj,True) self.log.info('End Make Link Name.') return 0
def _make_signpost_element(self): self.log.info('Start Make SignPost element.') sqlcmd = """ SELECT folder, sign_id, array_agg("row") as "rows", array_agg("column") as "columns", array_agg(itemtype) as itemtypes, array_agg(iteminfo) as iteminfos FROM ( SELECT gid, sign_id::BIGINT, "row"::INTEGER, "column"::INTEGER, itemtype::INTEGER, iteminfo, folder FROM org_signpost_table_2 order by folder, sign_id, "row", "column", gid ) AS A GROUP BY folder, sign_id ORDER BY folder, sign_id; """ self.CreateTable2('mid_temp_signpost_element') temp_file_obj = cache_file.open('signpost_element') # 创建临时文件 signs = self.get_batch_data(sqlcmd) for sign_info in signs: folder = sign_info[0] # folder名or区域名 sign_id = sign_info[1] # 看板id # rows = sign_info[2] # 方向番号 # columns = sign_info[3] # 同个方向内顺序号 itemtypes = sign_info[4] # 种别 iteminfos = sign_info[5] # 种别 sign_post = SignPostElementMmi(sign_id, folder) exit_no = None route_no = None signpost_name = None lang_code = MMI_OFFICIAL_LANG_CODE for itemtype, iteminfo in zip(itemtypes, iteminfos): # 去掉距离 name = self._delete_distance_str(iteminfo) if not name: # 去掉只有距离,没有名称的记录 # self.log.info(iteminfo) continue sign_type = self._get_sign_type(itemtype) if not sign_type: # 空不收录 continue name_type = self._cvt_name_type(sign_type) # ## 处理各种种别的数据 if SIGN_POST_TYPE_SHIELD_ID == sign_type: # shield id route_no = self._get_route_no(name, lang_code) if not route_no: # 'National Highway 5', 'National Highway 5 10 Km' c = re.compile(r'^National\s*Highway', re.I) if not c.findall(name): self.log.error('Error RouteNo. ' 'SignId=%s,Folder=%s,info=%s' % (sign_id, folder, iteminfo)) # 'National Highway' replace 'NH' routenum = c.sub('NH', name) info_list = routenum.split(' ') length = len(info_list) p = re.compile(r'\D+') # len=2, 番号是数字,如:'National Highway 5'改为'NH 5',并做成番号 if length == 2 and not p.findall(info_list[1]): route_no = self._get_route_no(routenum, lang_code) else: # 把这种'National Highway 5 10 Km', 做成方面名称 # 由于距离被去掉,这里一般不会再执行 self.log.error('Error RouteNo. ' 'SignId=%s,Folder=%s,info=%s' % (sign_id, folder, iteminfo)) continue if route_no: sign_post.add_route_no(route_no) else: self.log.error('Get RouteNo Failed. ' 'SignId=%s,Folder=%s,type=%s,info=%s' % (sign_id, folder, itemtype, iteminfo)) elif SIGN_POST_TYPE_EXIT_NO == sign_type: # 出口番号 # 存在多个出口番号,其他出口番号做出口番号的别名 e_no = MultiLangName(lang_code, name, name_type) if exit_no: exit_no.add_alter(e_no) else: # 第一个出口番号 exit_no = e_no elif SIGN_POST_TYPE_NAME == sign_type: # 方面名称 sign_name = MultiLangName(lang_code, name, name_type) if signpost_name: # 其他番号 signpost_name.add_alter(sign_name) else: # 第一个名称 signpost_name = sign_name else: continue if route_no or exit_no or signpost_name: sign_post.set_exit_no(exit_no) sign_post.set_signpost_name(signpost_name) str_info = sign_post.to_string() if str_info: self._store_name_to_temp_file(temp_file_obj, str_info) else: self.log.error('No SignPost Info. ' 'Sign_id=%d, Folder=%s' % (sign_id, folder)) else: self.log.warning('No route_no/exit_no/signpost_name.' 'Sign_id=%d, Folder=%s' % (sign_id, folder)) # ## 把名称导入数据库 temp_file_obj.seek(0) self.pg.copy_from2(temp_file_obj, 'mid_temp_signpost_element') self.pg.commit2() # close file #temp_file_obj.close() cache_file.close(temp_file_obj, True) self.log.info('End Make SignPost element.') return 0