input-cleaner 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,177 @@
1
+ Metadata-Version: 2.4
2
+ Name: input_cleaner
3
+ Version: 0.1.0
4
+ Summary: Text cleaning, scoring and compression pipeline
5
+ Author: marlon(MARLM8)
6
+ Requires-Python: >=3.8
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: tiktoken
10
+ Dynamic: license-file
11
+
12
+ # Input Cleaner
13
+
14
+ Lightweight text preprocessing pipeline for cleaning, scoring and compressing user input.
15
+
16
+ Designed for LLM optimization, noise reduction and token efficiency.
17
+
18
+ ---
19
+
20
+ ## Features
21
+
22
+ * Text normalization and cleanup
23
+ * Noise detection using heuristic scoring
24
+ * Input compression (removes low-value words)
25
+ * Quality scoring system (0–100)
26
+ * Smart truncation for long inputs
27
+ * Token reduction tracking using `tiktoken`
28
+
29
+ ---
30
+
31
+ ## Installation
32
+
33
+ ```bash
34
+ pip install input_cleaner
35
+ ```
36
+
37
+ Or local development:
38
+
39
+ ```bash
40
+ pip install -e .
41
+ ```
42
+
43
+ ---
44
+
45
+ ## Quick Start
46
+
47
+ ```python
48
+ from input_cleaner import process
49
+
50
+ result = process("hello hello I really really need help with this")
51
+
52
+ print(result)
53
+ ```
54
+
55
+ ---
56
+
57
+ ## Example Output
58
+
59
+ ```json
60
+ {
61
+ "original": "hello hello I really really need help with this",
62
+ "cleaned": "hello i really need help with this",
63
+ "compressed": "need help this",
64
+ "score": 78,
65
+ "status": "compress",
66
+ "output": "need help this",
67
+ "token_reduction": 0.32
68
+ }
69
+ ```
70
+
71
+ ---
72
+
73
+ ## Pipeline Overview
74
+
75
+ The system processes text in 4 stages:
76
+
77
+ 1. **Cleaning**
78
+
79
+ * Normalize text (lowercase, symbols)
80
+ * Remove duplicates and noise patterns
81
+
82
+ 2. **Compression**
83
+
84
+ * Remove weak or redundant words
85
+ * Preserve important tokens
86
+
87
+ 3. **Scoring**
88
+
89
+ * Evaluate text quality using:
90
+
91
+ * entropy
92
+ * character diversity
93
+ * vowel ratio
94
+ * pattern detection
95
+
96
+ 4. **Decision**
97
+
98
+ * Block low-quality input
99
+ * Compress medium-quality input
100
+ * Accept high-quality input
101
+ * Truncate long input if needed
102
+
103
+ ---
104
+
105
+ ## Status Values
106
+
107
+ | Status | Meaning |
108
+ | ------------------- | --------------------- |
109
+ | `blocked_empty` | Empty input |
110
+ | `blocked_low_score` | Low quality score |
111
+ | `blocked_noise` | Likely random string |
112
+ | `blocked_low_info` | Too short / low value |
113
+ | `accepted_short` | Short but valid |
114
+ | `compress` | Needs compression |
115
+ | `truncated_smart` | Long input trimmed |
116
+ | `accepted` | Fully valid |
117
+
118
+ ---
119
+
120
+ ## Use Cases
121
+
122
+ * Preprocessing user input for LLMs
123
+ * Reducing token costs
124
+ * Filtering spam or noisy text
125
+ * Improving prompt quality
126
+ * Input validation layer for APIs
127
+
128
+ ---
129
+
130
+ ## Requirements
131
+
132
+ * Python 3.8+
133
+ * `tiktoken`
134
+
135
+ ---
136
+
137
+ ## Project Structure
138
+
139
+ ```bash
140
+ text_pipeline/
141
+ ├── cleaner.py
142
+ ├── compressor.py
143
+ ├── scorer.py
144
+ ├── decision.py
145
+ └── pipeline.py
146
+ ```
147
+
148
+ ---
149
+
150
+ ## Design Goals
151
+
152
+ * Fast (pure Python, no heavy dependencies)
153
+ * Deterministic (no randomness)
154
+ * Modular (each step isolated)
155
+ * Easy to extend
156
+
157
+ ---
158
+
159
+ ## Roadmap
160
+
161
+ * Add trigram scoring
162
+ * Language detection
163
+ * Configurable thresholds
164
+ * FastAPI wrapper
165
+ * Streaming support
166
+
167
+ ---
168
+
169
+ ## License
170
+
171
+ Apache-2.0
172
+
173
+ ---
174
+
175
+ ## Author
176
+
177
+ marlon (MARLM8)
@@ -0,0 +1,166 @@
1
+ # Input Cleaner
2
+
3
+ Lightweight text preprocessing pipeline for cleaning, scoring and compressing user input.
4
+
5
+ Designed for LLM optimization, noise reduction and token efficiency.
6
+
7
+ ---
8
+
9
+ ## Features
10
+
11
+ * Text normalization and cleanup
12
+ * Noise detection using heuristic scoring
13
+ * Input compression (removes low-value words)
14
+ * Quality scoring system (0–100)
15
+ * Smart truncation for long inputs
16
+ * Token reduction tracking using `tiktoken`
17
+
18
+ ---
19
+
20
+ ## Installation
21
+
22
+ ```bash
23
+ pip install input_cleaner
24
+ ```
25
+
26
+ Or local development:
27
+
28
+ ```bash
29
+ pip install -e .
30
+ ```
31
+
32
+ ---
33
+
34
+ ## Quick Start
35
+
36
+ ```python
37
+ from input_cleaner import process
38
+
39
+ result = process("hello hello I really really need help with this")
40
+
41
+ print(result)
42
+ ```
43
+
44
+ ---
45
+
46
+ ## Example Output
47
+
48
+ ```json
49
+ {
50
+ "original": "hello hello I really really need help with this",
51
+ "cleaned": "hello i really need help with this",
52
+ "compressed": "need help this",
53
+ "score": 78,
54
+ "status": "compress",
55
+ "output": "need help this",
56
+ "token_reduction": 0.32
57
+ }
58
+ ```
59
+
60
+ ---
61
+
62
+ ## Pipeline Overview
63
+
64
+ The system processes text in 4 stages:
65
+
66
+ 1. **Cleaning**
67
+
68
+ * Normalize text (lowercase, symbols)
69
+ * Remove duplicates and noise patterns
70
+
71
+ 2. **Compression**
72
+
73
+ * Remove weak or redundant words
74
+ * Preserve important tokens
75
+
76
+ 3. **Scoring**
77
+
78
+ * Evaluate text quality using:
79
+
80
+ * entropy
81
+ * character diversity
82
+ * vowel ratio
83
+ * pattern detection
84
+
85
+ 4. **Decision**
86
+
87
+ * Block low-quality input
88
+ * Compress medium-quality input
89
+ * Accept high-quality input
90
+ * Truncate long input if needed
91
+
92
+ ---
93
+
94
+ ## Status Values
95
+
96
+ | Status | Meaning |
97
+ | ------------------- | --------------------- |
98
+ | `blocked_empty` | Empty input |
99
+ | `blocked_low_score` | Low quality score |
100
+ | `blocked_noise` | Likely random string |
101
+ | `blocked_low_info` | Too short / low value |
102
+ | `accepted_short` | Short but valid |
103
+ | `compress` | Needs compression |
104
+ | `truncated_smart` | Long input trimmed |
105
+ | `accepted` | Fully valid |
106
+
107
+ ---
108
+
109
+ ## Use Cases
110
+
111
+ * Preprocessing user input for LLMs
112
+ * Reducing token costs
113
+ * Filtering spam or noisy text
114
+ * Improving prompt quality
115
+ * Input validation layer for APIs
116
+
117
+ ---
118
+
119
+ ## Requirements
120
+
121
+ * Python 3.8+
122
+ * `tiktoken`
123
+
124
+ ---
125
+
126
+ ## Project Structure
127
+
128
+ ```bash
129
+ text_pipeline/
130
+ ├── cleaner.py
131
+ ├── compressor.py
132
+ ├── scorer.py
133
+ ├── decision.py
134
+ └── pipeline.py
135
+ ```
136
+
137
+ ---
138
+
139
+ ## Design Goals
140
+
141
+ * Fast (pure Python, no heavy dependencies)
142
+ * Deterministic (no randomness)
143
+ * Modular (each step isolated)
144
+ * Easy to extend
145
+
146
+ ---
147
+
148
+ ## Roadmap
149
+
150
+ * Add trigram scoring
151
+ * Language detection
152
+ * Configurable thresholds
153
+ * FastAPI wrapper
154
+ * Streaming support
155
+
156
+ ---
157
+
158
+ ## License
159
+
160
+ Apache-2.0
161
+
162
+ ---
163
+
164
+ ## Author
165
+
166
+ marlon (MARLM8)
@@ -0,0 +1,5 @@
1
+ # __init__.py
2
+
3
+ from .input_cleaner.pipeline import process
4
+
5
+ __all__ = ["process"]
@@ -0,0 +1,182 @@
1
+ # cleaner.py
2
+ import re
3
+ import math
4
+ from collections import Counter
5
+
6
+ # CONFIG PANEL
7
+ ALLOWED = r",\.\?\(\)\/\-\+\"=<>\:@%#"
8
+
9
+ CONFIG = {
10
+ # Normalization
11
+ "lowercase": True,
12
+ "replace_semicolon": True,
13
+ "replace_quotes": True,
14
+
15
+ # Duplicates
16
+ "remove_duplicate_words": True,
17
+
18
+ # Repeats
19
+ "max_char_repetition": 3,
20
+
21
+ # Heuristics
22
+ "enable_noise_filter": True,
23
+ "min_word_length": 10,
24
+
25
+ # Metrics
26
+ "diversity_threshold": 0.45,
27
+ "entropy_threshold": 3.5,
28
+ "min_vowel_ratio": 0.2,
29
+
30
+ # Suspicious patterns
31
+ "keyboard_patterns": ["qwerty", "asdf", "zxcv"],
32
+ "weird_bigrams": ["jh", "kj", "zx", "qk", "wk"],
33
+
34
+ # Scoring
35
+ "score_threshold": 2,
36
+
37
+ # Action
38
+ "truncate_noise": True,
39
+ "truncate_length": 8,
40
+
41
+ # Artificial lists
42
+ "max_commas": 5,
43
+
44
+ # Final cleanup
45
+ "collapse_symbols": True,
46
+ "normalize_spaces": True,
47
+ }
48
+
49
+
50
+ # METRICS
51
+ def entropy(p):
52
+ freq = Counter(p)
53
+ n = len(p)
54
+ return -sum((c/n) * math.log2(c/n) for c in freq.values())
55
+
56
+
57
+ def vowel_ratio(p):
58
+ vowels = re.findall(r'[aeiouáéíóú]', p)
59
+ return len(vowels) / len(p) if p else 0
60
+
61
+
62
+ # NOISE CLASSIFIER
63
+ def noise_score(p, cfg):
64
+ score = 0
65
+
66
+ if len(p) < cfg["min_word_length"]:
67
+ return 0
68
+
69
+ if any(c.isdigit() for c in p):
70
+ return 0
71
+
72
+ # Diversity
73
+ diversity = len(set(p)) / len(p)
74
+ if diversity < cfg["diversity_threshold"]:
75
+ score += 1
76
+
77
+ # High entropy (randomness)
78
+ if entropy(p) > cfg["entropy_threshold"]:
79
+ score += 1
80
+
81
+ # Low vowels
82
+ if vowel_ratio(p) < cfg["min_vowel_ratio"]:
83
+ score += 1
84
+
85
+ # Structural repeats
86
+ if re.search(r'(.{2,4})\1{2,}', p):
87
+ score += 1
88
+
89
+ # Keyboard patterns
90
+ if any(x in p for x in cfg["keyboard_patterns"]):
91
+ score += 1
92
+
93
+ # Weird bigrams
94
+ if any(bg in p for bg in cfg["weird_bigrams"]):
95
+ score += 1
96
+
97
+ return score
98
+
99
+
100
+ def reduce_if_noise(match, cfg):
101
+ p = match.group(0)
102
+ score = noise_score(p, cfg)
103
+
104
+ if score >= cfg["score_threshold"]:
105
+ if cfg["truncate_noise"]:
106
+ return p[:cfg["truncate_length"]]
107
+ return ""
108
+
109
+ return p
110
+
111
+
112
+ # MAIN FILTER
113
+ def filter_input(text: str) -> str:
114
+ cfg = CONFIG
115
+
116
+ # Normalization
117
+ if cfg["lowercase"]:
118
+ text = text.lower()
119
+
120
+ if cfg["replace_semicolon"]:
121
+ text = text.replace(";", ",")
122
+
123
+ if cfg["replace_quotes"]:
124
+ text = text.replace("'", '"')
125
+
126
+ text = re.sub(r'[\[\{]', '(', text)
127
+ text = re.sub(r'[\]\}]', ')', text)
128
+
129
+ # Base filter
130
+ text = re.sub(
131
+ rf'[^a-záéíóúñüäëïöâêîôûàèìòù0-9\s{ALLOWED}]',
132
+ '',
133
+ text
134
+ )
135
+
136
+ # Duplicates
137
+ if cfg["remove_duplicate_words"]:
138
+ text = re.sub(r'\b(\w+)([\s.,]+\1\b)+', r'\1', text)
139
+
140
+ # Repeats
141
+ text = re.sub(
142
+ rf'(.)\1{{{cfg["max_char_repetition"]},}}',
143
+ r'\1',
144
+ text
145
+ )
146
+
147
+ # Weird patterns
148
+ text = re.sub(
149
+ r'([<>\W])([a-z0-9])(?:\1\2){2,}',
150
+ r'\2',
151
+ text
152
+ )
153
+
154
+ # Advanced heuristic
155
+ if cfg["enable_noise_filter"]:
156
+ text = re.sub(
157
+ r'\b[a-záéíóúñüäëïöâêîôûàèìòù]{%d,}\b' % cfg["min_word_length"],
158
+ lambda m: reduce_if_noise(m, cfg),
159
+ text
160
+ )
161
+
162
+ # Artificial lists
163
+ if text.count(",") > cfg["max_commas"]:
164
+ text = re.sub(
165
+ r'\b([a-z0-9])(?:,[a-z0-9]){3,}\b',
166
+ r'\1',
167
+ text
168
+ )
169
+
170
+ # Collapse symbols
171
+ if cfg["collapse_symbols"]:
172
+ text = re.sub(
173
+ rf'([{ALLOWED}])(\s*[{ALLOWED}])+',
174
+ r'\1',
175
+ text
176
+ )
177
+
178
+ # Spaces
179
+ if cfg["normalize_spaces"]:
180
+ text = re.sub(r'\s+', ' ', text).strip()
181
+
182
+ return text
@@ -0,0 +1,94 @@
1
+ # Compressor.py
2
+
3
+ import re
4
+
5
+ # 🔹 CONFIG
6
+ ALWAYS_REMOVE = {
7
+ # chat noise
8
+ "hello", "hey", "ok", "alright", "well",
9
+ "thanks", "please", "pls",
10
+
11
+ # articles
12
+ "the", "a", "an", "some"
13
+ }
14
+
15
+ OPTIONAL_REMOVE = {
16
+ # weak verbs
17
+ "need", "want", "wish", "can", "could",
18
+ "would", "like",
19
+
20
+ # prepositions
21
+ "of", "from", "to", "in", "on", "by", "for", "with", "without",
22
+
23
+ # pronouns
24
+ "me", "you", "him", "her", "us", "them", "it",
25
+
26
+ # intensifiers
27
+ "very", "much", "quite", "really", "super"
28
+ }
29
+
30
+ KEEP_ALWAYS = {
31
+ # negations
32
+ "no", "never",
33
+
34
+ # comparators
35
+ "more", "less", "better", "worse",
36
+
37
+ # logical connectors
38
+ "but", "because", "although", "if", "then",
39
+
40
+ # references
41
+ "this", "that", "those", "same"
42
+ }
43
+
44
+
45
+ # COMPRESSOR
46
+ def compress_input(text: str) -> str:
47
+ if not text:
48
+ return ""
49
+
50
+ text = text.lower()
51
+
52
+ # remove consecutive duplicates
53
+ text = re.sub(r'\b(\w+)(\s+\1\b)+', r'\1', text)
54
+
55
+ words = text.split()
56
+ result = []
57
+
58
+ for w in words:
59
+
60
+ # KEEP_ALWAYS → always keep
61
+ if w in KEEP_ALWAYS:
62
+ result.append(w)
63
+ continue
64
+
65
+ # ALWAYS_REMOVE → always drop
66
+ if w in ALWAYS_REMOVE:
67
+ continue
68
+
69
+ # STRUCTURED DATA
70
+ if any(c.isdigit() for c in w):
71
+ result.append(w)
72
+ continue
73
+
74
+ # VERY SHORT WORDS
75
+ if len(w) < 3:
76
+ continue
77
+
78
+ # REPEATED PATTERNS (noise)
79
+ if re.search(r'(.{2,3})\1{2,}', w):
80
+ continue
81
+
82
+ # OPTIONAL_REMOVE
83
+ if w in OPTIONAL_REMOVE:
84
+ # drop only if enough context
85
+ if len(words) > 3:
86
+ continue
87
+ else:
88
+ result.append(w)
89
+ continue
90
+
91
+ # DEFAULT → keep
92
+ result.append(w)
93
+
94
+ return " ".join(result)
@@ -0,0 +1,38 @@
1
+ # decision.py
2
+
3
+ def decide_input(text: str, score: float):
4
+
5
+ if not text:
6
+ return None, "blocked_empty"
7
+
8
+ words = text.split()
9
+ word_count = len(words)
10
+ length = len(text)
11
+
12
+ # HARD BLOCK
13
+ if score < 40:
14
+ return None, "blocked_low_score"
15
+
16
+ # typical noise (random strings)
17
+ if word_count == 1 and length > 12:
18
+ return None, "blocked_noise"
19
+
20
+ # too short input (low value)
21
+ if word_count <= 1 and length < 4:
22
+ return None, "blocked_low_info"
23
+
24
+ # SHORT USEFUL TEXT (KEEP)
25
+ if word_count <= 4 and score >= 70:
26
+ return text, "accepted_short"
27
+
28
+ # NEED COMPRESSION
29
+ if score < 80:
30
+ return text, "compress"
31
+
32
+ # LONG TEXT → SMART TRIM
33
+ if length > 180 or word_count > 30:
34
+ useful_words = words[:40]
35
+ return " ".join(useful_words), "truncated_smart"
36
+
37
+ # ACCEPTED
38
+ return text, "accepted"
@@ -0,0 +1,36 @@
1
+ # pipeline.py
2
+
3
+ from .cleaner import filter_input
4
+ from .scorer import score_input
5
+ from .decision import decide_input
6
+ from .compressor import compress_input
7
+ import tiktoken
8
+
9
+
10
+ def process(text: str):
11
+ original = text
12
+
13
+ cleaned = filter_input(original)
14
+ compressed = compress_input(cleaned)
15
+
16
+ score = score_input(cleaned)
17
+
18
+ output, status = decide_input(compressed, score)
19
+
20
+ # tiktoken (measure token reduction)
21
+ enc = tiktoken.encoding_for_model("gpt-4o-mini") # or your model
22
+ tokens_before = len(enc.encode(original))
23
+ tokens_after = len(enc.encode(output)) if output else 0
24
+
25
+ return {
26
+ "original": original,
27
+ "cleaned": cleaned,
28
+ "compressed": compressed,
29
+ "score": score,
30
+ "status": status,
31
+ "output": output,
32
+ "token_reduction": (
33
+ 1 - (tokens_after / tokens_before)
34
+ if tokens_before > 0 else 0
35
+ )
36
+ }
@@ -0,0 +1,125 @@
1
+ # scorer.py
2
+
3
+ import re
4
+ import math
5
+ from collections import Counter
6
+
7
+ # CONFIG
8
+ VOWELS = "aeiou"
9
+
10
+ COMMON_BIGRAMS = {
11
+ "th", "he", "in", "er", "an", "re", "on", "at", "en",
12
+ "nd", "ti", "es", "or", "te", "of", "ed", "is", "it",
13
+ "al", "ar", "st", "to", "nt", "ng", "se", "ha", "as"
14
+ }
15
+
16
+
17
+ # METRICS
18
+ def entropy(text):
19
+ freq = Counter(text)
20
+ n = len(text)
21
+ return -sum((c/n) * math.log2(c/n) for c in freq.values())
22
+
23
+
24
+ def common_bigram_ratio(text):
25
+ if len(text) < 4:
26
+ return 1
27
+
28
+ total = 0
29
+ common = 0
30
+
31
+ for i in range(len(text) - 1):
32
+ bg = text[i:i+2]
33
+ total += 1
34
+ if bg in COMMON_BIGRAMS:
35
+ common += 1
36
+
37
+ return common / total if total else 1
38
+
39
+
40
+ def max_consonant_run(text):
41
+ max_count = 0
42
+ count = 0
43
+
44
+ for c in text:
45
+ if c not in VOWELS and c.isalpha():
46
+ count += 1
47
+ max_count = max(max_count, count)
48
+ else:
49
+ count = 0
50
+
51
+ return max_count
52
+
53
+
54
+ # MAIN SCORER
55
+ def score_input(text: str) -> float:
56
+ if not text:
57
+ return 0
58
+
59
+ text = text.lower()
60
+ score = 100
61
+ words = text.split()
62
+
63
+ # structural redundancy
64
+ if len(words) >= 4:
65
+ ratio = len(set(words)) / len(words)
66
+ if ratio < 0.5:
67
+ score -= 20
68
+
69
+ # info density
70
+ if words:
71
+ avg_len = sum(len(w) for w in words) / len(words)
72
+ if avg_len < 3:
73
+ score -= 15
74
+
75
+ # entropy (more aggressive)
76
+ if len(text) > 12:
77
+ e = entropy(text)
78
+ if e > 4.2:
79
+ score -= 35
80
+
81
+ # repeated patterns
82
+ if re.search(r'(.{2,3})\1{2,}', text):
83
+ score -= 40
84
+
85
+ # consonant runs (key)
86
+ if max_consonant_run(text) >= 5:
87
+ score -= 40
88
+
89
+ # low vowel ratio per word
90
+ for w in words:
91
+ if len(w) >= 6:
92
+ vowels = sum(1 for c in w if c in VOWELS)
93
+ if vowels / len(w) < 0.25:
94
+ score -= 25
95
+ break
96
+
97
+ # suspicious single word (very key)
98
+ if len(words) == 1 and len(text) > 8:
99
+ diversity = len(set(text)) / len(text)
100
+ e = entropy(text)
101
+
102
+ if diversity > 0.6 and e > 3.5:
103
+ score -= 60
104
+
105
+ # unlikely bigrams
106
+ if len(text) > 10:
107
+ ratio_bg = common_bigram_ratio(text)
108
+ if ratio_bg < 0.15:
109
+ score -= 50
110
+
111
+ # bonus: useful density
112
+ if any(c.isdigit() for c in text):
113
+ score += 5
114
+
115
+ if 2 <= len(words) <= 6:
116
+ score += 5
117
+
118
+ # long redundancy
119
+ if len(text) > 150 and len(words) > 10:
120
+ ratio = len(set(words)) / len(words)
121
+ if ratio < 0.6:
122
+ score -= 15
123
+
124
+ # FINAL CLAMP
125
+ return max(min(score, 100), 0)
@@ -0,0 +1,177 @@
1
+ Metadata-Version: 2.4
2
+ Name: input_cleaner
3
+ Version: 0.1.0
4
+ Summary: Text cleaning, scoring and compression pipeline
5
+ Author: marlon(MARLM8)
6
+ Requires-Python: >=3.8
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: tiktoken
10
+ Dynamic: license-file
11
+
12
+ # Input Cleaner
13
+
14
+ Lightweight text preprocessing pipeline for cleaning, scoring and compressing user input.
15
+
16
+ Designed for LLM optimization, noise reduction and token efficiency.
17
+
18
+ ---
19
+
20
+ ## Features
21
+
22
+ * Text normalization and cleanup
23
+ * Noise detection using heuristic scoring
24
+ * Input compression (removes low-value words)
25
+ * Quality scoring system (0–100)
26
+ * Smart truncation for long inputs
27
+ * Token reduction tracking using `tiktoken`
28
+
29
+ ---
30
+
31
+ ## Installation
32
+
33
+ ```bash
34
+ pip install input_cleaner
35
+ ```
36
+
37
+ Or local development:
38
+
39
+ ```bash
40
+ pip install -e .
41
+ ```
42
+
43
+ ---
44
+
45
+ ## Quick Start
46
+
47
+ ```python
48
+ from input_cleaner import process
49
+
50
+ result = process("hello hello I really really need help with this")
51
+
52
+ print(result)
53
+ ```
54
+
55
+ ---
56
+
57
+ ## Example Output
58
+
59
+ ```json
60
+ {
61
+ "original": "hello hello I really really need help with this",
62
+ "cleaned": "hello i really need help with this",
63
+ "compressed": "need help this",
64
+ "score": 78,
65
+ "status": "compress",
66
+ "output": "need help this",
67
+ "token_reduction": 0.32
68
+ }
69
+ ```
70
+
71
+ ---
72
+
73
+ ## Pipeline Overview
74
+
75
+ The system processes text in 4 stages:
76
+
77
+ 1. **Cleaning**
78
+
79
+ * Normalize text (lowercase, symbols)
80
+ * Remove duplicates and noise patterns
81
+
82
+ 2. **Compression**
83
+
84
+ * Remove weak or redundant words
85
+ * Preserve important tokens
86
+
87
+ 3. **Scoring**
88
+
89
+ * Evaluate text quality using:
90
+
91
+ * entropy
92
+ * character diversity
93
+ * vowel ratio
94
+ * pattern detection
95
+
96
+ 4. **Decision**
97
+
98
+ * Block low-quality input
99
+ * Compress medium-quality input
100
+ * Accept high-quality input
101
+ * Truncate long input if needed
102
+
103
+ ---
104
+
105
+ ## Status Values
106
+
107
+ | Status | Meaning |
108
+ | ------------------- | --------------------- |
109
+ | `blocked_empty` | Empty input |
110
+ | `blocked_low_score` | Low quality score |
111
+ | `blocked_noise` | Likely random string |
112
+ | `blocked_low_info` | Too short / low value |
113
+ | `accepted_short` | Short but valid |
114
+ | `compress` | Needs compression |
115
+ | `truncated_smart` | Long input trimmed |
116
+ | `accepted` | Fully valid |
117
+
118
+ ---
119
+
120
+ ## Use Cases
121
+
122
+ * Preprocessing user input for LLMs
123
+ * Reducing token costs
124
+ * Filtering spam or noisy text
125
+ * Improving prompt quality
126
+ * Input validation layer for APIs
127
+
128
+ ---
129
+
130
+ ## Requirements
131
+
132
+ * Python 3.8+
133
+ * `tiktoken`
134
+
135
+ ---
136
+
137
+ ## Project Structure
138
+
139
+ ```bash
140
+ text_pipeline/
141
+ ├── cleaner.py
142
+ ├── compressor.py
143
+ ├── scorer.py
144
+ ├── decision.py
145
+ └── pipeline.py
146
+ ```
147
+
148
+ ---
149
+
150
+ ## Design Goals
151
+
152
+ * Fast (pure Python, no heavy dependencies)
153
+ * Deterministic (no randomness)
154
+ * Modular (each step isolated)
155
+ * Easy to extend
156
+
157
+ ---
158
+
159
+ ## Roadmap
160
+
161
+ * Add trigram scoring
162
+ * Language detection
163
+ * Configurable thresholds
164
+ * FastAPI wrapper
165
+ * Streaming support
166
+
167
+ ---
168
+
169
+ ## License
170
+
171
+ Apache-2.0
172
+
173
+ ---
174
+
175
+ ## Author
176
+
177
+ marlon (MARLM8)
@@ -0,0 +1,15 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ input_cleaner/__init__.py
5
+ input_cleaner/cleaner.py
6
+ input_cleaner/compressor.py
7
+ input_cleaner/decision.py
8
+ input_cleaner/pipeline.py
9
+ input_cleaner/scorer.py
10
+ input_cleaner.egg-info/PKG-INFO
11
+ input_cleaner.egg-info/SOURCES.txt
12
+ input_cleaner.egg-info/dependency_links.txt
13
+ input_cleaner.egg-info/requires.txt
14
+ input_cleaner.egg-info/top_level.txt
15
+ tests/test.py
@@ -0,0 +1 @@
1
+ tiktoken
@@ -0,0 +1 @@
1
+ input_cleaner
@@ -0,0 +1,17 @@
1
+ [project]
2
+ name = "input_cleaner"
3
+ version = "0.1.0"
4
+ description = "Text cleaning, scoring and compression pipeline"
5
+ authors = [
6
+ {name = "marlon(MARLM8)"}
7
+ ]
8
+ readme = "README.md"
9
+ requires-python = ">=3.8"
10
+
11
+ dependencies = [
12
+ "tiktoken"
13
+ ]
14
+
15
+ [build-system]
16
+ requires = ["setuptools", "wheel"]
17
+ build-backend = "setuptools.build_meta"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,8 @@
1
+ from input_cleaner import process
2
+
3
+ while True:
4
+ user_input = input(" enter your text: ")
5
+ if user_input.lower() == "exit":
6
+ break
7
+ result = process(user_input)
8
+ print(result)