Kevin Hu commited on
Commit
b03b316
·
1 Parent(s): a4f36a5

Clean query. (#4259)

Browse files

### What problem does this PR solve?

#4239

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)

Files changed (2) hide show
  1. agent/component/__init__.py +1 -1
  2. rag/nlp/query.py +7 -3
agent/component/__init__.py CHANGED
@@ -36,12 +36,12 @@ from .iteration import Iteration, IterationParam
36
  from .iterationitem import IterationItem, IterationItemParam
37
 
38
 
39
-
40
  def component_class(class_name):
41
  m = importlib.import_module("agent.component")
42
  c = getattr(m, class_name)
43
  return c
44
 
 
45
  __all__ = [
46
  "Begin",
47
  "BeginParam",
 
36
  from .iterationitem import IterationItem, IterationItemParam
37
 
38
 
 
39
  def component_class(class_name):
40
  m = importlib.import_module("agent.component")
41
  c = getattr(m, class_name)
42
  return c
43
 
44
+
45
  __all__ = [
46
  "Begin",
47
  "BeginParam",
rag/nlp/query.py CHANGED
@@ -59,7 +59,7 @@ class FulltextQueryer:
59
  "",
60
  ),
61
  (r"(^| )(what|who|how|which|where|why)('re|'s)? ", " "),
62
- (r"(^| )('s|'re|is|are|were|was|do|does|did|don't|doesn't|didn't|has|have|be|there|you|me|your|my|mine|just|please|may|i|should|would|wouldn't|will|won't|done|go|for|with|so|the|a|an|by|i'm|it's|he's|she's|they|they're|you're|as|by|on|in|at|up|out|down|of) ", " ")
63
  ]
64
  for r, p in patts:
65
  txt = re.sub(r, p, txt, flags=re.IGNORECASE)
@@ -67,7 +67,7 @@ class FulltextQueryer:
67
 
68
  def question(self, txt, tbl="qa", min_match:float=0.6):
69
  txt = re.sub(
70
- r"[ :\r\n\t,,。??/`!!&\^%%()^\[\]]+",
71
  " ",
72
  rag_tokenizer.tradi2simp(rag_tokenizer.strQ2B(txt.lower())),
73
  ).strip()
@@ -81,16 +81,20 @@ class FulltextQueryer:
81
  tks_w = [(re.sub(r"[ \\\"'^]", "", tk), w) for tk, w in tks_w]
82
  tks_w = [(re.sub(r"^[a-z0-9]$", "", tk), w) for tk, w in tks_w if tk]
83
  tks_w = [(re.sub(r"^[\+-]", "", tk), w) for tk, w in tks_w if tk]
 
84
  syns = []
85
  for tk, w in tks_w:
86
  syn = self.syn.lookup(tk)
87
  syn = rag_tokenizer.tokenize(" ".join(syn)).split()
88
  keywords.extend(syn)
89
- syn = ["\"{}\"^{:.4f}".format(s, w / 4.) for s in syn if s]
90
  syns.append(" ".join(syn))
91
 
92
  q = ["({}^{:.4f}".format(tk, w) + " {})".format(syn) for (tk, w), syn in zip(tks_w, syns) if tk and not re.match(r"[.^+\(\)-]", tk)]
93
  for i in range(1, len(tks_w)):
 
 
 
94
  q.append(
95
  '"%s %s"^%.4f'
96
  % (
 
59
  "",
60
  ),
61
  (r"(^| )(what|who|how|which|where|why)('re|'s)? ", " "),
62
+ (r"(^| )('s|'re|is|are|were|was|do|does|did|don't|doesn't|didn't|has|have|be|there|you|me|your|my|mine|just|please|may|i|should|would|wouldn't|will|won't|done|go|for|with|so|the|a|an|by|i'm|it's|he's|she's|they|they're|you're|as|by|on|in|at|up|out|down|of|to|or|and|if) ", " ")
63
  ]
64
  for r, p in patts:
65
  txt = re.sub(r, p, txt, flags=re.IGNORECASE)
 
67
 
68
  def question(self, txt, tbl="qa", min_match:float=0.6):
69
  txt = re.sub(
70
+ r"[ :|\r\n\t,,。??/`!!&^%%()\[\]{}<>]+",
71
  " ",
72
  rag_tokenizer.tradi2simp(rag_tokenizer.strQ2B(txt.lower())),
73
  ).strip()
 
81
  tks_w = [(re.sub(r"[ \\\"'^]", "", tk), w) for tk, w in tks_w]
82
  tks_w = [(re.sub(r"^[a-z0-9]$", "", tk), w) for tk, w in tks_w if tk]
83
  tks_w = [(re.sub(r"^[\+-]", "", tk), w) for tk, w in tks_w if tk]
84
+ tks_w = [(tk.strip(), w) for tk, w in tks_w if tk.strip()]
85
  syns = []
86
  for tk, w in tks_w:
87
  syn = self.syn.lookup(tk)
88
  syn = rag_tokenizer.tokenize(" ".join(syn)).split()
89
  keywords.extend(syn)
90
+ syn = ["\"{}\"^{:.4f}".format(s, w / 4.) for s in syn if s.strip()]
91
  syns.append(" ".join(syn))
92
 
93
  q = ["({}^{:.4f}".format(tk, w) + " {})".format(syn) for (tk, w), syn in zip(tks_w, syns) if tk and not re.match(r"[.^+\(\)-]", tk)]
94
  for i in range(1, len(tks_w)):
95
+ left, right = tks_w[i - 1][0].strip(), tks_w[i][0].strip()
96
+ if not left or not right:
97
+ continue
98
  q.append(
99
  '"%s %s"^%.4f'
100
  % (