UploadVideoServlet.java
15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package com.bsth.data.zndd.voice;
import com.alibaba.fastjson.JSONObject;
import com.bsth.data.zndd.baidu.speech.restapi.asrdemo.AsrMain;
import com.bsth.entity.StationRoute;
import com.bsth.entity.sys.SysUser;
import com.bsth.security.util.SecurityUtils;
import com.bsth.service.StationRouteService;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Service
public class UploadVideoServlet extends HttpServlet {
@Autowired
StationRouteService stationRouteService;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("1");
}
//如果出现多线程的情况下 识别文件名称可以加上专属账号的属性
public void doPost(String line,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 得到上传文件的保存目录,将上传的文件存放于WEB-INF目录下,不允许外界直接访问,保证上传文件的安全
String savePath = "/pcm";
File file = new File(savePath);
// 判断上传文件的保存目录是否存在
if (!file.exists() && !file.isDirectory()) {
System.out.println(savePath + "目录不存在,需要创建");
// 创建目录
file.mkdir();
}
// 消息提示
String message = "";
try {
String filename = null;
// 使用Apache文件上传组件处理文件上传步骤:
// 1、创建一个DiskFileItemFactory工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
// 2、创建一个文件上传解析器
ServletFileUpload upload = new ServletFileUpload(factory);
// 解决上传文件名的中文乱码
upload.setHeaderEncoding("UTF-8");
// 3、判断提交上来的数据是否是上传表单的数据
if (!ServletFileUpload.isMultipartContent(request)) {
// 按照传统方式获取数据
return;
}
StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;
Iterator<String> iterator = req.getFileNames();
String[] value = new String[5];
int i = 0;
while (iterator.hasNext()) {
MultipartFile item = req.getFile(iterator.next());
SysUser user = SecurityUtils.getCurrentUser();
// 如果fileitem中封装的是上传文件
// 得到上传的文件名称,
filename = "test_"+user.getUserName()+".wav";
//item.getName();
//System.out.println(filename);
// 获取item中的上传文件的输入流
InputStream in = item.getInputStream();
// 创建一个文件输出流
FileOutputStream out = new FileOutputStream(savePath + "/" + filename);
// 创建一个缓冲区
byte buffer[] = new byte[1024];
// 判断输入流中的数据是否已经读完的标识
int len = 0;
// 循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
while ((len = in.read(buffer)) > 0) {
// 使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\"
// + filename)当中
out.write(buffer, 0, len);
}
// 关闭输入流
in.close();
// 关闭输出流
out.close();
// 删除处理文件上传时生成的临时文件
//item.delete();
message = "文件上传成功!";
//System.out.println(message);
/*VoiceUtil voiceUtil= new VoiceUtil();
String str = voiceUtil.getWord(savePath + "/" + filename);
str = str.replace(" ","");*/
//语音转文字
AsrMain demo = new AsrMain();
// 填写下面信息
String result = demo.run();
JSONObject json = JSONObject.parseObject(result);
List textList = ( List)json.get("result");
String str = textList.get(0).toString().replace("。","");
System.out.println("语音解析:"+str);
//定义输出类型
response.setHeader("Content-type", "text/html;charset=UTF-8");
PrintWriter writer = response.getWriter();
String text = textList.get(0).toString();
//text="新增出厂班次";
//text="帮我添加一个鸿音路南芦公路到临港大道枢纽站的班次";
/*if(isSimilarity(ZL.zdzdbc,text,null)){//帮我添加一个xx到xx站的班次(帮我添加一个鸿音路南芦公路到临港大道枢纽站的班次)
String[] ks = text.replace(",","").split("到");
if(ks[0].split("个").length==1){
writer.write(text+"_识别失败"+",999");
return;
}
if(ks[1].split("的").length==1){
writer.write(text+"_识别失败"+",999");
return;
}
String Station1= ks[0].split("个")[1];
String Station2= ks[1].split("的")[0];
*//* String fcsj = null;//线路名,上下行,发车时间
//发车时间
fcsj = extractTimes(text);*//*
//线路名称 - 线路编码和名称对照
Map<String, Object> param = new HashMap<>();
param.put("lineCode_eq", line);
param.put("directions_eq", 0);
param.put("destroy_eq", 0);
List<StationRoute> stationup = ((List<StationRoute>) stationRouteService.list(param));
//上行匹配
Map m = pyPp(stationup,Station1,Station2);
if (m.get("stationcode1") == null || m.get("stationcode2") == null){
param.put("directions_eq",1);
List<StationRoute> stationdown = ((List<StationRoute>) stationRouteService.list(param));
//下行匹配
Map smap = pyPp(stationdown,Station1,Station2);
if (smap.get("stationcode1") == null || smap.get("stationcode2") == null){
writer.write(text +","+"999");
return;
}
writer.write(text +",1,"+smap.get("stationcode1")+","+smap.get("stationcode2")+","+1);
return;
}
writer.write(text +",1,"+m.get("stationcode1")+","+m.get("stationcode2")+","+0);
return;
}*/
if(text.contains("轨迹")){
writer.write(text+"_"+ZL.LJCCBC.getDescription()+",66");
return;
}
if(isSimilarity(ZL.LJCCBC,text,null) || text.contains("出场")){//添加出厂班次
writer.write(text+"_"+ZL.LJCCBC.getDescription()+",2");
return;
}
if(isSimilarity(ZL.LJBC,text,null)){//添加班次
writer.write(text+"_"+ZL.LJBC.getDescription()+",3");
return;
}
if(isSimilarity(ZL.QXBC,text,null)){//取消班次
writer.write(text+"_"+ZL.QXBC.getDescription()+",11");
return;
}
if(isSimilarity(ZL.QXSF,text,null)){//取消实发
writer.write(text+"_"+ZL.QXSF.getDescription()+",12");
return;
}
if(isSimilarity(ZL.TZDF,text,"待")){//调整待发
writer.write(text+"_"+ZL.TZDF.getDescription()+",21");
return;
}
if(isSimilarity(ZL.TZSF,text,"实")){//调整实发
writer.write(text+"_"+ZL.TZSF.getDescription()+",22");
return;
}
if(isSimilarity(ZL.KQ,text,null)){//启动
writer.write(text+"_"+ZL.KQ.getDescription()+",31");
return;
}
if(isSimilarity(ZL.GB,text,null)){//关闭
writer.write(text+"_"+ZL.GB.getDescription()+",41");
return;
}
else {
writer.write(text+"_识别失败"+",999");
}
}
} catch (Exception e) {
message = "文件上传失败!";
System.out.println(message);
e.printStackTrace();
}
}
public static Map pyPp(List<StationRoute> stationss,String Station1,String Station2){
Double sity1 = 0D,sity2 = 0D; //拼音匹配百分比
Map m = new HashMap();
String stationcode1 = null,stationcode2 = null;//匹配的站点
Boolean st2 = false;
for (StationRoute st : stationss){
sity1 = calculateSimilarity(st.getStationName(), Station1);
if (sity1 >= 0.75 && !st2){
stationcode1 = st.getStationCode();
st2 =true;
}
if (st2){
sity2 = calculateSimilarity(st.getStationName(), Station2);
if(sity2 >= 0.75){
stationcode2 = st.getStationCode();
}
}
}
m.put("stationcode1",stationcode1);
m.put("stationcode2",stationcode2);
return m;
}
private static final Pattern TIME_PATTERN =
Pattern.compile("([0-5][0-9]:[0-5][0-9])");
//获取添加班次时间
public static String extractTimes(String text) {
Matcher matcher = TIME_PATTERN.matcher(text);
while (matcher.find()) {
return matcher.group();
}
return "";
}
class retun {
private String tite;
private String txt;
public String getTite() {
return tite;
}
public void setTite(String tite) {
this.tite = tite;
}
public String getTxt() {
return txt;
}
public void setTxt(String txt) {
this.txt = txt;
}
}
public static String getPinyin(String chinese) {
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
StringBuilder sb = new StringBuilder();
char[] chars = chinese.toCharArray();
for (char c : chars) {
if (Character.isWhitespace(c)) {
continue;
}
if (c >= '\u4e00' && c <= '\u9fa5') {
try {
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c, format);
sb.append(pinyinArray[0]);
} catch (BadHanyuPinyinOutputFormatCombination e) {
e.printStackTrace();
}
} else {
sb.append(c);
}
}
return sb.toString();
}
//去除标点符号
public static String removePunctuation(String input) {
String punctuationRegex = "[\\p{Punct}]";
input=input.replaceAll(punctuationRegex, "");
String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(input);
return input.replaceAll(punctuationRegex, "").replaceAll(regEx, "");
}
public static void main(String[] args) {
System.out.println(calculateSimilarity("临加班次", "撤销班次"));
}
public static Double calculateSimilarity(String str1, String str2) {
String pinyinStr1 = getPinyin(str1);
String pinyinStr2 = getPinyin(str2);
int len1 = pinyinStr1.length();
int len2 = pinyinStr2.length();
int[][] dp = new int[len1 + 1][len2 + 1];
for (int i = 0; i <= len1; i++) {
dp[i][0] = 0;
}
for (int j = 0; j <= len2; j++) {
dp[0][j] = 0;
}
for (int i = 1; i <= len1; i++) {
for (int j = 1; j <= len2; j++) {
if (pinyinStr1.charAt(i - 1) == pinyinStr2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return Double.valueOf(dp[len1][len2]) / len1;
}
/* public static boolean isSimilarity(Integer[] gjc,String str,String gjz){
Set<String> ml=new HashSet<>();
GJC.nestedLoop(Arrays.asList(gjc),"",ml);
for (String s : ml) {
if(calculateSimilarity(s,str)>= 0.75){//相似度
if(gjz==null){//不含关键字
return true;
}else {
if(UploadVideoServlet.getPinyin(str).contains(UploadVideoServlet.getPinyin(gjz))){
return true;
}
}
}
}
return false;
}*/
public static boolean isSimilarity(ZL zl,String str,String gjz){
Integer[] ints=zl.getCodes();
Set<String> ml=new HashSet<>();
GJC.nestedLoop(Arrays.asList(ints),"",ml);
for (String s : ml) {
double d=calculateSimilarity(s,str);
if(d>= 0.75){//相似度
if(gjz==null){//不含关键字
return true;
}else {
if(UploadVideoServlet.getPinyin(str).contains(UploadVideoServlet.getPinyin(gjz))){
return true;
}
}
}
}
return false;
}
}