Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
sl-ai
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhouwei
sl-ai
Commits
b4190407
Commit
b4190407
authored
Aug 06, 2025
by
zhouwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增强新闻提取国家和城市信息逻辑
parent
7789b6b4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
7 deletions
+67
-7
NewsCountryCity.java
.../java/com/nanyan/securitylink/entity/NewsCountryCity.java
+11
-0
AIServiceImpl.java
...a/com/nanyan/securitylink/service/impl/AIServiceImpl.java
+56
-7
No files found.
src/main/java/com/nanyan/securitylink/entity/NewsCountryCity.java
0 → 100644
View file @
b4190407
package
com
.
nanyan
.
securitylink
.
entity
;
import
lombok.Data
;
import
java.util.List
;
@Data
public
class
NewsCountryCity
{
List
<
String
>
country
;
List
<
String
>
city
;
}
src/main/java/com/nanyan/securitylink/service/impl/AIServiceImpl.java
View file @
b4190407
...
...
@@ -25,10 +25,7 @@ import org.springframework.http.ResponseEntity;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.client.RestTemplate
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.*
;
@Slf4j
...
...
@@ -596,8 +593,8 @@ public class AIServiceImpl implements AIService {
" - 对\"某国\"\"该地区\"等表述,需结合上下文地理特征、政治背景推断\n"
+
" - 非中文表述需翻译为中文规范名称\n"
+
"4. 输出规范:\n"
+
"
- 仅返回标准JSON格式:{\"country\": \"country\", \"city\": \"city\"
}\n"
+
" - 无地理信息时返回:{\"country\":
\"\", \"city\": \"\"
}\n"
+
"
- 仅返回标准JSON格式:{\"country\": [\"country\"], \"city\": [\"city\"]
}\n"
+
" - 无地理信息时返回:{\"country\":
[], \"city\": []
}\n"
+
" - 禁止包含任何解释性文字或XML标签\n"
+
" - 翻译成英文\n"
+
"5. 验证逻辑:\n"
+
...
...
@@ -608,10 +605,11 @@ public class AIServiceImpl implements AIService {
"- 国家城市名称必须使用新闻原文中的语言版本\n"
+
"- 不存在的行政关系需视为无效信息(如\"巴黎属于德国\")\n"
+
"- 多地点新闻只提取核心事件发生地\n"
+
"- 返回的国家和城市名称必须是通俗意义的常见的英文名称,不能输出国家和城市中文名称\n"
+
"\n"
+
"<additional_rules>\n"
+
"1. 国家城市名称标准化:\n"
+
" - 优先使用主权国家名称(如\"中国\"而非\"中国大陆\")
\n
"
;
" - 优先使用主权国家名称(如\"中国\"而非\"中国大陆\")"
;
buildChatInputData
(
chatInputData
,
aiRequestDTO
.
getInputs
().
getMsg_info
(),
systemPrompt
);
String
apiKey
=
getModelApiKey
();
ChatCompletionResponse
chatCompletionResponse
=
aiRequest
(
chatInputData
,
apiKey
);
...
...
@@ -626,6 +624,7 @@ public class AIServiceImpl implements AIService {
}
content
=
content
.
replaceAll
(
"```json"
,
""
).
replaceAll
(
"```"
,
""
).
replaceAll
(
"\n"
,
""
);
JSONObject
jsonObject
=
JSONObject
.
parseObject
(
content
);
jsonObject
=
convertData
(
jsonObject
);
List
<
JSONObject
>
jsonObjects
=
new
ArrayList
<>();
jsonObjects
.
add
(
jsonObject
);
ResultVO
<
JSONObject
>
outputs
=
new
ResultVO
<>();
...
...
@@ -636,6 +635,56 @@ public class AIServiceImpl implements AIService {
}
return
null
;
}
private
static
Map
<
String
,
String
>
country
=
new
HashMap
<>();
private
static
Map
<
String
,
String
>
city
=
new
HashMap
<>();
static
{
country
.
put
(
"us"
,
"United States"
);
country
.
put
(
"usa"
,
"United States"
);
country
.
put
(
"the united states"
,
"United States"
);
city
.
put
(
"washington"
,
"Washington"
);
city
.
put
(
" d.c."
,
"Washington"
);
city
.
put
(
"washington dc"
,
"Washington"
);
city
.
put
(
"washington d.c."
,
"Washington"
);
city
.
put
(
"san diego"
,
"Santiago"
);
city
.
put
(
"伊斯兰堡"
,
"Islamabad"
);
city
.
put
(
"Manila"
,
"metro manila"
);
city
.
put
(
"Kiev"
,
"kyiv"
);
city
.
put
(
"孟买"
,
"Mumbai"
);
city
.
put
(
"bombay"
,
"Mumbai"
);
city
.
put
(
"delhi-ncr"
,
"New Delhi"
);
city
.
put
(
"伦敦"
,
"London"
);
city
.
put
(
"new york city"
,
"New York"
);
city
.
put
(
"旧金山"
,
"San Francisco"
);
city
.
put
(
"santa cruz de la sierra"
,
"Santa Cruz"
);
city
.
put
(
"bengaluru"
,
"Bangalore"
);
city
.
put
(
"达卡"
,
"Dhaka"
);
city
.
put
(
"尼斯"
,
"saint paul"
);
}
private
JSONObject
convertData
(
JSONObject
jsonObject
){
try
{
NewsCountryCity
newsCountryCity
=
JSONObject
.
parseObject
(
JSONObject
.
toJSONString
(
jsonObject
),
NewsCountryCity
.
class
);
if
(
CollectionUtils
.
isNotEmpty
(
newsCountryCity
.
getCity
())){
Set
<
String
>
set
=
new
HashSet
<>();
for
(
String
value
:
newsCountryCity
.
getCity
())
{
set
.
add
(
city
.
getOrDefault
(
value
,
value
));
}
newsCountryCity
.
getCity
().
clear
();
newsCountryCity
.
getCity
().
addAll
(
set
);
}
if
(
CollectionUtils
.
isNotEmpty
(
newsCountryCity
.
getCountry
())){
Set
<
String
>
set
=
new
HashSet
<>();
for
(
String
value
:
newsCountryCity
.
getCountry
())
{
set
.
add
(
country
.
getOrDefault
(
value
,
value
));
}
}
return
JSONObject
.
parseObject
(
JSONObject
.
toJSONString
(
newsCountryCity
));
}
catch
(
Exception
e
){
log
.
info
(
""
,
e
);
return
jsonObject
;
}
}
private
String
getModelApiKey
()
{
UserHeader
userHeader
=
UserThreadLocal
.
get
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment