The process I want to create is the following: The user sees a text about fruits and wants to know if certain fruits are present in the text. Therefore a search-box is provided in which he enters: (apple or pear) and banana
(or any other random search-pattern)
How do i program such a thing, and especially, how do i create a logical expression programmatically?
I have asked an AI but it gave me an illogical non-working sample.
I want to do the following:
Any ideas?
In the mean time i have come up with something like this:
(The boolsq stands for the search-terms that may or may not have been found, the operatorsq is also to be parsed from the search-string. Using operators and (a) or (o) and not (n). Not yet supported brackets and phrases, but it's a start..)
import strutils
var
boolsq: seq[bool] = @[true, false, true, false]
composbo: bool
# now using operators
var operatorsq: seq[char] = @['a', 'n', 'a', 'o']
for it, valbo in boolsq:
if it < operatorsq.len:
if it == 0:
case operatorsq[it]
of 'n':
composbo = not valbo
else:
composbo = valbo
else:
case operatorsq[it]
of 'a':
composbo = composbo and valbo
of 'o':
composbo = composbo or valbo
of 'n':
composbo = composbo and not valbo
else:
echo "invalid operator.."
else:
echo "cannot be bigger.."
echo it
echo composbo
OK thanks I will look into those libs, time permitting, for further enhancements.. As a last additon I will give the whole function I made.
proc filterIsMatching(tekst, searchtermst: string): string =
#[
The searchterms form the filter.
result types:
- If filter matches the text: yes
- if filter doesnt match: no
- if filter is invalid: error-message
currently supported operators:
- a = and, o = or, n = (and) not
- using no operators: will be treated as 'and'
ADAP FUT:
-add phrases
-enable parentheses
-enable alternative operators (more common ones)
]#
var
termsq, splitallsq, operatorsq: seq[string]
first_operator_missingbo: bool = true
abortbo: bool = false
foundsq: seq[bool] = @[]
composbo: bool
splitallsq = searchtermst.split()
for it, elemst in splitallsq:
if elemst.len > 1:
termsq.add(elemst)
elif elemst.len == 1:
operatorsq.add(elemst)
if it == 0:
first_operator_missingbo = false
if termsq.len == 0:
result = "No search-terms entered.."
abortbo = true
elif termsq.len > 0:
if termsq.len == operatorsq.len + 1:
if first_operator_missingbo:
operatorsq.insert("", 0)
else:
result = "Invalid search-terms; please re-enter"
abortbo = true
if operatorsq.len == 0:
# suppose and-ops are meant and add them
for thing in termsq:
operatorsq.add("a")
operatorsq.add("a")
if not abortbo:
#var operatorsq: seq[char] = @['a', 'n', 'a', 'o']
# test if the search-terms are present in the text
for termst in termsq:
if tekst.contains(termst):
foundsq.add(true)
else:
foundsq.add(false)
# create the logical expression based on the found results
for it, valbo in foundsq:
if it < operatorsq.len:
if it == 0:
case operatorsq[it]
of "n":
composbo = not valbo
else:
composbo = valbo
else:
case operatorsq[it]
of "a":
composbo = composbo and valbo
of "o":
composbo = composbo or valbo
of "n":
composbo = composbo and not valbo
else:
echo "invalid operator.."
else:
echo "cannot be bigger.."
#echo it
echo composbo
if composbo:
result = "yes"
else:
result = "no"