* Thanks guys..my project and my many problems @ 2003-02-24 13:52 Paul Gregory 2003-02-24 15:12 ` Preben Randhol ` (2 more replies) 0 siblings, 3 replies; 25+ messages in thread From: Paul Gregory @ 2003-02-24 13:52 UTC (permalink / raw) Thank-you Larry and Preban for your kind help. I'm literally new to ADA and I'm finding it quite difficult if anybody can help I would be so grateful its untrue!!. I'm getting really stressed by this - I suppose when you were novices you were pulling your hair out too ;-) --------------------------------------------------------------------------------- My project is this You are invited to design and implement a translation program which will take an English sentence and translate it into French (and optionally translate from French to English). Your program will use a simple dictionary containing the following words: English French big grand black noir cat chat dog chien is est small petit the le white blanc Example 1 Do you wish to add words to the dictionary?: N Please enter a sentence: The cat is white. The French translation of your sentence is: Le chat est blanc. Example 2 Do you wish to add words to the dictionary?: N Please enter a sentence: The cat is dead. I cannot translate that sentence. Example 3 Do you wish to add words to the dictionary?: Y English word: horse Equivalent French word: cheval Do you wish to add more words?: N Please enter a sentence: Le cheval est grand. The English translation of your sentence is: The horse is big. ______________________________________________________________ My code so far is this.. -- -- Read the English and translate it into French -- -- Constant array. -- with Ada.Text_io; with Nice_IO; use Nice_IO; with String_tokenizer; use String_tokenizer; procedure translate6 is type English is (big, black, cat, dog, es, small, the, white); type French is (grand, noir, chat, chien, est, petit, le, blanc); English_to_French: constant array(English) of French:= ( big => grand, black => noir, cat => chat, dog => chien, es => est, small => petit, the => le, white => blanc); S: String(1..80); -- Input string Last: Integer; -- Last character of input string S2: string (1..2); First: Integer; Eng: English; my_token : String_tokenizer.Token; length : integer; begin loop Put ("Do you wish to add words to the dictionary? : "); Ada.Text_IO.Get_Line (S, last); Put("Please enter a sentence: "); Ada.Text_io.Get_Line(S, Last); exit when Last = 0; set_string(s(1..Last)); -- this sets up the tokenizer while has_more_tokens loop my_token := next_token; -- my_token has the next token in the string length := my_token.token_string_length; -- length has the length of this token string Eng := English'Value(my_token.token_string(1..length)); Put_Line ("The French translation of your sentence is: " & French'Image(English_to_French(Eng))); end loop; end loop; exception when Constraint_Error => Put_Line(S(1..Last) & " cannot be translated"); end translate6; ________________________________________________________________ My problems are the following (deadline Friday - I'm getting really worried and stressed as I'm reading but finding it hard to understand / get the information I want for these paticular circumstance... Problem 1 - the UPPER CASE problem as stated before (where about in the program would I insert the character.handling bit?) Problem 2 - When the program translates the tokens it does it as follows.. When the program translates the tokens it does it like this... Please enter a sentence: the cat dog The French translation is : LE The French translation is: CHAT The French translation is: CHIEN ----> instead of The French translation is: Le chat chien. Any ideas how I can get round this ? Problem 3 is that one of the words I have to translate is 'Is' but as this is a reserved word it causes an error message everytime I run it. Problem 4 Is that as you can see from the question I have to "add words to the dictionary" - I'm not overly sure how to do this and presume I'd have to write some sort of IF statement. Everytime I try and write one it raises and untold amount of errors. Basically if they say 'N' I want it to go straight to the translator bit but if they say 'Y' then they must add a English word, then add the French version of the word then if I type the added word in a sentennce it must be able to recognise it.... Nurse...pass me the pills !! Cheers, PG ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-24 13:52 Thanks guys..my project and my many problems Paul Gregory @ 2003-02-24 15:12 ` Preben Randhol 2003-02-24 15:38 ` Hyman Rosen 2003-02-25 17:31 ` Matthew Heaney 2 siblings, 0 replies; 25+ messages in thread From: Preben Randhol @ 2003-02-24 15:12 UTC (permalink / raw) Paul Gregory wrote: > Thank-you Larry and Preban for your kind help. e ;-) > type English is > (big, black, cat, dog, es, small, the, white); > > type French is (grand, noir, chat, chien, est, petit, le, blanc); > > English_to_French: constant array(English) of French:= > ( big => grand, > black => noir, > cat => chat, > dog => chien, > es => est, > small => petit, > the => le, > white => blanc); > Why do you use enumerates? Is it required? If you are supposed to be able to add words and possibly store them too, why don't you rather make something like: type Max_Word_Length : constant Natural := 80; type Word_Record is record english : String (1 .. Max_Word_Length); french : String (1 .. Max_Word_Length); end record; of course you have to build a linked list or an array and a way to search for the correct word. Another way could be to use arrays (you don't have to deal with the linked list problems) but have the design a bit more flexible so you can add more languages if you want: type Dictionary is record word_id : Natural := 0; word : String (1 .. Max_Word_Length); end record; type English_List is array (1 .. 1000) of Dictionary; type French_List is array (1 .. 1000) of Dictionary; then when you add you generate a unique word_id /= 0 for each word and you put this in the English list along with the English word and in the French list with the french word. So that when you look up a word you get the word_id and you use this to find the same word in the other language. Of course you don't have to do this if you know that word number 3 in the English list will correspond with word number 3 in the French. Please note I haven't checked my code so there might be errors in it. Hope you can get further on your project with this. -- Preben Randhol ---------------- http://www.pvv.org/~randhol/ -- "Violence is the last refuge of the incompetent", Isaac Asimov ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-24 13:52 Thanks guys..my project and my many problems Paul Gregory 2003-02-24 15:12 ` Preben Randhol @ 2003-02-24 15:38 ` Hyman Rosen 2003-02-24 18:08 ` Preben Randhol ` (3 more replies) 2003-02-25 17:31 ` Matthew Heaney 2 siblings, 4 replies; 25+ messages in thread From: Hyman Rosen @ 2003-02-24 15:38 UTC (permalink / raw) Paul Gregory wrote: > type English is (big, black, cat, dog, es, small, the, white); > type French is (grand, noir, chat, chien, est, petit, le, blanc); This is the core of your difficulties. Since the dictionary is meant to be extensible (as you yourself have noticed), using enumerations to encode the words is inappropriate - these types are fixed at compile-time, and you cannot add to them at runtime. You need to represent your words as strings, and maintain a mapping table between English and French that way. It would be easy in C++ or Perl, which come with built in map containers. In Ada, you'll have to roll your own. ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-24 15:38 ` Hyman Rosen @ 2003-02-24 18:08 ` Preben Randhol 2003-02-25 2:01 ` Hyman Rosen 2003-02-24 18:37 ` Simon Wright ` (2 subsequent siblings) 3 siblings, 1 reply; 25+ messages in thread From: Preben Randhol @ 2003-02-24 18:08 UTC (permalink / raw) Hyman Rosen wrote: > table between English and French that way. It would be easy in C++ > or Perl, which come with built in map containers. In Ada, you'll Oh, is Safe STL standard now? http://www.horstmann.com/safestl.html -- Preben Randhol ---------------- http://www.pvv.org/~randhol/ -- "Violence is the last refuge of the incompetent", Isaac Asimov ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-24 18:08 ` Preben Randhol @ 2003-02-25 2:01 ` Hyman Rosen 2003-02-25 9:46 ` Preben Randhol 0 siblings, 1 reply; 25+ messages in thread From: Hyman Rosen @ 2003-02-25 2:01 UTC (permalink / raw) Preben Randhol wrote: > Hyman Rosen wrote: >>table between English and French that way. It would be easy in C++ >>or Perl, which come with built in map containers. In Ada, you'll > Oh, is Safe STL standard now? > http://www.horstmann.com/safestl.html What does easy have to do with safe? In any case, the site that you point to states that Safe STL has the same interface as normal STL, so any implementation may choose to supply it as a conforming option. ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-25 2:01 ` Hyman Rosen @ 2003-02-25 9:46 ` Preben Randhol 2003-02-25 16:07 ` Hyman Rosen 0 siblings, 1 reply; 25+ messages in thread From: Preben Randhol @ 2003-02-25 9:46 UTC (permalink / raw) Hyman Rosen wrote: > What does easy have to do with safe? This is the exact problem with the software industry. Doing it the easy way and ignoring the safty. -- Preben Randhol ---------------- http://www.pvv.org/~randhol/ -- "Violence is the last refuge of the incompetent", Isaac Asimov ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-25 9:46 ` Preben Randhol @ 2003-02-25 16:07 ` Hyman Rosen 0 siblings, 0 replies; 25+ messages in thread From: Hyman Rosen @ 2003-02-25 16:07 UTC (permalink / raw) Preben Randhol wrote: > This is the exact problem with the software industry. > Doing it the easy way and ignoring the safty. Huh? In C++ I can declare a map<string,string> and store my dictionary in it as simply as 'EtoF["cat"] = "chat";'. In Perl, I can do '$EtoF{cat} = "chat";'. Ada doesn't come with a containers library, so you have to build, buy, or find one before you can use it. What does any of this have to do with safety? ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-24 15:38 ` Hyman Rosen 2003-02-24 18:08 ` Preben Randhol @ 2003-02-24 18:37 ` Simon Wright 2003-02-24 22:55 ` Jano 2003-02-25 21:56 ` Simon Wright 2003-02-25 8:45 ` Rodrigo García 2003-02-25 17:34 ` Matthew Heaney 3 siblings, 2 replies; 25+ messages in thread From: Simon Wright @ 2003-02-24 18:37 UTC (permalink / raw) Hyman Rosen <hyrosen@mail.com> writes: > You need to represent your words as strings, and maintain a mapping > table between English and French that way. It would be easy in C++ > or Perl, which come with built in map containers. In Ada, you'll > have to roll your own. There are several Ada container libraries, but none of them are standardised. Paul, would you be marked down for using a container library? if not, you could check out the Booch Components at http://www.pushface.org/components/bc/ or the other container libraries at http://www.pushface.org/components/ . ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-24 18:37 ` Simon Wright @ 2003-02-24 22:55 ` Jano 2003-02-25 17:36 ` Matthew Heaney 2003-02-25 21:56 ` Simon Wright 1 sibling, 1 reply; 25+ messages in thread From: Jano @ 2003-02-24 22:55 UTC (permalink / raw) En el mensaje <x7vk7fpr0ch.fsf@smaug.pushface.org>, simon@pushface.org dice... > Paul, would you be marked down for using a container library? if not, > you could check out the Booch Components at > http://www.pushface.org/components/bc/ or the other container Uhm, being a novice I think he will find the Booch Components very difficult. I would go for two static arrays of (un)bounded strings, and a counter for words in use. I know, the maximum capacity would be bounded, but... I guess is a more novice approach. -- ------------------------- Jano 402450[at]cepsz.unizar.es ------------------------- ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-24 22:55 ` Jano @ 2003-02-25 17:36 ` Matthew Heaney 0 siblings, 0 replies; 25+ messages in thread From: Matthew Heaney @ 2003-02-25 17:36 UTC (permalink / raw) Jano <402450@cepsz.unizar.es> wrote in message news:<MPG.18c4c14ba0317dc99896a9@News.CIS.DFN.DE>... > > I would go for two static arrays of (un)bounded strings, and a counter > for words in use. I know, the maximum capacity would be bounded, but... The Charles library has dedicated map containers that have type String as the key. http://home.earthlink.net/~matthewjheaney/charles/ ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-24 18:37 ` Simon Wright 2003-02-24 22:55 ` Jano @ 2003-02-25 21:56 ` Simon Wright 1 sibling, 0 replies; 25+ messages in thread From: Simon Wright @ 2003-02-25 21:56 UTC (permalink / raw) Simon Wright <simon@pushface.org> writes: > Paul, would you be marked down for using a container library? if not, > you could check out the Booch Components at > http://www.pushface.org/components/bc/ or the other container > libraries at http://www.pushface.org/components/ . Sorry, what I meant by the last line was "the other container libraries that I've listed at http://www.pushface.org/components/" ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-24 15:38 ` Hyman Rosen 2003-02-24 18:08 ` Preben Randhol 2003-02-24 18:37 ` Simon Wright @ 2003-02-25 8:45 ` Rodrigo García 2003-02-25 17:34 ` Matthew Heaney 3 siblings, 0 replies; 25+ messages in thread From: Rodrigo García @ 2003-02-25 8:45 UTC (permalink / raw) Hyman Rosen wrote: > Paul Gregory wrote: > >> type English is (big, black, cat, dog, es, small, the, white); >> type French is (grand, noir, chat, chien, est, petit, le, blanc); > > > This is the core of your difficulties. Since the dictionary is > meant to be extensible (as you yourself have noticed), using > enumerations to encode the words is inappropriate - these types > are fixed at compile-time, and you cannot add to them at runtime. > > You need to represent your words as strings, and maintain a mapping > table between English and French that way. It would be easy in C++ > or Perl, which come with built in map containers. In Ada, you'll > have to roll your own. > I prefer Python for this. Not surprisingly, in Python they call these kind of containers "Dictionaries". If you are using GNAT, maybe you can try its hash table... Rodrigo ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-24 15:38 ` Hyman Rosen ` (2 preceding siblings ...) 2003-02-25 8:45 ` Rodrigo García @ 2003-02-25 17:34 ` Matthew Heaney 2003-02-25 18:03 ` Hyman Rosen 3 siblings, 1 reply; 25+ messages in thread From: Matthew Heaney @ 2003-02-25 17:34 UTC (permalink / raw) Hyman Rosen <hyrosen@mail.com> wrote in message news:<1046101109.45189@master.nyc.kbcfp.com>... > > You need to represent your words as strings, and maintain a mapping > table between English and French that way. It would be easy in C++ > or Perl, which come with built in map containers. In Ada, you'll > have to roll your own. No, you won't, because the STL has already been ported to Ada. The library is called "Charles", in honor of Charles Babbage, with whom Lady Ada was affiliated. http://home.earthlink.net/~matthewjheaney/charles/ ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-25 17:34 ` Matthew Heaney @ 2003-02-25 18:03 ` Hyman Rosen 2003-02-26 8:14 ` Preben Randhol 0 siblings, 1 reply; 25+ messages in thread From: Hyman Rosen @ 2003-02-25 18:03 UTC (permalink / raw) Matthew Heaney wrote: > No, you won't, because the STL has already been ported to Ada. I know, actually, and good work it is. But it won't be readily at hand for the struggling student doing his homework. ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-25 18:03 ` Hyman Rosen @ 2003-02-26 8:14 ` Preben Randhol 0 siblings, 0 replies; 25+ messages in thread From: Preben Randhol @ 2003-02-26 8:14 UTC (permalink / raw) Hyman Rosen wrote: > Matthew Heaney wrote: >> No, you won't, because the STL has already been ported to Ada. > > I know, actually, and good work it is. But it won't be > readily at hand for the struggling student doing his > homework. Well, depends on the assignment. However if one were allowed to use charles or STL I think the assignment is a bit too trivial and would probably be bigger. -- Preben Randhol ---------------- http://www.pvv.org/~randhol/ -- "Violence is the last refuge of the incompetent", Isaac Asimov ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-24 13:52 Thanks guys..my project and my many problems Paul Gregory 2003-02-24 15:12 ` Preben Randhol 2003-02-24 15:38 ` Hyman Rosen @ 2003-02-25 17:31 ` Matthew Heaney 2003-02-25 19:57 ` chris.danx 2003-02-26 14:06 ` Paul Gregory 2 siblings, 2 replies; 25+ messages in thread From: Matthew Heaney @ 2003-02-25 17:31 UTC (permalink / raw) Paul Gregory <pg16@bton.ac.uk> wrote in message news:<3E5A2381.21FD3F14@bton.ac.uk>... > > My project is this > > You are invited to design and implement a translation program which will > take an English sentence and translate it into French (and optionally > translate from French to English). You could use Charles, which contains data structures that are exactly suited to your problem. package Translation_Tables is new Charles.Maps.Sorted.Strings.Unbounded (Element_Type => Charles.Strings.Unbounded.Container_Type); E2F : Translation_Tables.Container_Type; Insert (E2F, "big", Element => To_Container ("grand")); Insert (E2F, ...); procedure Lookup (E : String) is I : constant Iterator_Type := Find (E2F, Key => E); begin if I = Back (E2F) then --not found else declare F : Element_Subtype renames To_Access (I); --no copy --or F : constant Element_Subtype := Element (I); --copy begin --do something with F end; end if; end Lookup; The string map above accepts a key comparison operator as a generic formal subprogram, so you could plug one in that provides a case-insensitive compare. There's also a map version implemented as a hash table, and there's a hash function provided for type String. (The sorted version is implemented as a red-black tree.) I used the new Charles string container as the map element, but you could just as easily use any other non-limited type, such as Ada.Strings.Unbounded. A string pointer would also suffice, too. http://home.earthlink.net/~matthewjheaney/charles/ The most recent release occurred on 18 Feb 2003. Drop me a line if you have any questions. Matt ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-25 17:31 ` Matthew Heaney @ 2003-02-25 19:57 ` chris.danx 2003-02-25 21:17 ` Chad R. Meiners 2003-02-26 14:06 ` Paul Gregory 1 sibling, 1 reply; 25+ messages in thread From: chris.danx @ 2003-02-25 19:57 UTC (permalink / raw) Matthew Heaney wrote: > Paul Gregory <pg16@bton.ac.uk> wrote in message news:<3E5A2381.21FD3F14@bton.ac.uk>... > >>My project is this >> >>You are invited to design and implement a translation program which will >>take an English sentence and translate it into French (and optionally >>translate from French to English). > > > You could use Charles, which contains data structures that are exactly > suited to your problem. If it's a uni assignment, chances are that Paul isn't allowed to use other peoples code in their exercise (especially at or near the beginning of learning to program, and in data structures courses)! At our uni (glasgow), we where unable to use others code until 3rd year and then only under authorisation by the lecturer who set the exercise! I always found it strange that they rammed Code Reuse down our throats the whole of second year, yet we weren't allowed to practise it! Danx ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-25 19:57 ` chris.danx @ 2003-02-25 21:17 ` Chad R. Meiners 2003-03-05 9:22 ` chris.danx 0 siblings, 1 reply; 25+ messages in thread From: Chad R. Meiners @ 2003-02-25 21:17 UTC (permalink / raw) "chris.danx" <spamoff.danx@ntlworld.com> wrote in message news:uUP6a.4405$EN3.34753@newsfep4-glfd.server.ntli.net... > I always found it strange that they rammed Code Reuse down our throats > the whole of second year, yet we weren't allowed to practise it! Perhaps they wanted you to write your code so you could reuse it in other projects. Data structure packages I wrote in other classes came in handy when doing other assignments when I was an undergraduate. Of course your milage could vary depending on your university. -CRM ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-25 21:17 ` Chad R. Meiners @ 2003-03-05 9:22 ` chris.danx 0 siblings, 0 replies; 25+ messages in thread From: chris.danx @ 2003-03-05 9:22 UTC (permalink / raw) Chad R. Meiners wrote: > "chris.danx" <spamoff.danx@ntlworld.com> wrote in message > news:uUP6a.4405$EN3.34753@newsfep4-glfd.server.ntli.net... > >>I always found it strange that they rammed Code Reuse down our throats >>the whole of second year, yet we weren't allowed to practise it! > > > Perhaps they wanted you to write your code so you could reuse it in other > projects. Data structure packages I wrote in other classes came in handy > when doing other assignments when I was an undergraduate. Of course your > milage could vary depending on your university. Yeah, I started doing that at the beginning of my first second year (I repeated second year). The problem was that by the time I had amassed a number of data structures, I'd learned new ways to make them better and improve their interfaces. Now they just all look ugly to me. One of the other problems I had was that the university has an assignment management system which is truely crap. Unless they'd specifically set it up to grab everything in a directory, it would only grab the files it knew about so we couldn't submit our data structures with our assignments. In first and second year, I usually had to moan at my supervisor to get packages sumbitted as I often liked to use the main program simply as a driver rather than plonk all the code in it once it got to 400+ lines of code. Really frustrating :( Also we can't really use them now, Ada was only used in term 1 and now it's all Java and C. Java has loads of data structures, so writing them for that is not really worth it and C, well, I don't want to go there... :) Chris ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-25 17:31 ` Matthew Heaney 2003-02-25 19:57 ` chris.danx @ 2003-02-26 14:06 ` Paul Gregory 2003-02-26 18:09 ` tmoran 2003-02-27 17:12 ` Update - PLEASE SOMEBODY HELP!!!! Paul Gregory 1 sibling, 2 replies; 25+ messages in thread From: Paul Gregory @ 2003-02-26 14:06 UTC (permalink / raw) Thanks a million Matthew and everybody else...however I went to see my tutor today and he refused me permission to use your package. Apparantly I MUST use a package that the University supplies called "string_tokenizer" to read the tokens in my array when translation...so It's back for the drawing board for me unfortunately :-( ----> ---------------------------------------------------------------------------------------- package body String_tokenizer is -- Uses recursive descent. -- Each syntactic token has a recognizer, which returns its token iff -- it is recognized at the current position in the input string. -- If a recognizer recognizes its token, it consumes the corresponding -- portion of the input string; if not, it leaves the string for others -- to attempt to recognize. -- Higher level syntactic recognizers call lower level recognizers, -- possibly recursively. -- Token string syntax ---------------------- -- NO_MORE_TOKENS ::= end of input string -- WORD_CHAR ::= A..Z | a..z -- WORD ::= WORD_CHAR | WORD_CHAR WORD -- PUNCTUATION ::= . | , | : | ; | ' | " | ` -- NUMBER_CHAR ::= 0..9 -- NUMBER ::= NUMBER_CHAR | NUMBER_CHAR NUMBER -- OPERATOR ::= + | - | * | / | ** -- BRACKET ::= ( | ) | { | } | [ | } -- WHITE_SPACE ::= space_character | space_character WHITE_SPACE -- OTHER ::= characters other than those mentioned above -- TOKEN ::= NO_MORE_TOKENS | WORD | PUNCTUATION | NUMBER | -- OPERATOR | BRACKET | OTHER -- STRING_TOKEN ::= WORD | PUNCTUATION | NUMBER | OPERATOR | BRACKET | -- OTHER -- NON_WHITE_SPACE_TOKEN_STRING ::= NO_MORE_TOKENS | STRING_TOKEN TOKEN_STRING -- TOKEN_STRING ::= NON_WHITE_SPACE_TOKEN_STRING | -- WHITE_SPACE NON_WHITE_SPACE_TOKEN_STRING WHITE_SPACE : constant integer := 7; -- needed internally, not for client FAIL : constant integer := 8; -- needed internally, not for client function char_type(c : Character) return Integer is -- post: returns type code for character c -- test: -- space -- all punctuation characters -- brackets -- a,z,A,Z -- 0,9 -- all operators -- other character char_type_table : array (Character range' '..'~') of Integer := (WHITE_SPACE,PUNCTUATION,PUNCTUATION,OTHER,OTHER,OTHER,OTHER,PUNCTUATION, -- ' '..''' BRACKET,BRACKET,OPERATOR,OPERATOR,PUNCTUATION,OPERATOR,PUNCTUATION,OPERATOR, -- '('..'/' NUMBER,NUMBER,NUMBER,NUMBER,NUMBER,NUMBER,NUMBER,NUMBER, -- '0'..'7' NUMBER,NUMBER,PUNCTUATION,PUNCTUATION,OPERATOR,OPERATOR,OPERATOR,PUNCTUATION, -- '8'..'?' OTHER,WORD,WORD,WORD,WORD,WORD,WORD,WORD, -- '@'..'G' WORD,WORD,WORD,WORD,WORD,WORD,WORD,WORD, -- 'H'..'O' WORD,WORD,WORD,WORD,WORD,WORD,WORD,WORD, -- 'P'..'W' WORD,WORD,WORD,BRACKET,OTHER,BRACKET,OTHER,OTHER, -- 'X'..'_' PUNCTUATION,WORD,WORD,WORD,WORD,WORD,WORD,WORD, -- '`'..'g' WORD,WORD,WORD,WORD,WORD,WORD,WORD,WORD, -- 'h'..'o' WORD,WORD,WORD,WORD,WORD,WORD,WORD,WORD, -- 'p'..'w' WORD,WORD,WORD,BRACKET,OTHER,BRACKET,OTHER); -- 'x'..'~' begin if c not in ' '..'~' then return OTHER; end if; return char_type_table(c); end; IN_STRING_SIZE : constant integer := 1000; in_string : String(1..IN_STRING_SIZE); in_string_pos : integer := IN_STRING_SIZE; blank_token : constant Token := (" ",0,0,NO_MORE_TOKENS); fail_token : constant Token := (" ",0,0,FAIL); current_token : Token := blank_token; -- methods to access current character and move to next character -- in input string function has_current_char return Boolean is --test: -- just before end of string -- end of string begin return in_string_pos <= IN_STRING_SIZE; end; function current_char return Character is -- pre: has_current_char -- post: returns current char -- test: once thru begin return in_string(in_string_pos); end; procedure inc is -- test : once thru begin in_string_pos := in_string_pos + 1; end; -- recognizer methods --------------------- function recognize_char(i : Integer) return Token is -- pre: i = character type to be recognized -- post: returns character token if recognized, fail token if not -- test: -- end of string -- current char is required char -- current char is not required char my_token : Token; begin if not has_current_char then return fail_token; end if; if char_type (current_char) = i then my_token := (current_char & " ",1,0,i); inc; return my_token; else return fail_token; end if; end; function recognize_sequence(i : Integer) return Token is -- pre: i = char type of sequence to be recognized -- post: returns token for recognized sequence, -- or fail token if not found -- test: -- not a recognized sequence -- sequence of 1 char -- sequence of 3 chars my_token : Token; next_Token : Token; word_pos :Integer; begin my_token := recognize_char(i); if my_token.token_type = FAIL then return my_token; end if; word_pos := 2; -- start position for rest of word loop next_Token := recognize_char(i); if next_token.token_type = FAIL then return my_token; end if; if word_pos <= TOKEN_STRING_SIZE then my_token.token_string(word_pos) := next_Token.token_string(1); word_pos := word_pos + 1; my_token.token_string_length := my_token.token_string_length + 1; end if; end loop; end; function recognize_no_more_tokens return Token is -- test: -- not end of string -- end of string begin if not has_current_char then return blank_token; else return fail_token; end if; end; function recognize_word_char return Token is begin return recognize_char(WORD); end; function recognize_word return Token is begin return recognize_sequence(WORD); end; function recognize_punctuation return Token is begin return recognize_char(PUNCTUATION); end; function recognize_number_char return Token is begin return recognize_char(NUMBER); end; function recognize_number return Token is -- test: -- not a number -- 2 char number my_token : Token; begin my_token := recognize_sequence(NUMBER); if my_token.token_type = FAIL then return my_token; end if; for i in 1..my_token.token_string_length loop my_token.token_value := my_token.token_value * 10 + character'pos(my_token.token_string(i)) - character'pos('0'); end loop; return my_token; end; function recognize_operator return Token is -- test: -- / -- */ -- **/ -- * at end of string my_token : Token; begin my_token := recognize_char(OPERATOR); if my_token.token_string(1) = '*' then if has_current_char and then current_char = '*' then my_token.token_string(2) := '*'; my_token.token_string_length := 2; inc; end if; end if; return my_token; end; function recognize_bracket return Token is begin return recognize_char(BRACKET); end; function recognize_white_space return Token is begin return recognize_sequence(WHITE_SPACE); end; function recognize_other return Token is begin return recognize_char(OTHER); end; function recognize_string_token return Token is -- test: -- word -- punctuation -- number -- operator -- bracket -- other -- none of these my_token : Token; begin my_token := recognize_word; if my_token.token_type /= FAIL then return my_token; end if; my_token := recognize_punctuation; if my_token.token_type /= FAIL then return my_token; end if; my_token := recognize_number; if my_token.token_type /= FAIL then return my_token; end if; my_token := recognize_operator; if my_token.token_type /= FAIL then return my_token; end if; my_token := recognize_bracket; if my_token.token_type /= FAIL then return my_token; end if; my_token := recognize_other; if my_token.token_type /= FAIL then return my_token; end if; return fail_token; end; function recognize_token_string_token return Token is -- test: -- NO_MORE_TOKENS preceded by whitespace -- string token preceded by whitespace my_token : Token; RECOGNIZE_FAILURE : exception; begin my_token := recognize_white_space; -- ignore whether there or not my_token := recognize_no_more_tokens; if my_token.token_type /= FAIL then return my_token; end if; my_token := recognize_string_token; if my_token.token_type /= FAIL then return my_token; end if; raise RECOGNIZE_FAILURE; end; procedure set_string(s : String) is -- pre: s = string to be tokenized -- post: in_string has been set up (space-padded) -- current_token is set to first token of string -- test: -- once thru with non-null s begin in_string_pos := 1; for i in s'range loop in_string(in_string_pos) := s(i); in_string_pos := in_string_pos + 1; end loop; while in_string_pos <= IN_STRING_SIZE loop in_string(in_string_pos) := ' '; in_string_pos := in_string_pos + 1; end loop; in_string_pos := in_string'first; current_token := recognize_token_string_token; end; function has_more_tokens return boolean is -- post: returns true iff there are more tokens in the string -- test: once thru begin return current_token.token_type /= NO_MORE_TOKENS; end; function next_token return Token is -- post: returns next token of string, or NO_MORE_TOKENS token if none -- test: twice thru my_token : Token; begin my_token := current_token; current_token := recognize_token_string_token; return my_token; end; end; ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Thanks guys..my project and my many problems 2003-02-26 14:06 ` Paul Gregory @ 2003-02-26 18:09 ` tmoran 2003-02-27 17:12 ` Update - PLEASE SOMEBODY HELP!!!! Paul Gregory 1 sibling, 0 replies; 25+ messages in thread From: tmoran @ 2003-02-26 18:09 UTC (permalink / raw) >package body String_tokenizer is Why in heavens name did you post this package *body*, which is of zero interest, just obfuscates things, and is 400 lines long? In Ada, all the information that should be needed by the user of a package lies in the package *specification*. Neither you nor any other user of String_tokenizer should have any need to see its body. ^ permalink raw reply [flat|nested] 25+ messages in thread
* Update - PLEASE SOMEBODY HELP!!!! 2003-02-26 14:06 ` Paul Gregory 2003-02-26 18:09 ` tmoran @ 2003-02-27 17:12 ` Paul Gregory [not found] ` <bot2j-ei3.ln1@beastie.ix.netcom.com> 1 sibling, 1 reply; 25+ messages in thread From: Paul Gregory @ 2003-02-27 17:12 UTC (permalink / raw) Well...the deadline is now 24 hours away.... I have the excellent program which Dennis Lee Bieber helped me with (thanks a lot Dennis you are a great guy!) detailed below which works excellent on the GNAT compiler. The problem is that the project is submitted online and run through a computer system and it has to translate EXACTLY to the letter to gain a pass (and it is pass or fail...you could have a program 100x better that does exactly the same but if it missed a full stop then it won't accept it etc) Now I need the program to compile like this for each of the four tests... eg). ----------Sorry expected answer was------------------------------- Do you wish to add words to the dictionary?: N Please enter a sentence: big. The French translation of your sentence is: grand. ----------Your answer however was--------------------------------- . eg 2 ----------Sorry expected answer was------------------------------- Do you wish to add words to the dictionary?: N Please enter a sentence: Big cat is black. The French translation of your sentence is: Grand chat est noir. eg3 ----------Sorry expected answer was------------------------------- Do you wish to add words to the dictionary?: Y English word: red Equivalent French word: rouge Do you wish to add more words?: N Please enter a sentence: The dog is red. The French translation of your sentence is: Le chien est rouge. eg4 ----------Sorry expected answer was------------------------------- Do you wish to add words to the dictionary?: Y English word: blue Equivalent French word: bleu Do you wish to add more words?: Y English word: sky Equivalent French word: ciel Do you wish to add more words?: N Please enter a sentence: Le ciel est bleu. The English translation of your sentence is: The sky is blue. -------------------- However due to the way my program is structured it asks for a "source language" and a "target language", yet I can't have this for the system. Eg it compiles on GNAT like so... ------------------------------------------------------------------------- Do you wish to add words to the dictionary? : n Enter the Source Language: English Enter the Target Language: French Please enter a sentence: The big cat The FRENCH translation of your sentence is: Le grand chat Enter the Source Language: -------------------------------------------------------------------------- So the problems are - French in capitals - uses 'image is there a way round this ? Don't want it to ask me for a source and target language. Need a full stop after every sentence. I'd be the happiest person in the world if somebody could help me resolve these problems and I'd repay you in whatever favours, cash whatever you require as I've hit the wall and the deadline is approaching and I'm going to almost certainly fail. :-( Thanks for your time, I know I'm very cheeky and as thick as a plank of wood... but I can't stress how stressed out, physically ill and depressed this damn project is making me feel. Even if you can only work out how to do the first test Ie... just a straight one word array comparison - your help would be greatfully appreciated. Thanks a million, Paul (code below, for anyone who is bored/kind enough to help).... with Ada.Text_IO; use Ada.Text_IO; with Ada.Strings; use Ada.Strings; with Ada.Strings.Fixed; use Ada.Strings.Fixed; with Ada.Characters.Handling; use Ada.Characters.Handling; procedure main is MaxWord : constant := 40; MaxLine : constant := 120; MaxTranslations : constant := 100; type Languages is (English, French); -- define known languages -- For each language named here, the Translations array will -- need another Head("word_in_new", MaxWord) added to each -- predefined set. subtype aWord is String(1..MaxWord); -- assume words are smaller subtype aLine is String(1..MaxLine); LineIn : aLine; -- lots of poorly named scratch variables WordIn : aWord; WordOut : aWord; Source : Languages; Target : Languages; SOL : boolean; -- Start of Line flag LineLen : integer range 0..MaxLine; Blnk : integer range 0..MaxLine; type TranslationSet is array(Languages) of aWord; -- use Head(string, size) with default pad to fill short -- strings to word length fixed strings Translations : array (1..MaxTranslations) of TranslationSet := ( (Head("big", MaxWord), Head("grand", MaxWord)), (Head("black", MaxWord), Head("noir", MaxWord)), (Head("cat", MaxWord), Head("chat", MaxWord)), (Head("dog", MaxWord), Head("chien", MaxWord)), (Head("is", MaxWord), Head("est", MaxWord)), (Head("small", MaxWord), Head("petit", MaxWord)), (Head("the", MaxWord), Head("le", MaxWord)), (Head("white", MaxWord), Head("blanc", MaxWord)), (Head("Brighton", MaxWord), Head("Brighton", MaxWord)), others => (Head(" ", MaxWord), Head(" ", MaxWord)) ); LastTranslation : integer range 1..MaxTranslations := 9; begin -- allow user to add words to translation dictionary as long as space -- is available (note: dictionary is not saved between program runs!!) while LastTranslation < MaxTranslations loop Put("Do you wish to add words to the dictionary? : "); Get_Line(Item => LineIn, Last => LineLen); exit When To_Upper(LineIn(1)) /= 'Y'; LastTranslation := LastTranslation + 1; for l in Languages loop Put("Enter the new word in "); Put(Languages'Image(l)); Put(": "); Get_Line(Item => WordIn, Last => LineLen); Translations(LastTranslation)(l) := Head(To_Lower(WordIn(1..LineLen)), MaxWord); end loop; New_Line; end loop; -- loop until user enters a blank line for Source language loop New_Line; Put("Enter the Source Language: "); Get_Line(Item => WordIn, Last => LineLen); exit when LineLen = 0; Source := Languages'Value(WordIn(1..LineLen)); Put("Enter the Target Language: "); Get_Line(Item => WordIn, Last => LineLen); Target := Languages'Value(WordIn(1..LineLen)); Put_Line("Please enter a sentence: "); Get_Line(Item => LineIn, Last => LineLen); LineIn := Head(To_Lower(LineIn(1..LineLen)), MaxLine); New_Line; Put("The "); Put(Languages'Image(Target)); Put_Line(" translation is: "); -- loop until the input line has been consumed. SOL := True; while LineIn(1) /= ' ' loop Blnk := Index(LineIn, " "); if Blnk > 1 and Blnk <= MaxWord then WordIn := Head(LineIn(1..Blnk-1), MaxWord); LineIn := Head(LineIn(Blnk+1..MaxLine), MaxLine); else WordIn := Head(LineIn(1..MaxWord), MaxWord); LineIn := Head(" ", MaxLine); end if; WordOut := Head("<" & Trim(WordIn, BOTH) & ">", MaxWord); -- if no translation, output input value for i in 1..LastTranslation loop if WordIn = Translations(i)(Source) then WordOut := Translations(i)(Target); exit; end if; end loop; if SOL then WordOut(1) := To_Upper(WordOut(1)); SOL := False; end if; Put(Item => Trim(WordOut, BOTH)); Put(" "); end loop; New_Line; New_Line; end loop; end main; ^ permalink raw reply [flat|nested] 25+ messages in thread
[parent not found: <bot2j-ei3.ln1@beastie.ix.netcom.com>]
* Re: Update - PLEASE SOMEBODY HELP!!!! [not found] ` <bot2j-ei3.ln1@beastie.ix.netcom.com> @ 2003-02-28 10:16 ` Georg Bauhaus 2003-02-28 10:53 ` Paul Gregory 0 siblings, 1 reply; 25+ messages in thread From: Georg Bauhaus @ 2003-02-28 10:16 UTC (permalink / raw) Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote: : Paul Gregory fed this fish to the penguins on Thursday 27 February 2003 : 09:12 am: : : Addendum to my previous post... : : I /would/ be interested in seeing the spec file for the tokenizer... Maybe you could ask John English, as Gregory appears to be in Brighton. Gregory, have you seen him and asked him about the whys and hows of the assignment? ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Update - PLEASE SOMEBODY HELP!!!! 2003-02-28 10:16 ` Georg Bauhaus @ 2003-02-28 10:53 ` Paul Gregory 2003-02-28 16:47 ` Simon Wright 0 siblings, 1 reply; 25+ messages in thread From: Paul Gregory @ 2003-02-28 10:53 UTC (permalink / raw) It's getting hold of him ! A very busy man. Georg Bauhaus wrote: > Dennis Lee Bieber <wlfraed@ix.netcom.com> wrote: > : Paul Gregory fed this fish to the penguins on Thursday 27 February 2003 > : 09:12 am: > : > : Addendum to my previous post... > : > : I /would/ be interested in seeing the spec file for the tokenizer... > > Maybe you could ask John English, as Gregory appears to be in Brighton. > Gregory, have you seen him and asked him about the whys and hows of the > assignment? ^ permalink raw reply [flat|nested] 25+ messages in thread
* Re: Update - PLEASE SOMEBODY HELP!!!! 2003-02-28 10:53 ` Paul Gregory @ 2003-02-28 16:47 ` Simon Wright 0 siblings, 0 replies; 25+ messages in thread From: Simon Wright @ 2003-02-28 16:47 UTC (permalink / raw) Paul Gregory <pg16@bton.ac.uk> writes: > It's getting hold of him ! > > A very busy man. But nevertheless known to read c.l.a! ^ permalink raw reply [flat|nested] 25+ messages in thread
end of thread, other threads:[~2003-03-05 9:22 UTC | newest] Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2003-02-24 13:52 Thanks guys..my project and my many problems Paul Gregory 2003-02-24 15:12 ` Preben Randhol 2003-02-24 15:38 ` Hyman Rosen 2003-02-24 18:08 ` Preben Randhol 2003-02-25 2:01 ` Hyman Rosen 2003-02-25 9:46 ` Preben Randhol 2003-02-25 16:07 ` Hyman Rosen 2003-02-24 18:37 ` Simon Wright 2003-02-24 22:55 ` Jano 2003-02-25 17:36 ` Matthew Heaney 2003-02-25 21:56 ` Simon Wright 2003-02-25 8:45 ` Rodrigo García 2003-02-25 17:34 ` Matthew Heaney 2003-02-25 18:03 ` Hyman Rosen 2003-02-26 8:14 ` Preben Randhol 2003-02-25 17:31 ` Matthew Heaney 2003-02-25 19:57 ` chris.danx 2003-02-25 21:17 ` Chad R. Meiners 2003-03-05 9:22 ` chris.danx 2003-02-26 14:06 ` Paul Gregory 2003-02-26 18:09 ` tmoran 2003-02-27 17:12 ` Update - PLEASE SOMEBODY HELP!!!! Paul Gregory [not found] ` <bot2j-ei3.ln1@beastie.ix.netcom.com> 2003-02-28 10:16 ` Georg Bauhaus 2003-02-28 10:53 ` Paul Gregory 2003-02-28 16:47 ` Simon Wright
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox