您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

corporations.py 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import re,json,os
  2. import pandas as pd
  3. from rag.nlp import rag_tokenizer
  4. from . import regions
  5. current_file_path = os.path.dirname(os.path.abspath(__file__))
  6. GOODS = pd.read_csv(os.path.join(current_file_path, "res/corp_baike_len.csv"), sep="\t", header=0).fillna(0)
  7. GOODS["cid"] = GOODS["cid"].astype(str)
  8. GOODS = GOODS.set_index(["cid"])
  9. CORP_TKS = json.load(open(os.path.join(current_file_path, "res/corp.tks.freq.json"), "r"))
  10. GOOD_CORP = json.load(open(os.path.join(current_file_path, "res/good_corp.json"), "r"))
  11. CORP_TAG = json.load(open(os.path.join(current_file_path, "res/corp_tag.json"), "r"))
  12. def baike(cid, default_v=0):
  13. global GOODS
  14. try:
  15. return GOODS.loc[str(cid), "len"]
  16. except Exception as e:
  17. pass
  18. return default_v
  19. def corpNorm(nm, add_region=True):
  20. global CORP_TKS
  21. if not nm or type(nm)!=type(""):return ""
  22. nm = rag_tokenizer.tradi2simp(rag_tokenizer.strQ2B(nm)).lower()
  23. nm = re.sub(r"&", "&", nm)
  24. nm = re.sub(r"[\(\)()\+'\"\t \*\\【】-]+", " ", nm)
  25. nm = re.sub(r"([—-]+.*| +co\..*|corp\..*| +inc\..*| +ltd.*)", "", nm, 10000, re.IGNORECASE)
  26. nm = re.sub(r"(计算机|技术|(技术|科技|网络)*有限公司|公司|有限|研发中心|中国|总部)$", "", nm, 10000, re.IGNORECASE)
  27. if not nm or (len(nm)<5 and not regions.isName(nm[0:2])):return nm
  28. tks = rag_tokenizer.tokenize(nm).split(" ")
  29. reg = [t for i,t in enumerate(tks) if regions.isName(t) and (t != "中国" or i > 0)]
  30. nm = ""
  31. for t in tks:
  32. if regions.isName(t) or t in CORP_TKS:continue
  33. if re.match(r"[0-9a-zA-Z\\,.]+", t) and re.match(r".*[0-9a-zA-Z\,.]+$", nm):nm += " "
  34. nm += t
  35. r = re.search(r"^([^a-z0-9 \(\)&]{2,})[a-z ]{4,}$", nm.strip())
  36. if r:nm = r.group(1)
  37. r = re.search(r"^([a-z ]{3,})[^a-z0-9 \(\)&]{2,}$", nm.strip())
  38. if r:nm = r.group(1)
  39. return nm.strip() + (("" if not reg else "(%s)"%reg[0]) if add_region else "")
  40. def rmNoise(n):
  41. n = re.sub(r"[\((][^()()]+[))]", "", n)
  42. n = re.sub(r"[,. &()()]+", "", n)
  43. return n
  44. GOOD_CORP = set([corpNorm(rmNoise(c), False) for c in GOOD_CORP])
  45. for c,v in CORP_TAG.items():
  46. cc = corpNorm(rmNoise(c), False)
  47. if not cc: print (c)
  48. CORP_TAG = {corpNorm(rmNoise(c), False):v for c,v in CORP_TAG.items()}
  49. def is_good(nm):
  50. global GOOD_CORP
  51. if nm.find("外派")>=0:return False
  52. nm = rmNoise(nm)
  53. nm = corpNorm(nm, False)
  54. for n in GOOD_CORP:
  55. if re.match(r"[0-9a-zA-Z]+$", n):
  56. if n == nm: return True
  57. elif nm.find(n)>=0:return True
  58. return False
  59. def corp_tag(nm):
  60. global CORP_TAG
  61. nm = rmNoise(nm)
  62. nm = corpNorm(nm, False)
  63. for n in CORP_TAG.keys():
  64. if re.match(r"[0-9a-zA-Z., ]+$", n):
  65. if n == nm: return CORP_TAG[n]
  66. elif nm.find(n)>=0:
  67. if len(n)<3 and len(nm)/len(n)>=2:continue
  68. return CORP_TAG[n]
  69. return []