-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelefon2.py
More file actions
executable file
·52 lines (46 loc) · 1.75 KB
/
telefon2.py
File metadata and controls
executable file
·52 lines (46 loc) · 1.75 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python3
# $Id$
#
# Usage: ./telefon.py $HOME/Wissen/telefon.txt <pattern>
# This code comes from ChatGPT asking the question in
# https://github.com/ksulrich/knowledgeDB
import re
import sys
def read_know_file(file_path):
with open(file_path, 'r') as f:
contents = f.read()
blocks = re.split('-{3,}\n', contents)
return [block.strip() for block in blocks if block.strip()]
def search_block(query, block):
# Split the query into individual words/expressions
#search_terms = query.split()
search_terms = query
# Check if all search terms are found in the block
if all(re.search(term, block, re.IGNORECASE) for term in search_terms):
return block
def search_know_file(query, file_path):
print("query=", query, " file_path=", file_path)
blocks = read_know_file(file_path)
matching_blocks = [search_block(query, block) for block in blocks]
return [block for block in matching_blocks if block]
if __name__ == '__main__':
# we need at least the file and one pattern
print(sys.argv)
if len(sys.argv) <= 2:
print("""
Usage: telefon2.py <file> <pattern_1> <pattern_2> ... <pattern_n>
where <pattern_i> is a regular expression like "ul.*"
The patterns are ANDed, so you can search for that:
./telefon2.py telefon.txt klaus "ul.*"
and you are searching for klaus (case does not matter) AND
any word starting with ul
""")
sys.exit(1)
# The file is the first argument
# All the next arguments are the search patterns
query = sys.argv[2:]
matching_blocks = search_know_file(query, sys.argv[1])
for block in matching_blocks:
print('-' * 80)
print(block)
print('-' * 80)