You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
report_app/app/tools/count_data.py

123 lines
4.2 KiB
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import re
# text = "6月15日17时至6月16日17时期间全网累计停电132.59万户次5分钟以内短时停电用户23.48万户次环比减少68.28万户次其中重要用户停电0户次用户停电情况总体平稳。"
def count_change_outage(text):
# 匹配数字和“万”单位
pattern = r"(\d+\.\d+)万"
matches = re.findall(pattern, text)
# 提取累计停电、短时停电和环比变化用户数
total_outage = float(matches[0]) # 累计停电用户数
short_term_outage = float(matches[1]) # 短时停电用户数
change_outage = float(matches[2]) # 环比变化用户数
# 判断是增加还是减少
if "减少" in text:
result = change_outage / (total_outage + change_outage)
type = "减少"
elif "增加" in text:
result = change_outage / (total_outage - change_outage)
type = "增加"
else:
result = None # 或者其他默认值
if result is not None:
percentage = f"{result * 100:.2f}%"
print(f"计算结果:{percentage}")
else:
print("未找到增加或减少的关键字")
short_percentage = f"{short_term_outage / total_outage * 100:.2f}%"
# 匹配“重要用户停电”后面的数字
pattern = r"重要用户停电(\d+)户"
match = re.search(pattern, text)
if match:
important_stop_outage = match.group(1)
print(f"重要用户停电户次:{result}")
else:
important_stop_outage = "0"
print("未找到重要用户停电户次")
return (
total_outage,
short_term_outage,
change_outage,
percentage,
short_percentage,
important_stop_outage,
type,
)
# count_change_outage(text)
def count_outage_sentiment(text):
print("开始分析舆情数据:")
print(text)
# text = "全网监测到涉电力供应类舆情风险信息11条环比减少2条"
# text = "涉电力供应类舆情风险信息22条环比持平。其中1条为官方媒体发布其余21条均为个人账号发布。"
# 使用正则表达式匹配数字和关键词
pattern = r"信息(\d+)条,环比(增加|减少)(\d)条"
pattern_equal = r"信息(\d+)条,环比持平"
match = re.search(pattern, text)
match_equal = re.search(pattern_equal, text)
num1 = ""
change = ""
num2 = ""
result = ""
if match:
num1 = int(match.group(1)) # 第一个数字,如 11
change = match.group(2) # 变化类型,如 “减少” 或 “增加”
num2 = int(match.group(3)) # 第二个数字,如 2
if change == "减少":
result = f"{num2 / (num1 + num2) * 100:.2f}%"
num2 = num1 + num2
elif change == "增加":
result = f"{num2 / (num1 - num2) * 100:.2f}%"
num2 = num1 - num2
else:
result = None # 如果不是增加或减少,可以处理成其他情况
print(f"第一个数字:{num1}")
print(f"变化类型:{change}")
print(f"第二个数字:{num2}")
if result is not None:
print(f"计算结果:{result}")
else:
print("变化类型未知,无法计算")
# update:2025-07-08 增加持平
elif match_equal:
num1 = int(match_equal.group(1))
change = "持平"
num2 = int(match_equal.group(1))
result = ""
# change = match_equal.group(2)
else:
pattern = r"信息(\d+)条,同比(增加|减少)(\d+)条"
match = re.search(pattern, text)
if match:
num1 = int(match.group(1)) # 第一个数字,如 11
change = match.group(2) # 变化类型,如 “减少” 或 “增加”
num2 = int(match.group(3)) # 第二个数字,如 2
if change == "减少":
result = f"{num2 / (num1 + num2) * 100:.2f}%"
num2 = num1 + num2
elif change == "增加":
result = f"{num2 / (num1 - num2) * 100:.2f}%"
num2 = num1 - num2
else:
result = None # 如果不是增加或减少,可以处理成其他情况
print("未匹配到符合条件的内容")
return num1, change, num2, result