Search code examples
postgresqlpython-2.7database-schemadjango-1.9

What is the best data structure to store chunks of a text that can be conditionally shown as part of a text file?


Let's set up the scenario. This is a text file:

INTRODUCTION

Paragraph 01
Paragraph 02
Paragraph 03

SECTION XXXXXX

Paragraph 01
Paragraph 02

SECTION YYYYYY

Paragraph 01

SECTION ZZZZZZ

Paragraph 01
Paragraph 02
Paragraph 03

Each paragraph can contain more paragraphs inside, but let's keep things simple.

We want to programatically build text files like those by following simple rules:

  • Sections are always present, containing at least one paragraph
  • Any paragraph can be shown or not based on one or more conditions. Conditions are defined as code evaluated against a context (think about eval function in Python, for example).
  • Context will be provided at runtime
  • Paragraphs can start with a number. So, we can not put the numbers as part of the paragraph (paragraphs can be present or not, and their numbers must follow a correct sequence: 1, 2, 3, etc)

The type of texts to be built are terms of use, privacy policy, etc. As simplification, legal texts containing more or less content based on web form responses.

EDIT: The text is generated separately from the forms. We only have the responses.

So, my approach is to store the chunks of text (paragraphs) as columns of a database. Each column along with:

  • Position inside its section
  • Code to evaluate as condition(s). The context to evaluate that code against will be provided at runtime, as said.

As we can have one or more conditions to evaluate to determine if a chunk of text will be included or not in the final text, I'm not sure about what kind of data structure to use.

Relational database? The number of columns would be dynamic, due to the existence of an initially unlimited number of conditions to be evaluated in each case.

NoSQL database?, storing the structure as a JSON, containing text + array of conditions?

Any other approach?


Solution

  • I solved the problem with a different approach.

    We build the text file in the frontend, by drag&dropping 2 types of widgets into a canvas (representing the content of the text file):

    • Simple text chunks
    • Conditional widgets: they'll show/hide a text chunk based on a condition evaluated in the frontend at runtime (that's the key). Something similar to Angular ng-if.

    Then we compile the full text, also in the frontend.

    So, the backend just stores chunks of text. No need to translate conditional logic to database. It even sounds silly now...