正则的应用提取

import re

def ner_car_directions(text):
“”“提取方位实体”“”
all_direc_list = [“所有”, “整车”, “全部”]
direc1_list = [“前”, “后”, “左”, “右”]
zhu_flag_list = [“主驾”, “主座”, “主座”]
fu_flag_list =[“副驾”, “副坐”, “副座”]
direc2_list = zhu_flag_list + fu_flag_list

try:
    for all_direc in all_direc_list:
        if all_direc in text:
            return ["所有"]

    out_ner_list = []

    if "主副" in text:
        out_ner_list.append("主副驾")
        text = text.replace("主副", "##")

    for direc2 in direc2_list:
        if direc2 in text:
            if direc2 in zhu_flag_list:
                out_ner_list.append("主驾")
            elif direc2 in fu_flag_list:
                out_ner_list.append("副驾")
            else:
                out_ner_list.append(direc2)
            text = text.replace(direc2, "#"*len(direc2))

    direc1_str = "".join(direc1_list)
    direc1_flags = u"[%s]{1,}" % direc1_str
    dire_re = re.compile(direc1_flags)
    regex_results = dire_re.findall(text)
    out_ner_list.extend(regex_results)
    if not len(out_ner_list):
        return []
    regex_results_str = "".join(out_ner_list)

    out_ner_str = ""
    for i in regex_results_str:
        if i not in out_ner_str:
            out_ner_str += i
    print("44", out_ner_str)
    c = 0
    for i in out_ner_str:
        if i in text:
            c += 1
    if c == 4:
        out_ner_str = "所有"

    out_str = out_ner_str
    if not out_str:
        return []
    return [out_str]
except Exception as e:
    print("e: ", e)
    return []

def ner_car_colors(text):
random_colors = [“随机”, “自动”, “自由”]
other_colors = [“五颜六色”, “多彩斑斓”, “彩色”]
colors = [“红”,“深蓝”, “绿”, “白”, “黄”, “黑”, “橘”,“篮”, “色”]

try:
    for multi_color in other_colors:
        if multi_color in text:
            return ["多彩"]

    ner_out_list = []
    color_compile = re.compile(u"[%s]{1,}" % "".join(colors))
    color_ner_results = color_compile.findall(text)
    ner_out_list.extend(color_ner_results)

    # 处理随机情况
    for random_color in random_colors:
        if random_color in text and random_color not in ner_out_list:
            ner_out_list.append(random_color)
            return ner_out_list

    out_list = []
    for out_color in ner_out_list:
        if len(out_color) > 0:
            if str(out_color).startswith("色") or str(out_color).startswith("深"):
                continue
            out_list.append(out_color)
    out_str = "".join(out_list)
    if not out_str:
        return []
    return [out_str]
except Exception as e:
    print("e: ", e)
    return []

if name == ‘main’:
#text = “打开左左排右右边右前前后后车窗”
# text = “打开后排右主驾”
# a = ner_car_direction(text)
# print(a)

text = "氛围灯颜色为深"
b = ner_car_colors(text)
print(b)
print(len(b))

import re

from common_tools import ner_car_directions, ner_car_colors

def regex_ner_direction(text, intent, slots, service=“carControl”):
direc_flag = “”
if direc_flag not in str(slots):
return intent, slots
# 提方位
try:
ner_results_ret = ner_car_directions(text)
if len(ner_results_ret):
ner_results = ner_results_ret[0]
print(“—”, ner_results)
intent = str(intent).replace(direc_flag, ner_results)
slots = str(slots).replace(direc_flag, ner_results)
except Exception as e:
print(“e:”, e)
return intent, slots

def regex_ner_color(text, intent, slots, service=“carControl”):
direc_flag = “”
if direc_flag not in str(slots):
return intent, slots
# 提方位
try:
ner_results_ret = ner_car_colors(text)
if len(ner_results_ret):
ner_results = ner_results_ret[0]
print(“—”, ner_results)
intent = str(intent).replace(direc_flag, ner_results)
slots = str(slots).replace(direc_flag, ner_results)
print(slots)
except Exception as e:
print(“e:”, e)
return intent, slots

def move_service_to_other(text, slots, intent):
if “” in str(slots):
return “other”

def max_confirm(text, slots):
if “MAX” not in str(slots):
return False
flag1 = [“最”, “极”, “顶”]
c = 0
for i in flag1:
if i in text:
c += 1
if c == 0:
return False

flag2 = ["高", "大", "多", "上", "猛", "强", "顶"]
regex = re.compile(u"[%s]{1,}" % "".join(flag2))
regex_results = regex.findall(text)
if not len(regex_results):
    return False
return True

def min_confirm(text, slots):
if “MIN” not in str(slots):
return False
flag1 = [“最”, “极”]
flag2 = [“小”, “低”, “少”, “弱”, “瘦”, “下”, “底”]

c = 0
for i in flag1:
    if i in text:
        c += 1
if c == 0:
    return False

regex = re.compile(u"[%s]{1,}" % "".join(flag2))
regex_results = regex.findall(text)
if not len(regex_results):
    return False
return True

def plus_little_confirm(text, slots):
# 需要判断offset不在其中
if “PLUS” not in str(slots):
return False
flag1 = [“高”, “大”, “多”, “强”, “增”, “加”, “升”, “上”, “猛”]
c = 0
for i in flag1:
if i in text:
c += 1
if c == 0:
return False

flag2 = ["一点", "缝隙", "一丢", "一丝", "一些"]
regex = re.compile(u"[%s]{1,}" % "".join(flag2))
regex_results = regex.findall(text)
if not len(regex_results):
    return False
return True

def minus_little_confirm(text, slots):
# 需要判断offset不在其中
if “MINUS” not in str(slots):
return False
flag1 = [“低”, “小”, “少”, “降”, “下”, “缩”, “弱”]
c = 0
for i in flag1:
if i in text:
c += 1
if c == 0:
return False

flag2 = ["一点", "缝隙", "一丢", "一丝", "一些"]
regex = re.compile(u"[%s]{1,}" % "".join(flag2))
regex_results = regex.findall(text)
if not len(regex_results):
    return False
return True

def plus_little_confirm(text, slots):
# 需要判断offset不在其中
if “PLUS” not in str(slots):
return False
flag1 = [“高”, “大”, “多”, “强”, “增”, “加”, “升”, “上”, “猛”]
c = 0
for i in flag1:
if i in text:
c += 1
if c == 0:
return False

flag2 = ["一点", "缝隙", "一丢", "一丝", "一些"]
regex = re.compile(u"[%s]{1,}" % "".join(flag2))
regex_results = regex.findall(text)
if not len(regex_results):
    return False
return True

def aircontrol_minus_little_confirm(text, slots):
# 需要判断offset不在其中
if “MINUS” not in str(slots):
return False
flag1 = [“低”, “小”, “少”, “降”, “下”, “缩”, “弱”]
c = 0
for i in flag1:
if i in text:
c += 1
if c == 0:
return False

flag2 = ["一点", "缝隙", "一丢", "一丝", "一些"]
regex = re.compile(u"[%s]{1,}" % "".join(flag2))
regex_results = regex.findall(text)
if not len(regex_results):
    return False
slots = str(slots).replace("PLUS", "PLUS_LITTLE")
return True

def aircontrol_plus_little_confirm(text, slots):
# 需要判断offset不在其中
if “PLUS” not in str(slots):
return False

flag1 = ["高", "大", "多", "强", "增", "加", "升", "上", "猛"]
c = 0
for i in flag1:
    if i in text:
        c += 1
if c == 0:
    return False

flag2 = ["一点", "缝隙", "一丢", "一丝", "一些"]
regex = re.compile(u"[%s]{1,}" % "".join(flag2))
regex_results = regex.findall(text)
if not len(regex_results):
    return False
slots = str(slots).replace("PLUS", "PLUS_LITTLE")
return slots

if name == ‘main’:
# text = “打开全部车窗”
# intent = “打开[]车窗”
# slots = {“name”: “窗户”}
# intent, slots = regex_ner_direction(text, intent, slots)
# print(intent, “–>”, slots)

text = "氛围灯调为红色"
intent = "打开[<direction>]车窗氛围灯调为<color>"
slots = {"name": "氛围灯", "color": "<color>"}
intent, slots = regex_ner_color(text, intent, slots)
print(intent, "-->", slots)

# 颜色如果正则和模型都没提取到,转为随机
text = "温度调高一些"
slots = {"name":"空调", "tmp":"PLUS"}
i = aircontrol_plus_little_confirm(text, slots)
print(i)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值