File size: 942 Bytes
bfc0ec6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""A signal to search for a substring in a document."""
import re
from typing import Any, Iterable, Optional

from typing_extensions import override

from ..schema import Field, Item, RichData, SignalInputType, field, lilac_span
from ..signal import Signal


class SubstringSignal(Signal):
  """Find a substring in a document."""
  name = 'substring_search'
  display_name = 'Substring Search'
  input_type = SignalInputType.TEXT

  query: str

  _regex: re.Pattern[str]

  def __init__(self, **kwargs: Any):
    super().__init__(**kwargs)
    self._regex = re.compile(self.query, re.IGNORECASE)

  @override
  def fields(self) -> Field:
    return field(fields=['string_span'])

  @override
  def compute(self, data: Iterable[RichData]) -> Iterable[Optional[Item]]:
    for text in data:
      if not isinstance(text, str):
        yield None
        continue
      yield [lilac_span(m.start(), m.end()) for m in self._regex.finditer(text)]