Python Forum
Python for syntax conversion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python for syntax conversion
#1
My homework needs to use Python to write a programme for syntax files conversion.
For many construction software, the interoperability is weak and they cannot be shared to use.
However, they would contain a syntax file when they are saved.
That syntax file uses semantic semantic expression to present the building model in that software.
This file can be inported back to the corresponding software to use.
However, different construction software possesses their own syntax files with different syntax format and arrangement.
Therefore, this homework need me to write a programme for syntax files conversion.
But, I do not know how to start. I know the 'open' command let me to open the files in Python but I do not know how to do in the next step

For example,
This is one of the things in Etabs, a construction software

FRAMESECTION "B200x300" MATERIAL "C45" SHAPE "Concrete Rectangular" D 300 B 200

This is another syntax in SAP2000, also a construction software

SectionName=B200x300 Material=C45 Shape=Rectangular t3=300 t2=200 Area=60000

So, how can I write the programme so that the a new syntax files would be created and change the things in Etabs to SAP2000
Reply
#2
First you have to read the Etabs data. They appear to be in name/value pairs. So I would use split() to get the words. Loop through the words, starting with an empty name, and an empty list of name/value pairs. For each word, if you don't have a name, save it as the name. If you have a name and the word only has numbers (use isdigit() to check), append a tuple of the name and the word to the the list, and clear the name. Otherwise, keep adding to the value until you have a double quote at the end, and store that as a name/value tuple and clear the name.

Then you need to translate. You'll need a mapping of terms, like FRAMESECTION to SectionName. Loop through your stored name/value pairs, change the names and give the values with the new format, and make sure you add things like area as needed.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Dec-20-2019, 03:58 PM)ichabod801 Wrote: First you have to read the Etabs data. They appear to be in name/value pairs. So I would use split() to get the words. Loop through the words, starting with an empty name, and an empty list of name/value pairs. For each word, if you don't have a name, save it as the name. If you have a name and the word only has numbers (use isdigit() to check), append a tuple of the name and the word to the the list, and clear the name. Otherwise, keep adding to the value until you have a double quote at the end, and store that as a name/value tuple and clear the name.

Then you need to translate. You'll need a mapping of terms, like FRAMESECTION to SectionName. Loop through your stored name/value pairs, change the names and give the values with the new format, and make sure you add things like area as needed.

I have made a similar example to imitate the different arrangement and expression in those syntax files.
(a) I am ten years old
I love eating apple
I am a boy


(b) Apple is my favourite fruit
I am a male
I have birthed 10 years


I would like to convert from (a) to (b), may you show me the steps?
Reply
#4
kingsman Wrote:(a) I am ten years old
I love eating apple
I am a boy

(b) Apple is my favourite fruit
I am a male
I have birthed 10 years
Here is a basic algorithmic scheme that you could follow for such a work
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.
Reply
#5
(Dec-20-2019, 06:59 PM)Gribouillis Wrote:
kingsman Wrote:(a) I am ten years old
I love eating apple
I am a boy

(b) Apple is my favourite fruit
I am a male
I have birthed 10 years
Here is a basic algorithmic scheme that you could follow for such a work
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?
Reply
#6
If you don't mind using someone else's code, there is a module in pypi named 'text2digits' under the MIT licence that does the number conversion
>>> from text2digits import text2digits
>>> td2 = text2digits.Text2Digits()
>>> td2.convert("I'm twenty five years old")
"I'm 25 years old"
>>> td2.convert("seventy two")
'72'
Considering that you are a beginner in python, it would probably be a good idea to use this code instead of rewriting your own. You will probably meet many other issues before the project is complete.
Reply
#7
I am just curious how the initial project to parse [relatively] well structured construction software file formats and convert between them turned into natural language conversion/processing project :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(Dec-21-2019, 05:16 PM)Gribouillis Wrote: If you don't mind using someone else's code, there is a module in pypi named 'text2digits' under the MIT licence that does the number conversion
>>> from text2digits import text2digits
>>> td2 = text2digits.Text2Digits()
>>> td2.convert("I'm twenty five years old")
"I'm 25 years old"
>>> td2.convert("seventy two")
'72'
Considering that you are a beginner in python, it would probably be a good idea to use this code instead of rewriting your own. You will probably meet many other issues before the project is complete.

This is a fantastic module. The problem of last statement has been solved.
Then, I would like to use real syntax file in Etabs for conversion.
However, there is a new problem Confused
In my example, I know that there are 3 sentences and the position of the keywords so that I can extract the word and convert it.
The code I have used on the above is that I already know the file just had 3 statements so I append them into a list and assign a name for them.
But in Etabs, for example the storey statement, I don't know how many storeys are in the model previously so I can't use the code on the above.
So, how can I ensure that all the statements are extracted from the file?
Reply
#9
kingsman Wrote:So, how can I ensure that all the statements are extracted from the file?
That's the whole problem of parsing. You need to describe the statements of the input language as accurately as possible.

If the structure of the code is that of a sequence of statements with one statement per line, you can create an expandable list of python objects containing the data read from the file. For example if the form of the storey statement is

Output:
STOREY foo SPAM eggs HAM foo BAR qux
I would suggest to create a class

class Statement:
    def __init__(self, data):
        self.data = data
    def __repr__(self):
        return "{}({})".format(self.__class__.__name__, self.data)

class Storey(Statement):
    pass

statements = []
...
for line in File:
    data = line.split()
    if data[0] == 'STOREY':
        statements.append(Storey(data))
    ...
You would create such a class for every kind of statements and append instances to the statements list during the parsing step.

At first, concentrate only on the parsing step. Once it is done, the rest will be relatively easy.
Reply
#10
(Dec-22-2019, 10:58 AM)Gribouillis Wrote:
class Statement:
    def __init__(self, data):
        self.data = data
    def __repr__(self):
        return "{}({})".format(self.__class__.__name__, self.data)

I want to know that what is the meaning of __repr__ inside the class.
Also, how can it help me in this situation.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python format conversion bluefrog 2 2,700 Jul-22-2018, 03:49 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020