You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

schools.py 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Licensed under the Apache License, Version 2.0 (the "License");
  2. # you may not use this file except in compliance with the License.
  3. # You may obtain a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. #
  13. import os
  14. import json
  15. import re
  16. import copy
  17. import pandas as pd
  18. current_file_path = os.path.dirname(os.path.abspath(__file__))
  19. TBL = pd.read_csv(os.path.join(current_file_path, "res/schools.csv"), sep="\t", header=0).fillna("")
  20. TBL["name_en"] = TBL["name_en"].map(lambda x: x.lower().strip())
  21. GOOD_SCH = json.load(open(os.path.join(current_file_path, "res/good_sch.json"), "r"))
  22. GOOD_SCH = set([re.sub(r"[,. &()()]+", "", c) for c in GOOD_SCH])
  23. def loadRank(fnm):
  24. global TBL
  25. TBL["rank"] = 1000000
  26. with open(fnm, "r", encoding='utf-8') as f:
  27. while True:
  28. l = f.readline()
  29. if not l:break
  30. l = l.strip("\n").split(",")
  31. try:
  32. nm,rk = l[0].strip(),int(l[1])
  33. #assert len(TBL[((TBL.name_cn == nm) | (TBL.name_en == nm))]),f"<{nm}>"
  34. TBL.loc[((TBL.name_cn == nm) | (TBL.name_en == nm)), "rank"] = rk
  35. except Exception:
  36. pass
  37. loadRank(os.path.join(current_file_path, "res/school.rank.csv"))
  38. def split(txt):
  39. tks = []
  40. for t in re.sub(r"[ \t]+", " ",txt).split(" "):
  41. if tks and re.match(r".*[a-zA-Z]$", tks[-1]) and \
  42. re.match(r"[a-zA-Z]", t) and tks:
  43. tks[-1] = tks[-1] + " " + t
  44. else:tks.append(t)
  45. return tks
  46. def select(nm):
  47. global TBL
  48. if not nm:return
  49. if isinstance(nm, list):nm = str(nm[0])
  50. nm = split(nm)[0]
  51. nm = str(nm).lower().strip()
  52. nm = re.sub(r"[((][^()()]+[))]", "", nm.lower())
  53. nm = re.sub(r"(^the |[,.&()();;·]+|^(英国|美国|瑞士))", "", nm)
  54. nm = re.sub(r"大学.*学院", "大学", nm)
  55. tbl = copy.deepcopy(TBL)
  56. tbl["hit_alias"] = tbl["alias"].map(lambda x:nm in set(x.split("+")))
  57. res = tbl[((tbl.name_cn == nm) | (tbl.name_en == nm) | (tbl.hit_alias == True))]
  58. if res.empty:return
  59. return json.loads(res.to_json(orient="records"))[0]
  60. def is_good(nm):
  61. global GOOD_SCH
  62. nm = re.sub(r"[((][^()()]+[))]", "", nm.lower())
  63. nm = re.sub(r"[''`‘’“”,. &()();;]+", "", nm)
  64. return nm in GOOD_SCH