projectname.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """
  2. 项目名称字段检查
  3. """
  4. import re
  5. from docs.config import general_config
  6. from util.sensitive_word import AcAutomation
  7. import csv
  8. class ProjectnameChecker(object):
  9. """
  10. 项目名称字段检查
  11. """
  12. def __init__(self):
  13. self.errors_tables = {
  14. "0101": {
  15. "name": "项目名称长度小于等于5",
  16. "parent_name": "长度类型",
  17. "parent_code": "01",
  18. "checkFn": self.lt5
  19. },
  20. "0102": {
  21. "name": "长度大于等于100",
  22. "parent_name": "长度类型",
  23. "parent_code": "01",
  24. "checkFn": self.gt100
  25. },
  26. "0201":{
  27. "name": "非汉字占比>55%",
  28. "parent_name": "汉字占比",
  29. "parent_code": "02",
  30. "checkFn": self.check0201
  31. },
  32. "0302": {
  33. "name": "不包含通用词汇(中标公告)",
  34. "parent_name": "语义表述不完整",
  35. "parent_code": "03",
  36. "checkFn": self.check0302
  37. }
  38. }
  39. @staticmethod
  40. def gt100(projectname: str) -> bool:
  41. """
  42. 标题长度大于80
  43. :param title:
  44. :return:返回true 代表异常
  45. """
  46. return len(projectname) >= 100
  47. @staticmethod
  48. def lt5(projectname: str) -> bool:
  49. """
  50. 标题长度小于5
  51. :param title:
  52. :return:返回true 代表异常
  53. """
  54. return len(projectname) <= 5
  55. def check0201(self,projectname: str) -> bool:
  56. """
  57. 标题非汉字占比 >55%
  58. :param title:
  59. :return:返回true 代表异常
  60. """
  61. # chinese_chars = [char for char in title if '\u4e00' <= char <= '\u9fff'] # 匹配汉字
  62. non_chinese_chars = [char for char in projectname if not ('\u4e00' <= char <= '\u9fff')] # 匹配非汉字和非字母数字字符
  63. non_chinese_chars_radio = len(non_chinese_chars) / len(projectname)
  64. if non_chinese_chars_radio > 0.5:
  65. return True
  66. return False
  67. def check0302(self,projectname: str) -> bool:
  68. """
  69. 没有通用后缀
  70. :param title:
  71. :return:返回true 代表异常
  72. """
  73. self.check_general_ac = AcAutomation()
  74. with open(general_config["table_field_config"]["path"], "r") as f:
  75. reads = csv.reader(f)
  76. [self.check_general_ac.add_word(w[0]) for w in reads]
  77. p1 = re.compile(r"^[3|6|7|8|0|\.]")
  78. p2 = re.compile(".*--")
  79. if p1.match(projectname):
  80. # print(11111)
  81. return True
  82. if p2.match(projectname):
  83. # print(2222)
  84. return True
  85. if self.check_general_ac.search(projectname):
  86. return False
  87. return True