extractcity.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package main
  2. import (
  3. qu "qfw/util"
  4. "go.mongodb.org/mongo-driver/bson/primitive"
  5. "github.com/go-ego/gse"
  6. )
  7. //省
  8. type Province struct {
  9. Name string
  10. Brief string
  11. Cap string
  12. Captial *City
  13. }
  14. //市
  15. type City struct {
  16. Name string
  17. Brief string
  18. P *Province
  19. }
  20. //区或县
  21. type District struct {
  22. Name string
  23. C *City
  24. }
  25. //街道
  26. type Street struct {
  27. Name string
  28. D *District
  29. }
  30. //村、社区、居委会
  31. type Community struct {
  32. Name string
  33. S *Street
  34. }
  35. //区或县简称对应的全称和市信息
  36. type DistrictSimFull struct {
  37. SimName string
  38. FullName string
  39. C *City
  40. }
  41. //打分信息
  42. type ScoreInfo struct {
  43. FullAreaScore map[string]float64
  44. FullCityScore map[string]float64
  45. FullDistrictScore map[string]float64
  46. SimAreaScore map[string]float64
  47. SimCityScore map[string]float64
  48. SimDistrictScore map[string]float64
  49. }
  50. var (
  51. ProvinceMap map[string]string //省全称简称(key:浙江省 val:浙江)
  52. ProvinceBriefMap map[string]*Province //省简称对应的省信息(key:浙江 val:&Province{})
  53. CityMap map[string]string //市全称简称(key:杭州市 val:杭州)
  54. CityBriefMap map[string]*City //市简称对应的市信息(key:杭州 val:&City{})
  55. CityFullMap map[string]*City //市全称对应的市信息(key:杭州市 val:&City{})
  56. DistrictCityMap map[string]*City //
  57. NewDistrictCityMap map[string][]*City //区或县全称对应的city(全国有相同名称的区或县,这里对应的city用slice)
  58. DistrictSimAndAll map[string]string //区或县(key:简称 val:全称)
  59. NewDistrictSimAndAll map[string][]map[string]*City //区或县(key:简称 val: 相同简称的区全称:所在市)
  60. StreetDistrictMap map[string]*District //街道对应的区或县
  61. NewStreetDistrictMap map[string][]*District //街道全称对应的区或县
  62. CommunityDistrictMap map[string][]*District //村、居委会对应的区或县
  63. Trie_Full_Province *Trie //省全称 省、直辖市、自治区
  64. Trie_Full_City *Trie //市全称 地级市
  65. Trie_Full_District *Trie //县全称 市辖区、县(旗)、县级市、自治县(自治旗)、特区、林区
  66. Trie_Full_Street *Trie //街道、乡镇全称 镇、乡、民族乡、县辖区、街道
  67. Trie_Full_Community *Trie //村/委员会全称 村、居委会
  68. Trie_Sim_Province *Trie //省简称
  69. Trie_Sim_City *Trie //市简称
  70. Trie_Sim_District *Trie //县简称
  71. Trie_Fulls []*Trie //所有全称
  72. Trie_Sims []*Trie //所有简称
  73. Seg_SV *gse.Segmenter //分词
  74. // Seg_PCD *gse.Segmenter //分词
  75. )
  76. func InitInfo() {
  77. InitVar() //初始化变量
  78. InitSeg() //初始化分词
  79. InitCity() //初始化城市信息
  80. }
  81. //初始化变量
  82. func InitVar() {
  83. defer qu.Catch()
  84. //初始化Trie
  85. //全称
  86. Trie_Full_Province = &Trie{}
  87. Trie_Full_City = &Trie{}
  88. Trie_Full_District = &Trie{}
  89. Trie_Full_Street = &Trie{}
  90. Trie_Full_Community = &Trie{}
  91. //简称
  92. Trie_Sim_Province = &Trie{}
  93. Trie_Sim_City = &Trie{}
  94. Trie_Sim_District = &Trie{}
  95. //初始化分词
  96. // Seg_PCD = &gse.Segmenter{}
  97. Seg_SV = &gse.Segmenter{}
  98. //初始化map
  99. ProvinceMap = make(map[string]string)
  100. CityMap = make(map[string]string)
  101. DistrictSimAndAll = make(map[string]string)
  102. NewDistrictSimAndAll = make(map[string][]map[string]*City)
  103. CityBriefMap = make(map[string]*City)
  104. CityFullMap = make(map[string]*City)
  105. ProvinceBriefMap = make(map[string]*Province)
  106. NewDistrictCityMap = make(map[string][]*City)
  107. NewStreetDistrictMap = make(map[string][]*District)
  108. CommunityDistrictMap = make(map[string][]*District)
  109. }
  110. //初始化分词
  111. func InitSeg() {
  112. defer qu.Catch()
  113. // Seg_PCD.LoadDict("res/pcd.txt")
  114. Seg_SV.LoadDict("res/sv.txt")
  115. }
  116. //初始化城市信息
  117. func InitCity() {
  118. defer qu.Catch()
  119. InitCityAll() //城市全称信息
  120. InitCitySim() //城市简称信息
  121. Trie_Fulls = []*Trie{Trie_Full_Province, Trie_Full_City, Trie_Full_District, Trie_Full_Street, Trie_Full_Community}
  122. Trie_Sims = []*Trie{Trie_Sim_Province, Trie_Sim_City, Trie_Sim_District}
  123. }
  124. func InitCityAll() {
  125. query := map[string]interface{}{
  126. "s_type": "cityall",
  127. "delete": false,
  128. "s_version": Version,
  129. }
  130. list, _ := MgoExt.Find(VersionColl, query, nil, nil)
  131. qu.Debug("all---", len(list))
  132. for _, v := range list {
  133. name := qu.ObjToString(v["s_name"])
  134. tmp := v["content"].(map[string]interface{})
  135. //加载省信息
  136. Trie_Full_Province.AddWords(name) //加入省全称Trie(k:浙江省)
  137. p := &Province{}
  138. p.Name = name //省全称:浙江省
  139. p.Brief = tmp["brief"].(string) //省简称:浙江
  140. Trie_Sim_Province.AddWords(p.Brief) //加入省简称Trie(k:浙江)
  141. ProvinceMap[name] = p.Brief //浙江省:浙江
  142. ProvinceBriefMap[p.Brief] = p //浙江:省信息{}
  143. p.Cap = tmp["captial"].(string) //省会(杭州)
  144. //加载市信息
  145. city, _ := tmp["city"].(map[string]interface{})
  146. for k, v := range city {
  147. Trie_Full_City.AddWords(k) //加入市全称Trie(k:杭州市)
  148. v1m, _ := v.(map[string]interface{})
  149. c := &City{}
  150. c.Name = k //市全称:杭州市
  151. c.Brief = v1m["brief"].(string) //市简称:杭州
  152. Trie_Sim_City.AddWords(c.Brief) //加入市简称Trie(k:杭州)
  153. CityMap[k] = c.Brief //杭州市:杭州
  154. CityBriefMap[c.Brief] = c //杭州:市信息{}
  155. CityFullMap[k] = c //杭州市:市信息{}
  156. c.P = p
  157. if c.Name == p.Cap {
  158. p.Captial = c //加载province中的省会市信息{}
  159. }
  160. //区县
  161. districtmap, _ := v1m["area"].(map[string]interface{}) //区或县
  162. for district, streets := range districtmap {
  163. d := &District{}
  164. d.Name = district
  165. d.C = c
  166. //省直辖市,河南济源市没有区一级,目前区一级写的还是济源市
  167. //匹配时,如果匹配到区,拿区和市比对,相同则代表是省直辖市,不要区一级
  168. Trie_Full_District.AddWords(district) //加入区或县全称Trie
  169. ctmp := NewDistrictCityMap[district]
  170. if len(ctmp) == 0 {
  171. tmpcarr := []*City{c}
  172. NewDistrictCityMap[district] = tmpcarr
  173. } else {
  174. NewDistrictCityMap[district] = append(NewDistrictCityMap[district], c)
  175. }
  176. //街道
  177. streetmap, _ := streets.(map[string]interface{})
  178. for street, communitys := range streetmap {
  179. s := &Street{}
  180. s.Name = street
  181. s.D = d
  182. Trie_Full_Street.AddWords(street) //加入街道全称Trie
  183. dtmp := NewStreetDistrictMap[street]
  184. if len(dtmp) == 0 {
  185. tmpdarr := []*District{d}
  186. NewStreetDistrictMap[street] = tmpdarr
  187. } else {
  188. NewStreetDistrictMap[street] = append(NewStreetDistrictMap[street], d)
  189. }
  190. //村、居委会
  191. for _, ct_tmp := range communitys.(primitive.A) {
  192. ct := qu.ObjToString(ct_tmp)
  193. Trie_Full_Community.AddWords(ct) //加入居委会、村全称Trie
  194. cttmp := CommunityDistrictMap[ct]
  195. if len(cttmp) == 0 {
  196. tmpdarr := []*District{d}
  197. CommunityDistrictMap[ct] = tmpdarr
  198. } else {
  199. CommunityDistrictMap[ct] = append(CommunityDistrictMap[ct], d)
  200. }
  201. }
  202. }
  203. }
  204. }
  205. }
  206. }
  207. func InitCitySim() {
  208. defer qu.Catch()
  209. query := map[string]interface{}{
  210. "s_type": "citysim",
  211. "delete": false,
  212. "s_version": Version,
  213. }
  214. list, _ := MgoExt.Find(VersionColl, query, nil, nil)
  215. qu.Debug("sim---", len(list))
  216. for _, v := range list {
  217. tmp := v["content"].(map[string]interface{})
  218. city, _ := tmp["city"].(map[string]interface{})
  219. for _, v1 := range city {
  220. v1m, _ := v1.(map[string]interface{})
  221. cb := v1m["brief"].(string) //市简称
  222. arr := v1m["area"].(map[string]interface{}) //区或县简称
  223. for districtsim, districtall := range arr {
  224. dfullstr, _ := districtall.(string)
  225. Trie_Sim_District.AddWords(districtsim) //加入区或县简称Trie
  226. c := CityBriefMap[cb]
  227. dfullarr := NewDistrictSimAndAll[districtsim]
  228. dfullcity := map[string]*City{dfullstr: c}
  229. if len(dfullarr) == 0 {
  230. tmparr := []map[string]*City{dfullcity}
  231. NewDistrictSimAndAll[districtsim] = tmparr
  232. } else {
  233. NewDistrictSimAndAll[districtsim] = append(NewDistrictSimAndAll[districtsim], dfullcity)
  234. }
  235. }
  236. }
  237. }
  238. }