Searches plain-text database of TCM herbal formulas and displays the matches, formulas with more matches are displayed first. Written in V.
import arrays
import io
import os
import readline
import regex
fn main() {
mut raw_array := []string{len: 10000, init: ''}
mut formula_number := 0
mut formula_array := []string{len: 1000, init: ''}
mut herbs_array := []string{len: 1000, init: ''}
mut notes_array := []string{len: 1000, init: ''}
flname := 'formula_list.txt'
file := os.open(flname)!
mut r := io.new_buffered_reader(reader: file)
mut count := 0
for {
count += 1
line := r.read_line() or { break }
raw_array[count] = line
}
formula_array, herbs_array, notes_array, formula_number = build_arrays(mut raw_array)
println('\n\n\n total formulas scanned: ${formula_number} \n\n\n')
get_query(formula_array, herbs_array, notes_array, formula_number)!
}
fn build_arrays(mut raw_array_ba []string) ([]string, []string, []string, int) {
mut index := 0
mut formula_number := 0
mut formula_line := 0
mut temp_note := ''
mut formula_array := []string{len: 1000, init: ''}
mut herbs_array := []string{len: 1000, init: ''}
mut notes_array := []string{len: 1000, init: ''}
for index = 0; index < raw_array_ba.len; index++ {
if raw_array_ba[index] == '*****' {
break
} else if raw_array_ba[index] == '=====' {
formula_number += 1
formula_line = 0
continue
}
formula_line += 1
if formula_line == 1 {
formula_array[formula_number] = raw_array_ba[index]
} else if formula_line == 2 {
herbs_array[formula_number] = raw_array_ba[index]
} else if formula_line > 2 {
temp_note = raw_array_ba[index]
temp_note += ' \n'
raw_array_ba[index] = temp_note
notes_array[formula_number] += raw_array_ba[index]
}
}
return formula_array, herbs_array, notes_array, formula_number
}
fn get_query(formula_array_gq []string, herbs_array_gq []string, notes_array_gq []string, formula_number_gq int)! {
mut notes_lined_array := []string{len: 1000, init: ''}
mut score_array := []int{len: 1000, init: 0}
mut terms_array := []string{len: 10, init: ""}
mut results_txt := "\n"
arrays.copy(mut notes_lined_array, notes_array_gq)
println("\n\n")
mut r := readline.Readline{}
mut query := r.read_line(' Enter search terms (comma separated), or x to exit: ')!
if query == 'x' {
exit(0)
}
mut term := r'(, )'
mut re := regex.regex_opt(term)!
query = re.replace_simple(query, ",")
terms_array = query.split(",")
println("\n Search terms in terms_array are: ${terms_array}")
// count occurances:
for i in 0 .. formula_number_gq {
for one_term in terms_array {
if notes_lined_array[i].count(one_term) > 0 {
score_array[i]++
query = '(${one_term})'
re = regex.regex_opt(query)!
notes_lined_array[i] = re.replace(notes_lined_array[i], r'____[\0]____')
}
}
}
collated := arrays.map_of_counts(score_array)
for x := collated.len - 1; x >= 1; x-- {
results_txt += " Formulas where ${x} matched: ${collated[x]} \n"
}
mut score := arrays.max(score_array)!
loopmax := score + 1
for _ in 0 .. loopmax {
for j in 0 .. formula_number_gq {
if score_array[j] == score {
results_txt += "\n:::::::::::::::::::: matched ${score} ::::::::::::::::::::::::::::::::::::::: \n"
results_txt += formula_array_gq[j]
results_txt += "\n"
results_txt += herbs_array_gq[j]
results_txt += "\n"
results_txt += notes_lined_array[j]
}
}
score = score - 1
if score == 0 {break}
}
os.write_file("/tmp/formula_results.txt", results_txt)!
os.execute("lite-xl /tmp/formula_results.txt")
get_query(formula_array_gq, herbs_array_gq, notes_array_gq, formula_number_gq)!
}
© Brett Mahar
site nginx on openbsd