Skip to content

Commit f56e229

Browse files
committed
blog about AI and how I used it to solve a problem
1 parent 2cba8d6 commit f56e229

File tree

2 files changed

+125
-1
lines changed

2 files changed

+125
-1
lines changed

site/content/post/2006/bloggarse.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ tags = ["bloggar.se", "tag", "tags", "plugin"]
77

88
Nu har jag laggt till [bloggar.se][1]-tags i bloggen, det blev ett litet plugin som jag slängde ihop. Mycket roligare att skriva själv än att ta nått färdigt.
99

10-
Nu behöver jag bara skriva `:<!-- --!>: tag1, tag2` för att få en fin lista med länkar. Jag vet inte om det funkar i wysiwyg-editorn men då jag kör plain text så bryr jag mig inte
10+
Nu behöver jag bara skriva `:: tag1, tag2` för att få en fin lista med länkar. Jag vet inte om det funkar i wysiwyg-editorn men då jag kör plain text så bryr jag mig inte
1111

1212
:)
1313

site/content/post/2024/uses-for-ai.md

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
+++
2+
date = "2024-07-30T22:50:00+02:00"
3+
title = "Uses of AI"
4+
[taxonomies]
5+
tags = ["AI", "ChatGPT", "LLM", "GPT"]
6+
+++
7+
8+
Yeah, I agree that there is an AI hype going on. I agree that there is a lot of overlap with the cryptocoin hype. I also wish that people stopped to shoehorn in AI to everything. I wished we never called it AI because it's not intelligent. But here we are.
9+
10+
I find the technology fascinating, and I love to see real problems being solved with it. I love to play with AI of every kind and I write some software using it. It's hardly the magic unicorn dust that some companies love to tell you, but there are uses. Let me illustrate a really simple example with this blog post.
11+
12+
## Tag processing
13+
14+
About fifteen years ago I wrote a custom written [tag plugin](@/post/2006/bloggarse.md) for my blog. I wrote a small plugin that parsed the blog and generated inline tags that where picked up by a [blogging aggregator](http://www.knuff.se/) and [tagging service](http://www.bloggar.se) that was popular in Sweden back then.
15+
16+
The tags where useful data left there in old posts. They looked like below, and there was 186 different posts with them. Several hundreds of useful tags left unused for 15 years.
17+
18+
```
19+
:: tag1, tag2
20+
```
21+
22+
I have since then migrated between blogging platforms several times. It's not like it's hard for me to write a little script to parse this and import the tags. But there is a little friction, and this is not important. I have not bothered to do this... for years. I find AI useful for these cases. I know I can do it my self, I just can't bother. Please use some of the magical fairy dust to solve the problem for me.
23+
24+
### Let's ask ChatGPT
25+
26+
I opened a chat window and wrote this prompt. I usually finds it useful to be specific and give it samples for best results. It's also easier to just copy and paste an example instead of trying to explain what I need.
27+
28+
> Write me a small python program that will do this:
29+
>
30+
> Scan the folder structure in `site/content` and look for files containing the pattern `:: tag1, tag2` and so on. Basically a comma separated list with a :: at the start of the line.
31+
>
32+
> If the file contains this pattern, update the front matter. It's a code written between two +++ like the example below:
33+
> ```
34+
> +++
35+
> date = "2007-11-01T11:31:52+00:00"
36+
> title = "Nya Gmail"
37+
> +++
38+
> ```
39+
> I like you to insert the found tags in that like this:
40+
> ```
41+
> +++
42+
> date = "2007-11-01T11:31:52+00:00"
43+
> title = "Nya Gmail"
44+
> tags = ["tag1", "tag2"]
45+
> +++
46+
> ```
47+
48+
This created a small python script for me, but I realized that I forgot to remove the old tag format so I added
49+
50+
> Also, please remove the line with the ::
51+
52+
It provided me with an updated script. I did quick sanity check and executed it and it did exactly what I had asked it to do. Of course, I'm only human so I realized that I had provided the incorrect syntax, so I updated the chat with:
53+
54+
> Oh, I forgot. Please add [taxonomies] above the tags section. And please make all tags lowercase
55+
56+
I also took the chance to make everything lowercase. And here is the final script:
57+
58+
```python
59+
import os
60+
import re
61+
62+
def update_front_matter(file_path, tags):
63+
with open(file_path, 'r') as file:
64+
content = file.read()
65+
66+
front_matter_match = re.search(r'\+\+\+\n(.*?)\n\+\+\+', content, re.DOTALL)
67+
if not front_matter_match:
68+
return
69+
70+
front_matter = front_matter_match.group(1)
71+
new_front_matter = f'{front_matter}\n[taxonomies]\ntags = {tags}\n'
72+
updated_content = content.replace(front_matter, new_front_matter)
73+
74+
updated_content = re.sub(r'^::.*$', '', updated_content, flags=re.MULTILINE)
75+
76+
with open(file_path, 'w') as file:
77+
file.write(updated_content)
78+
79+
def scan_and_update_tags(directory):
80+
tag_pattern = re.compile(r'^::\s*(.*)$', re.MULTILINE)
81+
82+
for root, _, files in os.walk(directory):
83+
for file in files:
84+
file_path = os.path.join(root, file)
85+
with open(file_path, 'r') as f:
86+
content = f.read()
87+
88+
tag_match = tag_pattern.search(content)
89+
if tag_match:
90+
tags = [tag.strip().lower() for tag in tag_match.group(1).split(',')]
91+
tags_str = str(tags).replace("'", '"')
92+
update_front_matter(file_path, tags_str)
93+
94+
scan_and_update_tags('site/content')
95+
```
96+
97+
I did a single change to the script, I removed a `\n`, a stylistic choice that I preferred. And I ended up with 186 of these changes:
98+
99+
```diff
100+
diff --git a/site/content/post/2006/akismet.md b/site/content/post/2006/akismet.md
101+
index 53b7ed7..00b2c20 100644
102+
--- a/site/content/post/2006/akismet.md
103+
+++ b/site/content/post/2006/akismet.md
104+
@@ -1,13 +1,15 @@
105+
+++
106+
date = "2006-06-14T22:41:05+00:00"
107+
title = "Akismet"
108+
+[taxonomies]
109+
+tags = ["akismet", "spam"]
110+
+++
111+
112+
> Akismet has protected your site from 88 spam comments.
113+
114+
Jag är riktigt glad att jag valde att installera [Akismet][1] innan stormen kom.
115+
116+
-:: Akismet, spam
117+
+
118+
119+
<small></small>
120+
```
121+
122+
## Did this save time?
123+
124+
Yes, the dialog about took be around 2 minutes. There are no way that I would be able to reproduce the code, or even a more hack-y version of the code in that time. But the most important aspect it that it did it. I have ignored this small chore for 15 years and now it was really easy and fun to fix it. So I did.

0 commit comments

Comments
 (0)