Dec-21-2019, 04:49 PM
(This post was last modified: Dec-21-2019, 05:03 PM by Gribouillis.)
(Dec-20-2019, 06:59 PM)Gribouillis Wrote:kingsman Wrote:(a) I am ten years oldHere is a basic algorithmic scheme that you could follow for such a work
I love eating apple
I am a boy
(b) Apple is my favourite fruit
I am a male
I have birthed 10 years
Output:Description of the input language: it has 3 statements age_stm: syntax: "I am <number> years old" parameters: number: a number written in english eating_stm: syntax: "I love eating <food>" parameters: food: the name of some food gender_stm: syntax: "I am a <gender>" parameters: gender: boy or girl First step: PARSING OF THE INPUT FILE The aim of this step is to recognize the statements contained in the input file and their parameters. In the example, the result is a sequence of python objects age_stm(number = ten) eating_stm(food = apple) gender_stm(gender = boy) Second step: CODE GENERATION each parsed statement is translated in a statement of the second language according to a specific rule rule for an age_stm: convert the number in english to a number in digits emit the statement "I have birthed <digits> years" rule for a eating_stm: identify the type of the food (eg apple is a fruit) emit the statement "<food> is my favorite <food type>" rule for a gender_stm: convert the gender to the appropriate transcription (boy -> male) emit the statement "I am a <converted gender>" There may be many more rules, for example there may be rules to change the order of the emited statements, also a single input statement may induce more than one output statement, etc.
These are the codes I have create
File = open('First.txt', 'r') thing = [] for line in File: thing.append(line) first_stm = thing[0] second_stm = thing[1] third_stm = thing[2] number = (first_stm.split()[2]) food = (second_stm.split()[3]) gender = (third_stm.split()[3]) File.close() Conversion = open('First conversion', 'w+') Conversion.write(food + ' is my favourite fruit\n') if gender == 'boy': Conversion.write('I am a male') else: Conversion.write('I am a female') Conversion.close()For the first statement, I need to convert ten to 10.
But there are various situations, the age can be 11, 13 ,46, 57, 65
So, how can I convert it from a string to digit.
For the third statement, the case is just boy or girl, It does not similar to the first statement that having so many options.
How about imitate the third statement using 'if' statement
for example
if number == 'one' Conversion.write('I have birthed 1 years') elif number == 'two' Conversion.write('I have birthed 2 years')If I use this method, I need to use 'if' for many time such as 100 time to satisfy the reality case(100 years old)
Is there any easier method to deal with it?