From: Isotope
Subject: converting a string to a list
Date: 
Message-ID: <a48l13$h5h$1@yarrow.open.ac.uk>
Can anyone help with the following?

I want to represent a string, taken from user input,  as a list - exactly as
it is - even if includes only one bracket, for example:

"this is a ( test"  to '(this is a ( test)

What is the best way of dealing with "stray" brackets?

I would really appreciate any comments.....

Annika

From: Kent M Pitman
Subject: Re: converting a string to a list
Date: 
Message-ID: <sfwy9hzew8g.fsf@shell01.TheWorld.com>
"Isotope" <·········@open.ac.uk> writes:

> Can anyone help with the following?
> 
> I want to represent a string, taken from user input,  as a list - exactly as
> it is - even if includes only one bracket, for example:
> 
> "this is a ( test"  to '(this is a ( test)
> 
> What is the best way of dealing with "stray" brackets?
> 
> I would really appreciate any comments.....

Show what you want the result to be and this question will be easier to
answer.

Also, please say if this is homework.  (If it is, you should show your best
attempt at a partial solution.)
From: Isotope
Subject: Re: converting a string to a list
Date: 
Message-ID: <a494rs$c3t$1@knossos.btinternet.com>
No... I can assure you this isn't homework, but I have plenty of partial
solutions all the same - just none that work!

The reason for asking is that I am using LISP_mod to generate a web-page
into which a user can type any text that they want (perhaps, unwittingly
missing off brackets and the like). I am storing the result as a string in
the slot of a class - which is also written into a text-file for storage and
retrieval. I need to be able to find out if certain words are members of
that slot, so I would like to turn the string into a list.

For an example (please remember I am now at home and my code is at work, so
I aplogise if brackets are out of place)

str = "this is a ( test"

(setq temp-list nil)
(with-input-from-string (str (annotation resource))
 (do
    ((word (read str nil) (read str nil))
    ((not str))
    (push str temp-list)
))

Of course, it cannot read past end of stream once it hits the left
bracket....

The only solution I have so far is to create a character-bag and remove
certain characters - but I would prefer not to have to do this.

Thanks for any help,

Annika


Kent M Pitman <······@world.std.com> wrote in message
····················@shell01.TheWorld.com...
> "Isotope" <·········@open.ac.uk> writes:
>
> > Can anyone help with the following?
> >
> > I want to represent a string, taken from user input,  as a list -
exactly as
> > it is - even if includes only one bracket, for example:
> >
> > "this is a ( test"  to '(this is a ( test)
> >
> > What is the best way of dealing with "stray" brackets?
> >
> > I would really appreciate any comments.....
>
> Show what you want the result to be and this question will be easier to
> answer.
>
> Also, please say if this is homework.  (If it is, you should show your
best
> attempt at a partial solution.)
From: Kyongho Min
Subject: Re: converting a string to a list
Date: 
Message-ID: <bd09e78f.0202111731.f161b3d@posting.google.com>
Let's get back to the first message.

The input is "who is jill" and the output is (who is jill).
It means that a list of symbols is required from a string.

You can get words from the string by using (elt) function.
The word boundary is #\Space or you can program your own function
(word-boundp char) if the char is #\Space, #\( etc.

If you want to extract legal lexical items only, you can use the lexicon
of your system to check out the spelling of the word extracted.

At this stage, you can get a string of a word. What you want is 
the list of symbols. Try (read-from-string) to convert a string into
a symbol.

If you are using Franz ACL60, then you can combine functions such as
(mapcar #'read-from-string (delimited-string-to-list string #\space)).
In this case, if your input string is delimited by #\space only.
Otherwise, you may get error-message.

cheers

Kyongho
From: Kent M Pitman
Subject: Re: converting a string to a list
Date: 
Message-ID: <sfw3d07v4lu.fsf@shell01.TheWorld.com>
···········@aut.ac.nz (Kyongho Min) writes:

> Let's get back to the first message.
> 
> The input is "who is jill" and the output is (who is jill).
> It means that a list of symbols is required from a string.
> 
> You can get words from the string by using (elt) function.
> The word boundary is #\Space or you can program your own function
> (word-boundp char) if the char is #\Space, #\( etc.
> 
> If you want to extract legal lexical items only, you can use the lexicon
> of your system to check out the spelling of the word extracted.
> 
> At this stage, you can get a string of a word. What you want is 
> the list of symbols. Try (read-from-string) to convert a string into
> a symbol.

READ-FROM-STRING reads something in Lisp syntax.

I'll just go out on a limb here and be less laid back and more prescriptive
about this:

Never use READ or READ-FROM-STRING unless (a) the person who entered 
the syntax was familiar with lisp syntax and all of the data type mappings
that come from its varied notations or (b) you have written something to
check the input beforehand yourself to make sure that there are no syntaxes
that you don't expect.

I see no code here to check that you have symbol syntax, so describing
READ-FROM-STRING as a translator from string to symbol syntax is just
not appropriate.

> If you are using Franz ACL60, then you can combine functions such as
> (mapcar #'read-from-string (delimited-string-to-list string #\space)).
> In this case, if your input string is delimited by #\space only.
> Otherwise, you may get error-message.

What will it do if the string is:

  John said, "Hello, Sam, do you have any #2 pencils for sale?"

The characters comma (,) and sharpsign (#) and doublequote (") have special
Lisp syntax that READ-FROM-STRING will not be happy running across if what
it is looking for is symbol syntax.

Further, characters like question mark (?), exclamation mark (!), and 
period (.) have special significance in human language that don't
apply in Lisp.
From: Barry Margolin
Subject: Re: converting a string to a list
Date: 
Message-ID: <IqX98.25$g01.151292@burlma1-snr2>
In article <············@knossos.btinternet.com>,
Isotope <·········@open.ac.uk> wrote:
>The only solution I have so far is to create a character-bag and remove
>certain characters - but I would prefer not to have to do this.

What about my suggestion to modify the readtable so that parentheses (and
all other special characters, I presume) are treated as constituents?

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Isotope
Subject: Re: converting a string to a list
Date: 
Message-ID: <a49hmm$67l$1@paris.btinternet.com>
Sorry... I only just saw this through a different news-reader. I'm not sure
how to do this off the top of my head. I'll have look into it tomorrow when
I have access to my books. One question occurs to me, though.... if I do
this will member work the same if all special characters are treated as
consituents? Please forgive my ignorace.

Thanks,

Annika


Barry Margolin <······@genuity.net> wrote in message
························@burlma1-snr2...
> In article <············@knossos.btinternet.com>,
> Isotope <·········@open.ac.uk> wrote:
> >The only solution I have so far is to create a character-bag and remove
> >certain characters - but I would prefer not to have to do this.
>
> What about my suggestion to modify the readtable so that parentheses (and
> all other special characters, I presume) are treated as constituents?
>
> --
> Barry Margolin, ······@genuity.net
> Genuity, Woburn, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to
newsgroups.
> Please DON'T copy followups to me -- I'll assume it wasn't posted to the
group.
From: Barry Margolin
Subject: Re: converting a string to a list
Date: 
Message-ID: <X%X98.31$g01.153890@burlma1-snr2>
In article <············@paris.btinternet.com>,
Isotope <·········@open.ac.uk> wrote:
>Sorry... I only just saw this through a different news-reader. I'm not sure
>how to do this off the top of my head. I'll have look into it tomorrow when
>I have access to my books. One question occurs to me, though.... if I do
>this will member work the same if all special characters are treated as
>consituents? Please forgive my ignorace.

The point of my suggestion is that all characters (except space) would be
treated just like alphanumerics, so everything will be either a symbol or
number.  MEMBER just compares them using EQL, which works find on symbols
and numbers.

One problem you may run into is if the punctuation characters aren't
separated from words by whitespace.  E.g. if the string contains

"quick (brown) fox"

the second element of the resulting list will be the symbol |(BROWN)|
rather than BROWN.

If you want to ignore punctuation, why don't you just remove all of it
first:

(defun alphanumeric-or-white-p (c)
  (or (alphanumericp c)
      (member c '(#\space #\tab #\newline))))

(with-input-from-string (stream (remove-if-not #'alphanumeric-or-white-p
                                               string))
  ...)

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Isotope
Subject: Re: converting a string to a list
Date: 
Message-ID: <a4arj8$df6$1@yarrow.open.ac.uk>
"Barry Margolin" <······@genuity.net> wrote in message
························@burlma1-snr2...
> In article <············@paris.btinternet.com>,
> Isotope <·········@open.ac.uk> wrote:
> >Sorry... I only just saw this through a different news-reader. I'm not
sure
> >how to do this off the top of my head. I'll have look into it tomorrow
when
> >I have access to my books. One question occurs to me, though.... if I do
> >this will member work the same if all special characters are treated as
> >consituents? Please forgive my ignorace.
>
> The point of my suggestion is that all characters (except space) would be
> treated just like alphanumerics, so everything will be either a symbol or
> number.  MEMBER just compares them using EQL, which works find on symbols
> and numbers.
>
> One problem you may run into is if the punctuation characters aren't
> separated from words by whitespace.  E.g. if the string contains
>
> "quick (brown) fox"
>
> the second element of the resulting list will be the symbol |(BROWN)|
> rather than BROWN.
>
> If you want to ignore punctuation, why don't you just remove all of it
> first:
>
> (defun alphanumeric-or-white-p (c)
>   (or (alphanumericp c)
>       (member c '(#\space #\tab #\newline))))
>
> (with-input-from-string (stream (remove-if-not #'alphanumeric-or-white-p
>                                                string))
>   ...)
>

Thanks for your comments

The basis is of the problem is that I want to be able to match punctuation,
as well as words, rather than just ignoring it. But, I am beginning to think
that this may not be such a good goal.

Annika



> --
> Barry Margolin, ······@genuity.net
> Genuity, Woburn, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to
newsgroups.
> Please DON'T copy followups to me -- I'll assume it wasn't posted to the
group.
From: lin8080
Subject: Re: converting a string to a list
Date: 
Message-ID: <3C6852F8.52C0B209@freenet.de>
Isotope schrieb:

> No... I can assure you this isn't homework, but I have plenty of partial
> solutions all the same - just none that work!

maybe it is not right, but is there no try and catch like in java ? when
you only have problems with ")" try to catch that error and insert the
asci value (or something like that) instead of "(".

stefan


catch n. an exit point which is established by a catch form within the
dynamic scope of its body, which is named by a catch tag, and to which
control and values may be thrown. 

catch tag n. an object which names an active catch. (If more than one
catch is active with the same catch tag, it is only possible to throw to
the innermost such catch because the outer one is shadowed[2].) 

throw causes a non-local control transfer to a catch whose tag is eq to
tag. 

 (catch 'a
   (catch 'b
     (unwind-protect (throw 'a 1)
       (throw 'b 2))))
From: Isotope
Subject: Re: converting a string to a list
Date: 
Message-ID: <a4arn7$dfm$1@yarrow.open.ac.uk>
Yes, it would be fairly simple to use javascript to control the input. This
is probably what I will end up doing to avoid people submitting text where
there is one bracket, for example. However, I am also trying to expand my
LISP knowledge, which is why I'm trying to solve the problem in LISP, too.

Annika

"lin8080" <·······@freenet.de> wrote in message
······················@freenet.de...
> Isotope schrieb:
>
> > No... I can assure you this isn't homework, but I have plenty of partial
> > solutions all the same - just none that work!
>
> maybe it is not right, but is there no try and catch like in java ? when
> you only have problems with ")" try to catch that error and insert the
> asci value (or something like that) instead of "(".
>
> stefan
>
>
> catch n. an exit point which is established by a catch form within the
> dynamic scope of its body, which is named by a catch tag, and to which
> control and values may be thrown.
>
> catch tag n. an object which names an active catch. (If more than one
> catch is active with the same catch tag, it is only possible to throw to
> the innermost such catch because the outer one is shadowed[2].)
>
> throw causes a non-local control transfer to a catch whose tag is eq to
> tag.
>
>  (catch 'a
>    (catch 'b
>      (unwind-protect (throw 'a 1)
>        (throw 'b 2))))
>
>
From: Christopher C. Stacy
Subject: Re: converting a string to a list
Date: 
Message-ID: <uaduf4frf.fsf@theworld.com>
>>>>> On Mon, 11 Feb 2002 19:08:44 +0000 (UTC), Isotope  ("Isotope") writes:
 Isotope> The reason for asking is that I am using LISP_mod to
 Isotope> generate a web-page into which a user can type any text that
 Isotope> they want (perhaps, unwittingly missing off brackets and the
 Isotope> like). I am storing the result as a string in the slot of a
 Isotope> class - which is also written into a text-file for storage
 Isotope> and retrieval.
 Isotope> I need to be able to find out if certain words are members of
 Isotope> that slot, so I would like to turn the string into a list.

Why not just operate directly on the input text as a string data type.
You would need to be able to step through the words in the input string
string, and look up each word to see if it's in the list of words that
you are interested in.

You would have a simple loop to find the indices representing the next
word in the string, and then use a function something like this:

 (defun word-member (string start end list)
              (let ((length (- end start)))
                (dolist (word list)
                  (when (and (= (length word) length)
                             (string-equal string word :start1 start :end1 end))
                    (return word)))))

Example:

  (word-member "the quick brown fox" 4 9 
	'("lazy" "silly" "quick" "pretty"))

One nice thing about this is that is does not cons at all.

cheers,
Chris
From: Christopher C. Stacy
Subject: Re: converting a string to a list
Date: 
Message-ID: <u4rkn4f5p.fsf@theworld.com>
 >  (defun word-member (string start end list)
 >               (let ((length (- end start)))
 >                 (dolist (word list)
 >                   (when (and (= (length word) length)
 >                              (string-equal string word :start1 start :end1 end))
 >                     (return word)))))

STRING-EQUAL doubtless tests that the strings are the same length,
so you don't need to check that yourself.  (When I wrote that, I was
thinking about using a function other than STRING-EQUAL...)
From: Vladimir Zolotykh
Subject: Re: converting a string to a list
Date: 
Message-ID: <3C67F478.79C103AF@eurocom.od.ua>
It seems as very strong and artificial restriction. Are you sure
you really need exactly that ? If you'd tried to describe your
intention in more details it would be easier to suggest something.

> 
> Can anyone help with the following?
> 
> I want to represent a string, taken from user input,  as a list - exactly as
> it is - even if includes only one bracket, for example:
> 
> "this is a ( test"  to '(this is a ( test)
> 
> What is the best way of dealing with "stray" brackets?
> 
> I would really appreciate any comments.....
> 
> Annika

-- 
Vladimir Zolotykh
From: Barry Margolin
Subject: Re: converting a string to a list
Date: 
Message-ID: <osU98.12$g01.133490@burlma1-snr2>
In article <············@yarrow.open.ac.uk>,
Isotope <·········@open.ac.uk> wrote:
>Can anyone help with the following?
>
>I want to represent a string, taken from user input,  as a list - exactly as
>it is - even if includes only one bracket, for example:
>
>"this is a ( test"  to '(this is a ( test)
>
>What is the best way of dealing with "stray" brackets?

You could change the readtable to give them the same syntax as
alphanumerics.  Then you'll get the list that prints as:

 (THIS IS A |(| TEST)

-- 
Barry Margolin, ······@genuity.net
Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.