Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
邬吉朋
ops
Commits
2f2fc3cf
Commit
2f2fc3cf
authored
Oct 22, 2025
by
wujipeng
Browse files
解析md文件
parent
c075730a
Changes
2
Show whitespace changes
Inline
Side-by-side
src/main/java/com/ruzhoukj/sms/controller/CertificateController.java
View file @
2f2fc3cf
package
com.ruzhoukj.sms.controller
;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
import
com.google.common.collect.Lists
;
import
com.platform.exception.BusinessException
;
import
com.platform.model.resp.Result
;
import
com.platform.utils.StringUtils
;
import
com.ruzhoukj.sms.entity.CertificateEntity
;
import
com.ruzhoukj.sms.model.vo.KzWorkVO
;
import
com.ruzhoukj.sms.pojo.vo.CertificateVO
;
import
com.ruzhoukj.sms.service.CertificateService
;
import
com.ruzhoukj.sms.service.KzWorkService
;
import
com.ruzhoukj.sms.util.MostSimilarStringFinder
;
import
io.swagger.annotations.ApiOperation
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.util.ObjectUtils
;
import
org.springframework.web.bind.annotation.GetMapping
;
...
...
@@ -33,6 +36,7 @@ public class CertificateController {
@Resource
private
KzWorkService
kzWorkService
;
@ApiOperation
(
"证书替换"
)
@GetMapping
(
"/replace"
)
public
Result
<
String
>
replace
(
@RequestParam
(
"name"
)
String
name
)
{
log
.
info
(
"name-------------{}"
,
name
);
...
...
@@ -44,9 +48,9 @@ public class CertificateController {
String
localDirectory
=
certificate
.
getPath
();
Boolean
crt
=
upload
(
localDirectory
,
certificate
.
getPrefix
()
+
".ruzhoukj.com.crt"
,
"account.ruzhoukj.com.crt"
);
Boolean
key
=
upload
(
localDirectory
,
certificate
.
getPrefix
()
+
".ruzhoukj.com.key"
,
"account.ruzhoukj.com.key"
);
if
(
crt
&&
key
){
if
(
crt
&&
key
)
{
return
Result
.
data
(
name
+
"证书替换成功"
);
}
else
{
}
else
{
return
Result
.
fail
(
name
+
"证书替换失败,请检查日志信息,排查错误原因"
);
}
}
...
...
@@ -70,11 +74,32 @@ public class CertificateController {
return
true
;
}
@ApiOperation
(
"运维记录"
)
@GetMapping
(
"/record"
)
public
Result
<
List
<
KzWorkVO
>>
record
(
@RequestParam
(
value
=
"name"
,
required
=
false
)
String
name
,
@RequestParam
(
value
=
"time"
,
required
=
false
)
String
time
)
{
public
Result
<
List
<
KzWorkVO
>>
record
(
@RequestParam
(
value
=
"name"
,
required
=
false
)
String
name
,
@RequestParam
(
value
=
"time"
,
required
=
false
)
String
time
)
{
log
.
info
(
"name-------------{}"
,
name
);
log
.
info
(
"time-------------{}"
,
time
);
return
Result
.
data
(
kzWorkService
.
record
(
name
,
time
));
}
@ApiOperation
(
"解析md文件"
)
@GetMapping
(
"/parse/md"
)
public
String
parseMd
(
@RequestParam
(
name
=
"md"
)
String
md
,
@RequestParam
(
name
=
"name"
)
String
name
)
{
log
.
info
(
"md-------------{}"
,
md
);
log
.
info
(
"name-------------{}"
,
name
);
List
<
String
>
filterData
=
Lists
.
newArrayList
();
String
[]
originalData
=
md
.
split
(
"\\|"
);
for
(
String
datum
:
originalData
)
{
if
(
StringUtils
.
isNotEmpty
(
datum
)
&&
!
datum
.
contains
(
"name"
)
&&
!
datum
.
contains
(
"---"
))
{
filterData
.
add
(
datum
.
replace
(
" "
,
""
).
replace
(
"\n"
,
""
).
replace
(
" "
,
""
));
}
}
for
(
String
filterDatum
:
filterData
)
{
log
.
info
(
"filterDatum-------------{}"
,
filterDatum
);
}
return
MostSimilarStringFinder
.
findMostSimilar
(
name
,
filterData
,
"COSINE"
);
}
}
\ No newline at end of file
src/main/java/com/ruzhoukj/sms/util/MostSimilarStringFinder.java
0 → 100644
View file @
2f2fc3cf
package
com.ruzhoukj.sms.util
;
import
java.util.*
;
import
org.apache.commons.collections4.CollectionUtils
;
// 用于Jaccard算法(需添加依赖)
// 注意:使用编辑距离和余弦相似度的实现如下,无需额外库。
public
class
MostSimilarStringFinder
{
/**
* 在给定集合中查找与目标字符串最相似的一个元素
* @param input 目标字符串
* @param candidates 待比较的字符串集合
* @param algorithm 使用的算法("JACCARD", "LEVENSHTEIN", "COSINE")
* @return 集合中最相似的字符串
*/
public
static
String
findMostSimilar
(
String
input
,
List
<
String
>
candidates
,
String
algorithm
)
{
if
(
candidates
==
null
||
candidates
.
isEmpty
())
{
return
null
;
// 处理空集合
}
String
mostSimilar
=
null
;
double
maxSimilarity
=
-
1.0
;
// 相似度初始化为最小值
for
(
String
candidate
:
candidates
)
{
double
currentSimilarity
=
0.0
;
switch
(
algorithm
.
toUpperCase
())
{
case
"JACCARD"
:
currentSimilarity
=
calculateJaccardSimilarity
(
input
,
candidate
);
break
;
case
"LEVENSHTEIN"
:
currentSimilarity
=
calculateLevenshteinSimilarity
(
input
,
candidate
);
break
;
case
"COSINE"
:
currentSimilarity
=
calculateCosineSimilarity
(
input
,
candidate
);
break
;
default
:
throw
new
IllegalArgumentException
(
"不支持的算法: "
+
algorithm
);
}
// 更新找到的最相似字符串
if
(
currentSimilarity
>
maxSimilarity
)
{
maxSimilarity
=
currentSimilarity
;
mostSimilar
=
candidate
;
}
}
System
.
out
.
println
(
"使用 "
+
algorithm
+
" 算法,最高相似度为: "
+
maxSimilarity
);
return
mostSimilar
;
}
// 1. Jaccard 相似度计算
public
static
double
calculateJaccardSimilarity
(
String
strA
,
String
strB
)
{
// 将字符串转换为字符集合
Set
<
Character
>
setA
=
new
HashSet
<>();
for
(
char
c
:
strA
.
toCharArray
())
{
setA
.
add
(
c
);
}
Set
<
Character
>
setB
=
new
HashSet
<>();
for
(
char
c
:
strB
.
toCharArray
())
{
setB
.
add
(
c
);
}
// 计算交集和并集
Set
<
Character
>
intersection
=
new
HashSet
<>(
setA
);
intersection
.
retainAll
(
setB
);
Set
<
Character
>
union
=
new
HashSet
<>(
setA
);
union
.
addAll
(
setB
);
if
(
union
.
isEmpty
())
{
return
0.0
;
// 避免除以零
}
return
(
double
)
intersection
.
size
()
/
union
.
size
();
}
// 2. 基于编辑距离的相似度计算 (归一化到0-1)
public
static
double
calculateLevenshteinSimilarity
(
String
s1
,
String
s2
)
{
int
editDistance
=
computeLevenshteinDistance
(
s1
,
s2
);
int
maxLength
=
Math
.
max
(
s1
.
length
(),
s2
.
length
());
if
(
maxLength
==
0
)
{
return
1.0
;
// 两个空字符串定义为完全相似
}
return
1.0
-
((
double
)
editDistance
/
maxLength
);
// 将距离转换为相似度
}
private
static
int
computeLevenshteinDistance
(
String
s1
,
String
s2
)
{
int
len1
=
s1
.
length
();
int
len2
=
s2
.
length
();
int
[][]
dp
=
new
int
[
len1
+
1
][
len2
+
1
];
for
(
int
i
=
0
;
i
<=
len1
;
i
++)
{
for
(
int
j
=
0
;
j
<=
len2
;
j
++)
{
if
(
i
==
0
)
{
dp
[
i
][
j
]
=
j
;
}
else
if
(
j
==
0
)
{
dp
[
i
][
j
]
=
i
;
}
else
{
int
cost
=
(
s1
.
charAt
(
i
-
1
)
==
s2
.
charAt
(
j
-
1
))
?
0
:
1
;
dp
[
i
][
j
]
=
Math
.
min
(
Math
.
min
(
dp
[
i
-
1
][
j
]
+
1
,
dp
[
i
][
j
-
1
]
+
1
),
dp
[
i
-
1
][
j
-
1
]
+
cost
);
}
}
}
return
dp
[
len1
][
len2
];
}
// 3. 余弦相似度计算
public
static
double
calculateCosineSimilarity
(
String
text1
,
String
text2
)
{
// 将字符串转换为词频向量
Map
<
String
,
Integer
>
vector1
=
getTermFrequency
(
text1
);
Map
<
String
,
Integer
>
vector2
=
getTermFrequency
(
text2
);
// 获取所有不重复的词汇
Set
<
String
>
vocabulary
=
new
HashSet
<>();
vocabulary
.
addAll
(
vector1
.
keySet
());
vocabulary
.
addAll
(
vector2
.
keySet
());
// 计算点积
double
dotProduct
=
0.0
;
for
(
String
word
:
vocabulary
)
{
int
freq1
=
vector1
.
getOrDefault
(
word
,
0
);
int
freq2
=
vector2
.
getOrDefault
(
word
,
0
);
dotProduct
+=
freq1
*
freq2
;
}
// 计算模长
double
normA
=
calculateNorm
(
vector1
);
double
normB
=
calculateNorm
(
vector2
);
if
(
normA
==
0
||
normB
==
0
)
{
return
0.0
;
// 避免除以零
}
return
dotProduct
/
(
normA
*
normB
);
}
// 辅助方法:计算词频
private
static
Map
<
String
,
Integer
>
getTermFrequency
(
String
text
)
{
// 简单分词:转为小写,按非字母数字分割
String
[]
words
=
text
.
toLowerCase
().
split
(
"\\W+"
);
Map
<
String
,
Integer
>
freqMap
=
new
HashMap
<>();
for
(
String
word
:
words
)
{
if
(!
word
.
isEmpty
())
{
freqMap
.
put
(
word
,
freqMap
.
getOrDefault
(
word
,
0
)
+
1
);
}
}
return
freqMap
;
}
// 辅助方法:计算向量的模长(欧几里得范数)
private
static
double
calculateNorm
(
Map
<
String
,
Integer
>
vector
)
{
double
sumSquares
=
0.0
;
for
(
int
freq
:
vector
.
values
())
{
sumSquares
+=
Math
.
pow
(
freq
,
2
);
}
return
Math
.
sqrt
(
sumSquares
);
}
// 测试示例
public
static
void
main
(
String
[]
args
)
{
List
<
String
>
stringCollection
=
Arrays
.
asList
(
"Where is the restroom?"
,
"Can you tell me where the bathroom is?"
,
"How do I get to the nearest toilet?"
,
"Where is the front desk?"
);
String
inputString
=
"Can you help me find the bathroom?"
;
// 使用不同的算法进行匹配
String
resultByJaccard
=
findMostSimilar
(
inputString
,
stringCollection
,
"JACCARD"
);
System
.
out
.
println
(
"Jaccard 最相似: "
+
resultByJaccard
+
"\n"
);
String
resultByLevenshtein
=
findMostSimilar
(
inputString
,
stringCollection
,
"LEVENSHTEIN"
);
System
.
out
.
println
(
"编辑距离最相似: "
+
resultByLevenshtein
+
"\n"
);
String
resultByCosine
=
findMostSimilar
(
inputString
,
stringCollection
,
"COSINE"
);
System
.
out
.
println
(
"余弦最相似: "
+
resultByCosine
);
}
}
\ No newline at end of file
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