JSON
JSON is how programs send data to each other. It looks almost exactly like a Python dictionary. When you call an AI model's API, the reply is JSON.
import json
text = '{"name": "Riya", "marks": [78, 85], "passed": true}'
data = json.loads(text) # Text to Python
print(data["name"], data["marks"][1])
data["city"] = "Pune"
print(json.dumps(data)) # Python back to textOutput
Riya 85
{"name": "Riya", "marks": [78, 85], "passed": true, "city": "Pune"}Regular expressions
A regular expression describes a pattern. It's the standard tool for cleaning text before it goes into a model.
import re
text = "Call 98765 43210 or mail riya@example.com by 5 pm"
print(re.findall(r"\d+", text)) # Groups of digits
print(re.findall(r"\S+@\S+", text)) # Something like an email
print(re.sub(r"\d", "#", text)) # Hide the digitsOutput
['98765', '43210', '5'] ['riya@example.com'] Call ##### ##### or mail riya@example.com by # pm
| Pattern | Matches |
|---|---|
| \d | A digit |
| \w | A letter, digit or underscore |
| \s | A space |
| + | One or more of the previous |
| * | Zero or more of the previous |
| ^ and $ | Start and end of the text |
Finished reading?