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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
def process_api(input_text):
|
| 12 |
# 这里编写实际的后端处理逻辑
|
| 13 |
|
| 14 |
return {
|
| 15 |
"status": "success",
|
| 16 |
# "result": f"Processed: {input_text.upper()}",
|
| 17 |
-
"result": f"Processed: {nlp(input_text).to_json()}",
|
|
|
|
| 18 |
"timestamp": time.time()
|
| 19 |
}
|
| 20 |
|
|
@@ -25,36 +249,4 @@ gr.Interface(
|
|
| 25 |
outputs="json",
|
| 26 |
title="Backend API",
|
| 27 |
allow_flagging="never"
|
| 28 |
-
).launch()
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# nlp = spacy.load("en_core_web_md")
|
| 34 |
-
# HTML_WRAPPER = "<div style='padding: 10px;'>{}</div>"
|
| 35 |
-
|
| 36 |
-
# def show_spatial_ent_table(doc):
|
| 37 |
-
# rows = []
|
| 38 |
-
# for i, ent in enumerate(doc.ents):
|
| 39 |
-
# rows.append(f"<tr><td>{i+1}</td><td>{ent.text}</td><td>{ent.label_}</td></tr>")
|
| 40 |
-
# table_html = "<table border='1'><tr><th>Index</th><th>Entity</th><th>Label</th></tr>" + "".join(rows) + "</table>"
|
| 41 |
-
# return table_html
|
| 42 |
-
|
| 43 |
-
# def process_api(input_text):
|
| 44 |
-
# doc = nlp(input_text)
|
| 45 |
-
# html_ent = displacy.render(doc, style="ent")
|
| 46 |
-
# html_ent = HTML_WRAPPER.format(html_ent.replace("\n", ""))
|
| 47 |
-
# html_table = show_spatial_ent_table(doc)
|
| 48 |
-
# final_html = html_ent + "<br>" + html_table
|
| 49 |
-
# return {
|
| 50 |
-
# "data": [{"html": final_html}],
|
| 51 |
-
# "timestamp": time.time()
|
| 52 |
-
# }
|
| 53 |
-
|
| 54 |
-
# gr.Interface(
|
| 55 |
-
# fn=process_api,
|
| 56 |
-
# inputs="text",
|
| 57 |
-
# outputs="json",
|
| 58 |
-
# allow_flagging="never",
|
| 59 |
-
# title="Backend API"
|
| 60 |
-
# ).launch()
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
+
# nlp = spacy.load("en_core_web_md")
|
| 11 |
+
# def process_api(input_text):
|
| 12 |
+
# # 这里编写实际的后端处理逻辑
|
| 13 |
+
|
| 14 |
+
# return {
|
| 15 |
+
# "status": "success",
|
| 16 |
+
# # "result": f"Processed: {input_text.upper()}",
|
| 17 |
+
# "result": f"Processed: {nlp(input_text).to_json()}",
|
| 18 |
+
# "timestamp": time.time()
|
| 19 |
+
# }
|
| 20 |
+
|
| 21 |
+
# # 设置API格式为JSON
|
| 22 |
+
# gr.Interface(
|
| 23 |
+
# fn=process_api,
|
| 24 |
+
# inputs="text",
|
| 25 |
+
# outputs="json",
|
| 26 |
+
# title="Backend API",
|
| 27 |
+
# allow_flagging="never"
|
| 28 |
+
# ).launch()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# nlp = spacy.import gradio as gr
|
| 34 |
+
import time
|
| 35 |
+
import spacy
|
| 36 |
+
from spacy.tokens import Span, Doc, Token
|
| 37 |
+
from spacy import displacy
|
| 38 |
+
import streamlit as st
|
| 39 |
+
import pandas as pd
|
| 40 |
+
from spacy.language import Language
|
| 41 |
+
import llm_ent_extract
|
| 42 |
+
import regex_spatial
|
| 43 |
+
import re
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
colors = {'GPE': "#43c6fc", "LOC": "#fd9720", "RSE":"#a6e22d"}
|
| 47 |
+
options = {"ents": ['GPE', 'LOC', "RSE"], "colors": colors}
|
| 48 |
+
HTML_WRAPPER = """<div style="overflow-x: auto; border: none solid #a6e22d; border-radius: 0.25rem; padding: 1rem">{}</div>"""
|
| 49 |
+
BASE_URL = ""
|
| 50 |
+
model = ""
|
| 51 |
+
types = ""
|
| 52 |
nlp = spacy.load("en_core_web_md")
|
| 53 |
+
gpe_selected = 'GPE'
|
| 54 |
+
loc_selected = 'loc'
|
| 55 |
+
rse_selected = 'rse'
|
| 56 |
+
rse_id = "rse_id"
|
| 57 |
+
def set_selected_entities(doc):
|
| 58 |
+
global gpe_selected, loc_selected, rse_selected, model
|
| 59 |
+
ents = [ent for ent in doc.ents if ent.label_ == gpe_selected or ent.label_ == loc_selected or ent.label_ == rse_selected]
|
| 60 |
+
|
| 61 |
+
doc.ents = ents
|
| 62 |
+
return doc
|
| 63 |
+
|
| 64 |
+
def update_entities(doc, entity_texts, replace=True):
|
| 65 |
+
"""
|
| 66 |
+
根据给定的文本内容标注实体,并直接修改 doc.ents。
|
| 67 |
+
|
| 68 |
+
:param doc: spaCy 解析后的 Doc 对象
|
| 69 |
+
:param entity_texts: 字典,键是要标注的实体文本,值是对应的实体类别
|
| 70 |
+
:param replace: 布尔值,True 则替换现有实体,False 则保留现有实体并添加新的
|
| 71 |
+
"""
|
| 72 |
+
new_ents = list(doc.ents) if not replace else [] # 如果 replace=False,保留已有实体
|
| 73 |
+
|
| 74 |
+
for ent_text, ent_label in entity_texts.items():
|
| 75 |
+
start = doc.text.find(ent_text) # 在全文中查找文本位置
|
| 76 |
+
if start != -1:
|
| 77 |
+
start_token = len(doc.text[:start].split()) # 计算起始 token 索引
|
| 78 |
+
end_token = start_token + len(ent_text.split()) # 计算结束 token 索引
|
| 79 |
+
|
| 80 |
+
if start_token < len(doc) and end_token <= len(doc): # 确保索引不越界
|
| 81 |
+
new_ent = Span(doc, start_token, end_token, label=ent_label)
|
| 82 |
+
new_ents.append(new_ent)
|
| 83 |
+
|
| 84 |
+
doc.set_ents(new_ents) # 更新 doc.ents
|
| 85 |
+
def find_ent_by_regex(doc, sentence, ent, regex):
|
| 86 |
+
global id
|
| 87 |
+
|
| 88 |
+
if id == "":
|
| 89 |
+
id = ent.text
|
| 90 |
+
for match in re.finditer(regex, doc.text):
|
| 91 |
+
start, end = match.span()
|
| 92 |
+
if(start>= sentence.start_char and start<= sentence.end_char):
|
| 93 |
+
span = doc.char_span(start, end)
|
| 94 |
+
if span is not None:
|
| 95 |
+
id = span.text +"_"+ id
|
| 96 |
+
if(start > ent.end_char):
|
| 97 |
+
ent.end_char = end
|
| 98 |
+
else:
|
| 99 |
+
ent.start_char = start
|
| 100 |
+
|
| 101 |
+
return ent
|
| 102 |
+
|
| 103 |
+
return ent
|
| 104 |
+
def set_extension():
|
| 105 |
+
Span.set_extension(rse_id, default="", force=True)
|
| 106 |
+
Doc.set_extension(rse_id, default="", force=True)
|
| 107 |
+
Token.set_extension(rse_id, default="", force=True)
|
| 108 |
+
def get_level1(doc, sentence, ent):
|
| 109 |
+
return find_ent_by_regex(doc, sentence, ent, regex_spatial.get_level1_regex())
|
| 110 |
+
|
| 111 |
+
def get_level2(doc, sentence, ent):
|
| 112 |
+
return find_ent_by_regex(doc, sentence, ent, regex_spatial.get_level2_regex())
|
| 113 |
+
|
| 114 |
+
def get_level3(doc, sentence, ent):
|
| 115 |
+
return find_ent_by_regex(doc, sentence, ent, regex_spatial.get_level3_regex())
|
| 116 |
+
def get_relative_entity(doc, sentence, ent):
|
| 117 |
+
global id
|
| 118 |
+
|
| 119 |
+
id = ""
|
| 120 |
+
rel_entity = get_level1(doc, sentence, ent)
|
| 121 |
+
# print(1111 ,rel_entity)
|
| 122 |
+
rel_entity = get_level2(doc, sentence, rel_entity)
|
| 123 |
+
# print(2222 ,rel_entity)
|
| 124 |
+
rel_entity = get_level3(doc, sentence, rel_entity)
|
| 125 |
+
# print(3333 ,rel_entity)
|
| 126 |
+
|
| 127 |
+
if("_" in id):
|
| 128 |
+
rel_entity = doc.char_span(rel_entity.start_char, rel_entity.end_char, "RSE")
|
| 129 |
+
rel_entity._.rse_id = id
|
| 130 |
+
|
| 131 |
+
# print(id, 'idid')
|
| 132 |
+
# print(rel_entity._.rse_id, '._._')
|
| 133 |
+
|
| 134 |
+
return rel_entity
|
| 135 |
+
rel_entity = doc.char_span(ent.start_char, ent.end_char, ent.label_)
|
| 136 |
+
rel_entity._.rse_id = id
|
| 137 |
+
# print(4444 ,rel_entity)
|
| 138 |
+
return rel_entity
|
| 139 |
+
|
| 140 |
+
@Language.component("spatial_pipeline")
|
| 141 |
+
def get_spatial_ent(doc):
|
| 142 |
+
set_extension()
|
| 143 |
+
new_ents = []
|
| 144 |
+
# ents = [ent for ent in doc.ents if ent.label_ == "GPE" or ent.label_ == "LOC"] # 筛选出ase
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
# LLM 输出
|
| 148 |
+
# GPE = '[###Pyrmont###, ###Glebe###]' # LLM 输出的实体
|
| 149 |
+
GPE = llm_ent_extract.extract_GPE(doc.text) # LLM 输出的实体
|
| 150 |
+
print(doc.text, 'llmin')
|
| 151 |
+
print(GPE, 'llout')
|
| 152 |
+
|
| 153 |
+
GPE = llm_ent_extract.extract(GPE, 'GPE')
|
| 154 |
+
print(GPE, 'llmout2')
|
| 155 |
+
update_entities(doc, GPE, True)
|
| 156 |
+
ents = doc.ents
|
| 157 |
+
print(ents, 'eee')
|
| 158 |
+
end = None
|
| 159 |
+
for ent in ents:
|
| 160 |
+
|
| 161 |
+
if ent.end != len(doc):
|
| 162 |
+
next_token = doc[ent.end]
|
| 163 |
+
if end is not None:
|
| 164 |
+
start = end
|
| 165 |
+
else:
|
| 166 |
+
start = ent.sent.start
|
| 167 |
+
if next_token.text.lower() in regex_spatial.get_keywords():
|
| 168 |
+
end = next_token.i
|
| 169 |
+
else:
|
| 170 |
+
end = ent.end
|
| 171 |
+
|
| 172 |
+
else:
|
| 173 |
+
start = ent.sent.start
|
| 174 |
+
end = ent.end
|
| 175 |
+
|
| 176 |
+
# print(doc, '//',start, '//', end, 999888)
|
| 177 |
+
# print(doc[start],'//', doc[end])
|
| 178 |
+
# print(ents, 999)
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
rsi_ent = get_relative_entity(doc,Span(doc, start, end), ent)
|
| 182 |
+
# print(doc.ents[0]._.rse_id, '._._2')
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
# print(rsi_ent.text, rsi_ent.label_, rsi_ent._.rse_id)
|
| 186 |
+
new_ents.append(rsi_ent)
|
| 187 |
+
|
| 188 |
+
doc.ents = new_ents
|
| 189 |
+
return doc
|
| 190 |
+
def extract_spatial_entities(text):
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
nlp.add_pipe("spatial_pipeline", after="ner")
|
| 194 |
+
doc = nlp(text)
|
| 195 |
+
|
| 196 |
+
# 分句处理
|
| 197 |
+
sent_ents = []
|
| 198 |
+
sent_texts = []
|
| 199 |
+
sent_rse_id = []
|
| 200 |
+
offset = 0 # 记录当前 token 偏移量
|
| 201 |
+
sent_start_positions = [0] # 记录句子信息
|
| 202 |
+
doc_copy = doc.copy() # 用于展示方程组合
|
| 203 |
+
for sent in doc.sents:
|
| 204 |
+
|
| 205 |
+
sent_doc = nlp(sent.text) # 逐句处理
|
| 206 |
+
sent_doc = set_selected_entities(sent_doc) # 这里处理实体
|
| 207 |
+
sent_texts.append(sent_doc.text)
|
| 208 |
+
|
| 209 |
+
for ent in sent_doc.ents:
|
| 210 |
+
sent_rse_id.append(ent._.rse_id)
|
| 211 |
+
# **调整每个实体的索引,使其匹配完整文本**
|
| 212 |
+
for ent in sent_doc.ents:
|
| 213 |
+
new_ent = Span(doc, ent.start + offset, ent.end + offset, label=ent.label_)
|
| 214 |
+
sent_ents.append(new_ent)
|
| 215 |
+
|
| 216 |
+
offset += len(sent) # 更新偏移量
|
| 217 |
+
sent_start_positions.append(sent_start_positions[-1] + len(sent)) # 记录句子起点
|
| 218 |
+
# **创建新 Doc**
|
| 219 |
+
final_doc = Doc(nlp.vocab, words=[token.text for token in doc], spaces=[token.whitespace_ for token in doc])
|
| 220 |
+
for i in sent_start_positions: # 手动标记句子起始点
|
| 221 |
+
if i < len(final_doc):
|
| 222 |
+
final_doc[i].is_sent_start = True
|
| 223 |
+
# **设置实体**
|
| 224 |
+
final_doc.set_ents(sent_ents)
|
| 225 |
+
|
| 226 |
+
for i in range(len(sent_rse_id)):
|
| 227 |
+
final_doc.ents[i]._.rse_id = sent_rse_id[i]
|
| 228 |
+
|
| 229 |
+
doc = final_doc
|
| 230 |
+
return doc.to_json()
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
|
| 234 |
def process_api(input_text):
|
| 235 |
# 这里编写实际的后端处理逻辑
|
| 236 |
|
| 237 |
return {
|
| 238 |
"status": "success",
|
| 239 |
# "result": f"Processed: {input_text.upper()}",
|
| 240 |
+
# "result": f"Processed: {nlp(input_text).to_json()}",
|
| 241 |
+
"result": f"Processed: {extract_spatial_entities(input_text)}",
|
| 242 |
"timestamp": time.time()
|
| 243 |
}
|
| 244 |
|
|
|
|
| 249 |
outputs="json",
|
| 250 |
title="Backend API",
|
| 251 |
allow_flagging="never"
|
| 252 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|