Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

schools.py 2.8KB

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