Search code examples
clips

CLIPS How to divide text into word?


I need to write a method of a function that does the following:

  • Divides the text into words;

  • Prints words that are different from the first word;

  • And before that converts each word according to the following rule:

    If the word is odd, then removes its middle letter.

The result is displayed on the screen and in a text file.


Solution

  • Here's a function that will give you the list of different words:

    CLIPS> 
    (deffunction munge (?text)
       (bind ?w1 (explode$ ?text))
       (bind ?w2 (create$))
       (progn$ (?w ?w1)
          (bind ?len (str-length ?w))
          (if (oddp ?len)
             then
               (bind ?nw (str-cat (sub-string 1 (div ?len 2) ?w)
                                   (sub-string (+ (div ?len 2) 2) ?len ?w)))
               (bind ?w2 (create$ ?w2 ?nw))
             else
               (bind ?w2 (create$ ?w2 (str-cat ?w)))))
        (bind ?first (nth$ 1 ?w2))
        (bind ?rest (rest$ ?w2))
        (bind ?w3 (create$))
        (progn$ (?w ?w2)
          (if (neq ?w ?first)
             then
             (bind ?w3 (create$ ?w3 ?w))))
        ?w3)
    CLIPS> (munge "red green blue purple brown green white red black blue")
    ("gren" "blue" "purple" "brwn" "gren" "whte" "blck" "blue")
    CLIPS>