2024-06-01 16:24:10 +08:00
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
#
|
|
|
|
|
|
2024-11-14 17:13:48 +08:00
|
|
|
|
import logging
|
2024-11-12 17:35:13 +08:00
|
|
|
|
import re
|
|
|
|
|
import json
|
|
|
|
|
import os
|
2024-02-23 18:28:12 +08:00
|
|
|
|
import pandas as pd
|
2024-04-28 19:13:33 +08:00
|
|
|
|
from rag.nlp import rag_tokenizer
|
2024-02-23 18:28:12 +08:00
|
|
|
|
from . import regions
|
2024-11-12 17:35:13 +08:00
|
|
|
|
|
|
|
|
|
|
2024-02-23 18:28:12 +08:00
|
|
|
|
current_file_path = os.path.dirname(os.path.abspath(__file__))
|
2024-12-08 14:21:12 +08:00
|
|
|
|
GOODS = pd.read_csv(
|
|
|
|
|
os.path.join(current_file_path, "res/corp_baike_len.csv"), sep="\t", header=0
|
|
|
|
|
).fillna(0)
|
2024-02-23 18:28:12 +08:00
|
|
|
|
GOODS["cid"] = GOODS["cid"].astype(str)
|
|
|
|
|
GOODS = GOODS.set_index(["cid"])
|
2024-12-08 14:21:12 +08:00
|
|
|
|
CORP_TKS = json.load(
|
|
|
|
|
open(os.path.join(current_file_path, "res/corp.tks.freq.json"), "r")
|
|
|
|
|
)
|
2024-02-23 18:28:12 +08:00
|
|
|
|
GOOD_CORP = json.load(open(os.path.join(current_file_path, "res/good_corp.json"), "r"))
|
|
|
|
|
CORP_TAG = json.load(open(os.path.join(current_file_path, "res/corp_tag.json"), "r"))
|
|
|
|
|
|
2024-12-08 14:21:12 +08:00
|
|
|
|
|
2024-02-23 18:28:12 +08:00
|
|
|
|
def baike(cid, default_v=0):
|
|
|
|
|
global GOODS
|
|
|
|
|
try:
|
|
|
|
|
return GOODS.loc[str(cid), "len"]
|
2024-11-12 17:35:13 +08:00
|
|
|
|
except Exception:
|
2024-02-23 18:28:12 +08:00
|
|
|
|
pass
|
|
|
|
|
return default_v
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def corpNorm(nm, add_region=True):
|
|
|
|
|
global CORP_TKS
|
2024-12-08 14:21:12 +08:00
|
|
|
|
if not nm or isinstance(nm, str):
|
|
|
|
|
return ""
|
2024-04-28 19:13:33 +08:00
|
|
|
|
nm = rag_tokenizer.tradi2simp(rag_tokenizer.strQ2B(nm)).lower()
|
2024-02-23 18:28:12 +08:00
|
|
|
|
nm = re.sub(r"&", "&", nm)
|
|
|
|
|
nm = re.sub(r"[\(\)()\+'\"\t \*\\【】-]+", " ", nm)
|
2024-12-08 14:21:12 +08:00
|
|
|
|
nm = re.sub(
|
|
|
|
|
r"([—-]+.*| +co\..*|corp\..*| +inc\..*| +ltd.*)", "", nm, 10000, re.IGNORECASE
|
|
|
|
|
)
|
|
|
|
|
nm = re.sub(
|
|
|
|
|
r"(计算机|技术|(技术|科技|网络)*有限公司|公司|有限|研发中心|中国|总部)$",
|
|
|
|
|
"",
|
|
|
|
|
nm,
|
|
|
|
|
10000,
|
|
|
|
|
re.IGNORECASE,
|
|
|
|
|
)
|
|
|
|
|
if not nm or (len(nm) < 5 and not regions.isName(nm[0:2])):
|
|
|
|
|
return nm
|
2024-02-23 18:28:12 +08:00
|
|
|
|
|
2024-11-28 13:00:38 +08:00
|
|
|
|
tks = rag_tokenizer.tokenize(nm).split()
|
2024-12-08 14:21:12 +08:00
|
|
|
|
reg = [t for i, t in enumerate(tks) if regions.isName(t) and (t != "中国" or i > 0)]
|
2024-02-23 18:28:12 +08:00
|
|
|
|
nm = ""
|
|
|
|
|
for t in tks:
|
2024-12-08 14:21:12 +08:00
|
|
|
|
if regions.isName(t) or t in CORP_TKS:
|
|
|
|
|
continue
|
|
|
|
|
if re.match(r"[0-9a-zA-Z\\,.]+", t) and re.match(r".*[0-9a-zA-Z\,.]+$", nm):
|
|
|
|
|
nm += " "
|
2024-02-23 18:28:12 +08:00
|
|
|
|
nm += t
|
|
|
|
|
|
|
|
|
|
r = re.search(r"^([^a-z0-9 \(\)&]{2,})[a-z ]{4,}$", nm.strip())
|
2024-12-08 14:21:12 +08:00
|
|
|
|
if r:
|
|
|
|
|
nm = r.group(1)
|
2024-02-23 18:28:12 +08:00
|
|
|
|
r = re.search(r"^([a-z ]{3,})[^a-z0-9 \(\)&]{2,}$", nm.strip())
|
2024-12-08 14:21:12 +08:00
|
|
|
|
if r:
|
|
|
|
|
nm = r.group(1)
|
|
|
|
|
return nm.strip() + (("" if not reg else "(%s)" % reg[0]) if add_region else "")
|
2024-02-23 18:28:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def rmNoise(n):
|
|
|
|
|
n = re.sub(r"[\((][^()()]+[))]", "", n)
|
|
|
|
|
n = re.sub(r"[,. &()()]+", "", n)
|
|
|
|
|
return n
|
|
|
|
|
|
2024-12-08 14:21:12 +08:00
|
|
|
|
|
2024-02-23 18:28:12 +08:00
|
|
|
|
GOOD_CORP = set([corpNorm(rmNoise(c), False) for c in GOOD_CORP])
|
2024-12-08 14:21:12 +08:00
|
|
|
|
for c, v in CORP_TAG.items():
|
2024-02-23 18:28:12 +08:00
|
|
|
|
cc = corpNorm(rmNoise(c), False)
|
2024-11-12 17:35:13 +08:00
|
|
|
|
if not cc:
|
2024-11-14 17:13:48 +08:00
|
|
|
|
logging.debug(c)
|
2024-12-08 14:21:12 +08:00
|
|
|
|
CORP_TAG = {corpNorm(rmNoise(c), False): v for c, v in CORP_TAG.items()}
|
|
|
|
|
|
2024-02-23 18:28:12 +08:00
|
|
|
|
|
|
|
|
|
def is_good(nm):
|
|
|
|
|
global GOOD_CORP
|
2024-12-08 14:21:12 +08:00
|
|
|
|
if nm.find("外派") >= 0:
|
|
|
|
|
return False
|
2024-02-23 18:28:12 +08:00
|
|
|
|
nm = rmNoise(nm)
|
|
|
|
|
nm = corpNorm(nm, False)
|
|
|
|
|
for n in GOOD_CORP:
|
|
|
|
|
if re.match(r"[0-9a-zA-Z]+$", n):
|
2024-12-08 14:21:12 +08:00
|
|
|
|
if n == nm:
|
|
|
|
|
return True
|
|
|
|
|
elif nm.find(n) >= 0:
|
|
|
|
|
return True
|
2024-02-23 18:28:12 +08:00
|
|
|
|
return False
|
|
|
|
|
|
2024-12-08 14:21:12 +08:00
|
|
|
|
|
2024-02-23 18:28:12 +08:00
|
|
|
|
def corp_tag(nm):
|
|
|
|
|
global CORP_TAG
|
|
|
|
|
nm = rmNoise(nm)
|
|
|
|
|
nm = corpNorm(nm, False)
|
|
|
|
|
for n in CORP_TAG.keys():
|
|
|
|
|
if re.match(r"[0-9a-zA-Z., ]+$", n):
|
2024-12-08 14:21:12 +08:00
|
|
|
|
if n == nm:
|
|
|
|
|
return CORP_TAG[n]
|
|
|
|
|
elif nm.find(n) >= 0:
|
|
|
|
|
if len(n) < 3 and len(nm) / len(n) >= 2:
|
|
|
|
|
continue
|
2024-02-23 18:28:12 +08:00
|
|
|
|
return CORP_TAG[n]
|
|
|
|
|
return []
|