content.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """
  2. 正文字段检查
  3. """
  4. class ContentChecker(object):
  5. """
  6. 正文检查
  7. """
  8. errors_tables = {
  9. "01": {"name": "正文长度异常",
  10. "code": "01",
  11. "rules": {
  12. "01": {
  13. "name": "低于200字",
  14. "code": "01",
  15. "checkFn": "lt200",
  16. },
  17. },
  18. },
  19. "02": {"name": "中文占比异常",
  20. "code": "02",
  21. "rules": {
  22. "01": {
  23. "name": "中文字符占比低于30%",
  24. "code": "01",
  25. "checkFn": "chineseCharLt30",
  26. },
  27. },
  28. },
  29. }
  30. def __init__(self):
  31. pass
  32. @staticmethod
  33. def lt200(content: str) -> bool:
  34. return len(content) < 200
  35. @staticmethod
  36. def chineseCharLt30(content: str) -> bool:
  37. chinese_char_count = sum(list(map(lambda x: 1 if '\u4e00' <= x <= '\u9fef' else 0, content)))
  38. return float(chinese_char_count) / float(len(content)) < 0.3