fix:测试拼音

上级 05e9109c
......@@ -187,6 +187,11 @@
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>com.hankcs</groupId>
<artifactId>hanlp</artifactId>
<version>portable-1.8.3</version>
</dependency>
</dependencies>
<build>
<plugins>
......
package com.kwan.springbootkwan.filter;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
......@@ -14,6 +19,7 @@ import java.io.IOException;
* @version : 2.2.0
* @date : 2023/7/9 22:54
*/
@Order(1)
@Component
public class CorsFilter implements Filter {
......@@ -25,7 +31,6 @@ public class CorsFilter implements Filter {
httpResponse.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
httpResponse.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
httpResponse.setHeader("Access-Control-Allow-Credentials", "true");
chain.doFilter(request, response);
}
}
package com.kwan.springbootkwan.filter;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
/**
* 添加过滤器
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/8/26 11:06
*/
@Component
@Order(2) // 设置过滤器的优先级为1,值越小,优先级越高
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// 过滤器逻辑
chain.doFilter(request, response);
}
}
package com.kwan.springbootkwan.utils;
import com.hankcs.hanlp.dictionary.py.Pinyin;
import com.hankcs.hanlp.dictionary.py.PinyinDictionary;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import java.util.List;
/**
* 拼音工具类
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/8/26 11:16
*/
public class PinyinUtil {
/**
* 获取中文完整拼音
*
* @param chineseStr
* @return
*/
public static String getPinyin(String chineseStr) {
List<Pinyin> pinyins = PinyinDictionary.convertToPinyin(chineseStr);
StringBuilder stringBuilder = new StringBuilder();
for (Pinyin pinyin : pinyins) {
stringBuilder.append(pinyin.getPinyinWithoutTone());
}
return stringBuilder.toString();
}
/**
* 获取中文拼音首字母
*
* @param chineseStr
* @return
*/
public static String getInitial(String chineseStr) {
List<Pinyin> pinyins = PinyinDictionary.convertToPinyin(chineseStr);
if (CollectionUtils.isEmpty(pinyins)) {
return StringUtils.EMPTY;
}
return String.valueOf(pinyins.get(0).getPinyinWithoutTone().charAt(0));
}
}
......@@ -5,7 +5,7 @@ import com.kwan.springbootkwan.entity.User;
import com.kwan.springbootkwan.mapper.UserMapper;
import com.kwan.springbootkwan.service.ISendMsgHandle;
import com.kwan.springbootkwan.utils.RedisIdWorker;
import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
......@@ -129,7 +129,7 @@ public class UserServiceImplTest {
}
latch.await();
long end = System.currentTimeMillis();
System.out.println("times = " + (end- begin));
System.out.println("times = " + (end - begin));
}
......
package com.kwan.springbootkwan.utils;
import com.kwan.springbootkwan.SpringBootKwanApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = SpringBootKwanApplication.class)
public class PinyinUtilTest {
@Test
public void testName() {
assert "ceshi".equals(PinyinUtil.getPinyin("测试"));
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册