MCPcopy Create free account
hub / github.com/KoljaB/RealtimeVoiceChat / TextContext

Class TextContext

code/text_context.py:7–73  ·  view source on GitHub ↗

Extracts meaningful text segments (contexts) from a given string. This class identifies substrings that end with predefined split tokens, adhere to specified length constraints, and contain a minimum number of alphanumeric characters.

Source from the content-addressed store, hash-verified

5from colors import Colors # Assuming this is needed externally
6
7class TextContext:
8 """
9 Extracts meaningful text segments (contexts) from a given string.
10
11 This class identifies substrings that end with predefined split tokens,
12 adhere to specified length constraints, and contain a minimum number
13 of alphanumeric characters.
14 """
15 def __init__(self, split_tokens: Optional[Set[str]] = None) -> None:
16 """
17 Initializes the TextContext processor.
18
19 Sets up the characters used to determine valid context boundaries.
20
21 Args:
22 split_tokens: An optional set of strings. Each string is treated as a
23 potential end-of-context marker. If None, a default set
24 of punctuation and whitespace characters is used.
25 """
26 if split_tokens is None:
27 # Using a more explicit variable name internally for clarity
28 default_splits: Set[str] = {".", "!", "?", ",", ";", ":", "\n", "-", "。", "、"}
29 self.split_tokens: Set[str] = default_splits
30 else:
31 self.split_tokens: Set[str] = set(split_tokens)
32
33 def get_context(self, txt: str, min_len: int = 6, max_len: int = 120, min_alnum_count: int = 10) -> Tuple[Optional[str], Optional[str]]:
34 """
35 Finds the shortest valid context at the beginning of the input text.
36
37 Scans the text `txt` from the beginning up to `max_len` characters. It looks
38 for the first occurrence of a character from `self.split_tokens`. If found,
39 it checks if the substring ending at that token meets the `min_len` (overall
40 length) and `min_alnum_count` (alphanumeric character count) criteria.
41
42 Args:
43 txt: The input string from which to extract the context.
44 min_len: The minimum allowable overall length for the extracted context substring.
45 max_len: The maximum allowable overall length for the extracted context substring.
46 The search stops after examining this many characters.
47 min_alnum_count: The minimum number of alphanumeric characters required within
48 the extracted context substring.
49
50 Returns:
51 A tuple containing:
52 - The extracted context string if found, otherwise None.
53 - The remaining part of the input string after the context, otherwise None.
54 Returns (None, None) if no suitable context is found within the constraints.
55 """
56 alnum_count = 0
57
58 for i in range(1, min(len(txt), max_len) + 1):
59 char = txt[i - 1]
60 if char.isalnum():
61 alnum_count += 1
62
63 # Check if the current character is a potential context end
64 if char in self.split_tokens:

Callers 1

__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected