Liusuthu commited on
Commit
6eab964
·
verified ·
1 Parent(s): 359a38a

Update consult_func.py

Browse files
Files changed (1) hide show
  1. consult_func.py +51 -9
consult_func.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  import os
 
3
 
4
  os.environ["no_proxy"] = "localhost,127.0.0.1,::1"
5
 
@@ -312,18 +313,59 @@ def visibility_choice4(choice): # 根据选择选项的不同决定下一道题
312
  return gr.Radio(visible=False), gr.Radio(visible=True)
313
 
314
 
315
- def keyword(
316
- text,
317
- ): # 设置关于自杀倾向问题的触发词(你决定要不要加一个文本负向概率大于一定值的判定)
318
- keywords = ["轻生", "自杀", "死", "活着", "意义", "意思", "干劲"]
319
- for i in range(len(keywords)):
320
- if keywords[i].find(text) != -1:
321
- return True
322
- return False
323
-
324
 
325
  def visibility_choice5(text): # 关于自杀倾向问题是否触发
326
  if keyword(text) is True:
327
  return gr.Radio(visible=True), gr.Column(visible=False)
328
  else:
329
  return gr.Radio(visible=False), gr.Column(visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import os
3
+ from app_utils import text_score
4
 
5
  os.environ["no_proxy"] = "localhost,127.0.0.1,::1"
6
 
 
313
  return gr.Radio(visible=False), gr.Radio(visible=True)
314
 
315
 
 
 
 
 
 
 
 
 
 
316
 
317
  def visibility_choice5(text): # 关于自杀倾向问题是否触发
318
  if keyword(text) is True:
319
  return gr.Radio(visible=True), gr.Column(visible=False)
320
  else:
321
  return gr.Radio(visible=False), gr.Column(visible=True)
322
+
323
+
324
+ ##########################################################
325
+ # 获取next数组
326
+ def get_next(T):
327
+ i = 0
328
+ j = -1
329
+ next_val = [-1] * len(T)
330
+ while i < len(T) - 1:
331
+ if j == -1 or T[i] == T[j]:
332
+ i += 1
333
+ j += 1
334
+ # next_val[i] = j
335
+ if i < len(T) and T[i] != T[j]:
336
+ next_val[i] = j
337
+ else:
338
+ next_val[i] = next_val[j]
339
+ else:
340
+ j = next_val[j]
341
+ return next_val
342
+
343
+
344
+ # KMP算法
345
+ def kmp(S, T):
346
+ i = 0
347
+ j = 0
348
+ next = get_next(T)
349
+ while i < len(S) and j < len(T):
350
+ if j == -1 or S[i] == T[j]:
351
+ i += 1
352
+ j += 1
353
+ else:
354
+ j = next[j]
355
+ if j == len(T):
356
+ return i - j
357
+ else:
358
+ return -1
359
+
360
+ #识别负面词的函数
361
+ def keyword(
362
+ text
363
+ ):
364
+ negative_keywords = ["轻生", "自杀", "死", "活着", "意义", "意思", "干劲","自残","孤单","结束",
365
+ "救","血","割腕","歇斯底里","抑郁","创伤","遗书","遗嘱","痛苦","跳楼","双向",
366
+ "障碍","精神病","变态","一无是处","疯",]
367
+ text,score=text_score(text)
368
+ for word in negative_keywords:
369
+ if kmp(text,word)!=-1 and score>=0:
370
+ return True
371
+ return False