inputType.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # inputType.py
  2. # from services.templateData import templateData
  3. # 引入1-4套皮肤数据
  4. from services.style.style1to4data import sectorStyle1to4Data
  5. # 引入5套皮肤数据
  6. from services.style.style5data import sectorStyle5Data
  7. class inputType:
  8. def __init__(self):
  9. self.del_map = [
  10. "删除", "去掉", "移除", "去除", "不要", "删掉", "不需要", "没有"
  11. ]
  12. self.del_group_map = [
  13. "不要显示广告", "去掉所有广告", "纯净版" , "只要新闻"
  14. ]
  15. self.sectors_name = {
  16. # 公用通栏
  17. "adSector": "通栏广告",
  18. # 1-4套皮肤
  19. "headLineSector": "网站头条",
  20. "bannerSector": "焦点图",
  21. "linkSector": "外链面板",
  22. "manyPictureSector": "新闻图文组合1",
  23. "commentSector": "新闻图文组合2",
  24. "listSector": "新闻图文组合3",
  25. "onlyImgSector": "带广告新闻组合",
  26. # 第5套皮肤
  27. "headLineSector": "网站头条",
  28. "bannerSectorMerge": "焦点图",
  29. "linkCxfwSector": "外链面板-带查询服务",
  30. "NewsHyjjSector": "行业聚焦新闻通栏",
  31. "newsTabsSector": "热点新闻选项卡",
  32. "newsTextSector": "文字新闻通栏1",
  33. "newsTabsTextSector": "选项卡文字新闻通栏",
  34. "newsAllPictureSector": "图片新闻通栏1",
  35. "newsTabsAdSector": "选项卡资讯带两个广告",
  36. }
  37. self.pos_first = ["第一个", "第一", "首位", "最前面", "开头", "第一位", "顶部"]
  38. self.pos_second = ["第二个", "第二", "第二位"]
  39. self.pos_third = ["第三个", "第三", "第三位"]
  40. self.pos_last = ["最后一个", "最后", "末尾", "垫底", "最后一位", "底部"]
  41. self.pos_last_second = ["倒数第二个", "倒数第二", "倒数第二位"]
  42. self.pos_last_third = ["倒数第三个", "倒数第三", "倒数第三位"]
  43. def generate_inputType(self, user_text,matched_template_id):
  44. # 导入推理方法
  45. from services.ai_service import ai_service
  46. import re
  47. # 按标点符号分句处理,支持多指令
  48. segments = re.split(r'[,,。;;|]+', user_text)
  49. segments = [s.strip() for s in segments if s.strip()]
  50. templateData = None
  51. # 根据matched_template_id选择样式数据
  52. if int(matched_template_id) == 5:
  53. print("正在使用第5套皮肤的数据!")
  54. templateData = sectorStyle5Data
  55. else:
  56. print("正在使用前4套皮肤的数据!")
  57. templateData = sectorStyle1to4Data
  58. if not segments:
  59. segments = [user_text]
  60. for segment in segments:
  61. # 1. 检查用户输入中是否包含组删除词
  62. if any(word in segment for word in self.del_group_map):
  63. # 删除所有广告相关通栏
  64. # ad_sectors = ["通栏广告", "带广告新闻组合"]
  65. # 找到所有名字中含有广告的通栏
  66. ad_sectors = [sector for sector in templateData.sectors_config if "广告" in sector['CNname']]
  67. templateData.sectors_config = [
  68. sector for sector in templateData.sectors_config
  69. if sector not in ad_sectors
  70. ]
  71. # 添加推理过程
  72. ai_service.add_reasoning(f"另外,用户不想看到广告,因此我修改了通栏列表,只保留了新闻相关通栏")
  73. print(f"已删除所有广告通栏: {ad_sectors}")
  74. # 2. 检查用户输入中是否包含普通删除词
  75. elif any(word in segment for word in self.del_map):
  76. # 查找用户输入中提到的通栏名称
  77. sectors_to_delete = []
  78. for cnname in self.sectors_name.values():
  79. if cnname in segment:
  80. sectors_to_delete.append(cnname)
  81. # 删除匹配的通栏配置
  82. if sectors_to_delete:
  83. templateData.sectors_config = [
  84. sector for sector in templateData.sectors_config
  85. if sector['CNname'] not in sectors_to_delete
  86. ]
  87. # 添加推理过程
  88. ai_service.add_reasoning(f"另外,用户想要删除{sectors_to_delete}通栏,我来把它隐藏掉")
  89. print(f"已删除通栏: {sectors_to_delete}")
  90. # 3. 检查用户输入中是否包含位置调整
  91. # 遍历所有通栏名称
  92. for name, cnname in self.sectors_name.items():
  93. if cnname in segment:
  94. new_weight = None
  95. reasoning_msg = ""
  96. # 获取当前最大和最小权重
  97. current_weights = [s['weight'] for s in templateData.sectors_config]
  98. if not current_weights:
  99. break
  100. max_weight = max(current_weights)
  101. min_weight = min(current_weights)
  102. # 检查位置关键词
  103. force_pos_val = None
  104. if any(word in segment for word in self.pos_first):
  105. new_weight = max_weight + 2
  106. reasoning_msg = f"用户想要把{cnname}放在第一位,我提升了它的权重"
  107. elif any(word in segment for word in self.pos_second):
  108. new_weight = max_weight - 1
  109. reasoning_msg = f"用户想要把{cnname}放在第二位,我调整了它的权重"
  110. elif any(word in segment for word in self.pos_third):
  111. new_weight = max_weight - 2
  112. reasoning_msg = f"用户想要把{cnname}放在第三位,我调整了它的权重"
  113. elif any(word in segment for word in self.pos_last):
  114. new_weight = min_weight - 3
  115. force_pos_val = 1
  116. reasoning_msg = f"用户想要把{cnname}放在最后一位,我降低了它的权重"
  117. elif any(word in segment for word in self.pos_last_second):
  118. if min_weight < 6:
  119. new_weight = min_weight + 1
  120. else:
  121. new_weight = min_weight - 2
  122. force_pos_val = 2
  123. reasoning_msg = f"用户想要把{cnname}放在倒数第二位,我调整了它的权重"
  124. elif any(word in segment for word in self.pos_last_third):
  125. if min_weight < 6:
  126. new_weight = min_weight + 2
  127. else:
  128. new_weight = min_weight - 1
  129. force_pos_val = 3
  130. reasoning_msg = f"用户想要把{cnname}放在倒数第三位,我调整了它的权重"
  131. # 如果确定了新权重,进行修改
  132. if new_weight is not None:
  133. for sector in templateData.sectors_config:
  134. if sector['name'] == name:
  135. sector['weight'] = new_weight
  136. if force_pos_val is not None:
  137. sector['force_pos'] = force_pos_val
  138. # 添加推理过程
  139. ai_service.add_reasoning(reasoning_msg)
  140. print(f"已调整通栏 {cnname} 权重为: {new_weight}")
  141. break
  142. # 全局匹配器实例
  143. inputType_matcher = inputType()