Jess multislot questions - jess

This is for a homework assignment, but I am having a really hard time finding good jess information on the web. I'm trying to use a multislot to to solve a problem, but I can't find anything on how to match the different values. I have this:
(deftemplate patient (slot name)(multislot symptoms))
(deffacts init
(patient (name john) (symptoms very-high-fever cough)))
How can I match the left hand side for just a very-high-fever? This works if I know that the very-high-fever is the first symptom, but I can't be sure of that, so I need to be able to match if a very-high-fever is either symptom.
(defrule high-fever
(patient (name ?n)(symptoms very-high-fever ?))
=>
(printout t ?n " has a high fever." crlf))
I've tried various combinations of field constraints, but I can't seem to get it right and nothing online is giving me any clues.
Thanks.

Use a blank multifield before and after the item you want to match; they match zero or more items. So, something like
(patient (name ?n) (symptoms $? very-high-fever $?))
Will match any patient with the very-high-fever symptom in any position.

Related

How to clean data having spelling mistakes

I have data as below:
Carepnter
Carpentor
Labourer
Labor
Labour
Housewife
House Wife
housewife.
I want to clean data and rectify the spelling mistakes but not manually because its a huge data. Due to spelling mistakes these 50/60 occupations have become around 2000.
You would have to find strings that are close to the actual occupation, for instance carpenter.
Then you can try to find the closest n-matches to it.
Another question on here also dealt with finding similar strings (Python: find closest string (from a list) to another string) and the solutions from the answers for you could be either:
difflib.get_close_matches
Spelling corrector

i don´t know why this example of the jess rule manual doesn´t work

I am reading the jess manual, exactly in 6.12. The 'test' conditional element. I copied exactly the example for try running this code, but didn´t work at all.
NOTE: I have run another examples and work fine.
(deftemplate person (slot age))
(defrule example-8
(test (eq 4 (+ 2 2)))
=>
(printout t "2 + 2 is 4!" crlf))
My result is nothing.. don´t show me any message, don´t return any error message. I really want to understand it. Please help me on this.
Many rules — and this is one — require the “initial-fact” asserted by “reset” to operate. The manual explains in more detail, but just as a best practice, you generally need to call (reset) before asserting any facts and calling (run).

Rails quickly find record where has_many associations exactly match array of association ids

I've got a Product model which has_many OptionValue records, which describe color, size, etc.
Within my code, I need to query the Product model where the product.option_values.pluck(:id) array exactly matches an array of (e.g.) options = [1, 6, 4].
Running something like Product.includes(:option_values).where(option_values: { id: options_array }) returns all values that match at least one element of the options array, rather than all of them.
I've developed an inefficient way of getting the record I need, as follows:
Product.all.each { |v| return v if v.option_values.pluck(:id).sort == options_array.sort }
Obviously the above is way ott and I'm sure there's a simpler way to handle this, and I'm happy to use ActiveRecord or a straight SQL query (though I'm not too hot on the latter, so haven't come up with anything yet).
Any advice on the best way of achieving this greatly appreciated. Not sure I've explained this perfectly, so please comment if you've any questions.
Thanks in advance, Steve.
Clocked this in my old questions and threw together a quick and easy solution, so dropping it here for anyone else that might come this way:
product_options = product.option_values.pluck(:id)
unless options_array.length < product_options.length
(product_options & options_array).length == product_options.length
end
This:
checks the options_array is long enough to contain the necessary matches
looks for the elements in common across the two arrays ((product_options & options_array))
measures their length
checks this is the same length as the desired array product_options, meaning all the required options were found.
Alternatively, you can subtract one from the other and check there's nothing leftover (i.e. missing):
(product_options - options_array).empty?
Hope this helps someone at some point.

What type of programming is this?

I just finished taking an entry placement test for computer science as in college. I passed, but missed a bunch of questions in a specific category: variable assignment. I want to make sure I understand this before moving on.
It started out with easy things, like "set age equal to age"
int age = 18, pretty simple
But then, it had a question which I had no clue how to approach. It went something like...
"Determine if character c is is in alphabet and assign to a variable"
I could easily do that with a function, but the issue is, it gave me literally a line to write my entire answer (so about 50 characters max). Here is how the answer box looked:
My first thought was to do something like
in_alphabet = function(c) {
var alphabet = ["a", "b" ... "z"]
if(alphabet.indexOf(c) != -1)
return true;
}
But this solution has two issues:
How can I set the "c" value when the whole function is equal to in_alphabet?
I can't fit this into the small answer box. I am 99% sure they were looking for something else. Does anybody know what they were looking for? I can't think of a one line solution for this
Language doesn't matter (although a solution in java/c++ would be preferred). I would appreciate any guidance (doesn't have to be a solution, I just don't even know where to begin)
The question "Determine if character c is is in alphabet and assign to a variable" does not ask you to create a function (although in many languages this would be the best way to do this).
In R you could do something like:
inAlphabet <- c %in% letters
So you can certainly do it in one line in some real-world languages. Note that letters is a built-in list of characters.
It's a VBA solution and returns C in the variable:
LetterC = Mid("ABCDEFGHIJKLMNOPQRSTUVWXYZ", InStr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "C"), 1)
Is that what you're after?
Many languages have a data type that represents a single character, and they often can be compared using binary operators like < > <= >=, wherein the characters are compared numerically.
So something like this should suffice:
in_alphabet = c >= 'a' && c <= 'z'
And some languages already have built in methods to do things similar to this (e.g., Character.isLetter).
I copied straight from How to check if character is a letter in Javascript?
in_alphabet = c.length === 1 && c.match(/[a-z]/i)? str : ""
In Java, Character.isLetter(c)
In .NET, Char.IsLetter(c)
Perhaps you were being tested on knowledge of basic data types and some of the facilities they provide.

using structure definitions

i've been pulling my hair trying to figure this out. i'm suppose to design a function takes in a hstudent, i get another student with the same contents and with his age converted into dog years. any ideas on how to start? This exact question is going to be on an open book test I have on Friday.
(define-struct hsstudent (first-name last-name classroom overall-grade age))
(define hsstudent1 (make-hsstudent "Randy" "Smith" 'WH '-A 14))
(define hsstudent2 (make-hsstudent "Jon" "James" 'AH '-A 13 ))
(define hsstudent3 (make-hsstudent "Alex" "Manzi" 'LO '+A 16))
(define hsstudent4 (make-hsstudent "Kevin" "Matthews" 'WH '-A 14))
(define hsstudent5 (make-hsstudent "Issac" "Lewis" 'AH '-A 13 ))
(define hsstudent6 (make-hsstudent "Michael" "Gabbin" 'LO '+A 16))
Note: when you say design, that implies you're in a HTDP-based course.
You should already have been introduced to a very concrete set of steps to follow for designing functions that consume and produce structures. Have you looked at Designing with Structures and followed the steps there? If so, are you stuck at any particular step listed here?
The purpose of this methodology is to help pinpoint conceptual problems as soon as possible, rather than at coding time.
What you could do is use the struct selectors to get the values from the student and then give those to make-hsstudent in order to make a new student. For example
(hsstudent-age hsstudent1)
Will return 14. Generally (hsstudent-FIELDNAME student) will give the field value of FIELDNAME for student.