Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing logic from text

I have a program which recieves inputs in the form of text for example :

IF (A.4.1-1/1 OR A.4.1-1/2) AND A.4.4-1/9 AND (A.4.4-1/12 OR A.4.4-1/13 OR A.4.4-1/14 OR A.4.4-1/15) THEN R ELSE N/A

where A.4.1-1/1 etc. are variables with a value TRUE or FALSE. so far i have parsed up the text into the logical parts for the above example I have a list that looks like this:

['IF', '(', 'A.4.1-1/1', 'OR', 'A.4.1-1/2', ')', 'AND', 'A.4.4-1/9', 'AND', '(', 'A.4.4-1/12', 'OR', 'A.4.4-1/13', 'OR', 'A.4.4-1/14', 'OR', 'A.4.4-1/15', ')', 'THEN', 'R', 'ELSE', 'N/A']

I am just wondering is it possible to actually perform the logic on this list like combine all this up into the rquired python statements and provide the result. I am not sure were to start I have read on some sites that I should use a top down parser??

like image 753
frank johnson Avatar asked Jun 21 '11 19:06

frank johnson


People also ask

What does “implementing a logical model” mean?

In the current context, “implementing” really means working through the logical model’s components. By identifying identities, relationships, and attributes, you’ll reveal flaws that could produce anomalies in a working application: Multiple entities that represent the same conceptual entity

How was the text structure strategy developed?

The Text Structure Strategy was designed, developed, and refined through many years of research. After the initial identification of the five text structures, Meyer and colleagues conducted additional research about what and how good readers remembered information (Meyer, Brandt, & Bluth, 1980).

How does the content structure of a text affect recall?

The information presented higher in the content structure of a text is connected to better recall than information presented lower in the content structure (Meyer, 1975). Meyer and colleagues found that the hierarchical structure of texts fit into one or a combination of two or more of five specific text structures:

What is logical modeling in SQL Server?

Logical: Construct a model of the data using a specific model, but without consideration for the actual database system that will eventually store the data and run the application. Since SQL Server is a relational database management system (RDBMS), we’ll rely on the Entity-Relationship (ER) model.


2 Answers

This sounds like a task for Pyparsing:

The pyparsing module is an alternative approach to creating and executing simple grammars, vs. the traditional lex/yacc approach, or the use of regular expressions. The pyparsing module provides a library of classes that client code uses to construct the grammar directly in Python code.

You will be able to quickly define your grammar (instead of playing with regular expressions) and specific parsing actions. I have built very rich mini-languages using Pyparsing in under 300 lines of code.

like image 196
Escualo Avatar answered Sep 16 '22 20:09

Escualo


I'm not a Python guy, but I've done similar things in Java using JavaCC. What you'll want to do is write a grammar for your language (in a format such as EBNF, but it depends on the parser generator), then use a program like JavaCC to generate a parser for it, which will give you a parse tree that is more convenient to manipulate.

You should be able to find many useful examples, since the grammar of your input doesn't look too unusual (boolean operators, parenthesized expressions, and if-then-else statements are probably some of the most common use-cases for this).

You might find one of the Python libraries listed on this page useful.

like image 27
Kaypro II Avatar answered Sep 19 '22 20:09

Kaypro II