Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 logging
  14. import re
  15. import json
  16. import os
  17. import pandas as pd
  18. from rag.nlp import rag_tokenizer
  19. from . import regions
  20. current_file_path = os.path.dirname(os.path.abspath(__file__))
  21. GOODS = pd.read_csv(
  22. os.path.join(current_file_path, "res/corp_baike_len.csv"), sep="\t", header=0
  23. ).fillna(0)
  24. GOODS["cid"] = GOODS["cid"].astype(str)
  25. GOODS = GOODS.set_index(["cid"])
  26. CORP_TKS = json.load(
  27. open(os.path.join(current_file_path, "res/corp.tks.freq.json"), "r")
  28. )
  29. GOOD_CORP = json.load(open(os.path.join(current_file_path, "res/good_corp.json"), "r"))
  30. CORP_TAG = json.load(open(os.path.join(current_file_path, "res/corp_tag.json"), "r"))
  31. def baike(cid, default_v=0):
  32. global GOODS
  33. try:
  34. return GOODS.loc[str(cid), "len"]
  35. except Exception:
  36. pass
  37. return default_v
  38. def corpNorm(nm, add_region=True):
  39. global CORP_TKS
  40. if not nm or isinstance(nm, str):
  41. return ""
  42. nm = rag_tokenizer.tradi2simp(rag_tokenizer.strQ2B(nm)).lower()
  43. nm = re.sub(r"&", "&", nm)
  44. nm = re.sub(r"[\(\)()\+'\"\t \*\\【】-]+", " ", nm)
  45. nm = re.sub(
  46. r"([—-]+.*| +co\..*|corp\..*| +inc\..*| +ltd.*)", "", nm, 10000, re.IGNORECASE
  47. )
  48. nm = re.sub(
  49. r"(计算机|技术|(技术|科技|网络)*有限公司|公司|有限|研发中心|中国|总部)$",
  50. "",
  51. nm,
  52. 10000,
  53. re.IGNORECASE,
  54. )
  55. if not nm or (len(nm) < 5 and not regions.isName(nm[0:2])):
  56. return nm
  57. tks = rag_tokenizer.tokenize(nm).split()
  58. reg = [t for i, t in enumerate(tks) if regions.isName(t) and (t != "中国" or i > 0)]
  59. nm = ""
  60. for t in tks:
  61. if regions.isName(t) or t in CORP_TKS:
  62. continue
  63. if re.match(r"[0-9a-zA-Z\\,.]+", t) and re.match(r".*[0-9a-zA-Z\,.]+$", nm):
  64. nm += " "
  65. nm += t
  66. r = re.search(r"^([^a-z0-9 \(\)&]{2,})[a-z ]{4,}$", nm.strip())
  67. if r:
  68. nm = r.group(1)
  69. r = re.search(r"^([a-z ]{3,})[^a-z0-9 \(\)&]{2,}$", nm.strip())
  70. if r:
  71. nm = r.group(1)
  72. return nm.strip() + (("" if not reg else "(%s)" % reg[0]) if add_region else "")
  73. def rmNoise(n):
  74. n = re.sub(r"[\((][^()()]+[))]", "", n)
  75. n = re.sub(r"[,. &()()]+", "", n)
  76. return n
  77. GOOD_CORP = set([corpNorm(rmNoise(c), False) for c in GOOD_CORP])
  78. for c, v in CORP_TAG.items():
  79. cc = corpNorm(rmNoise(c), False)
  80. if not cc:
  81. logging.debug(c)
  82. CORP_TAG = {corpNorm(rmNoise(c), False): v for c, v in CORP_TAG.items()}
  83. def is_good(nm):
  84. global GOOD_CORP
  85. if nm.find("外派") >= 0:
  86. return False
  87. nm = rmNoise(nm)
  88. nm = corpNorm(nm, False)
  89. for n in GOOD_CORP:
  90. if re.match(r"[0-9a-zA-Z]+$", n):
  91. if n == nm:
  92. return True
  93. elif nm.find(n) >= 0:
  94. return True
  95. return False
  96. def corp_tag(nm):
  97. global CORP_TAG
  98. nm = rmNoise(nm)
  99. nm = corpNorm(nm, False)
  100. for n in CORP_TAG.keys():
  101. if re.match(r"[0-9a-zA-Z., ]+$", n):
  102. if n == nm:
  103. return CORP_TAG[n]
  104. elif nm.find(n) >= 0:
  105. if len(n) < 3 and len(nm) / len(n) >= 2:
  106. continue
  107. return CORP_TAG[n]
  108. return []