r/dailyprogrammer_ideas • • Jan 07 '19

[intermediate] Scriptio continua

Description --Use a dictionary to put spaces between the words in a piece of Scriptio continua--

Input description --A string with no spaces--

itdidsoindeedandmuchsoonerthanshehadexpectedbeforeshehaddrunkhalfthebottleshefoundherheadpressingagainsttheceilingandhadtostooptosaveherneckfrombeingbrokenshehastilyputdownthebottlesayingtoherselfthatsquiteenoughihopeishallnotgrowanymoreasitisicantgetoutatthedooridowishihadnotdrunkquitesomuch

Output description --A string with spaces between words--

it did so indeed and much sooner than she had expected before she had drunk half the bottle she found her head pressing against the ceiling and had to stoop to save her neck from being broken she hastily put down the bottle saying to herself thats quite enough i hope i shall not grow any more as it is i cant get out at the door i do wish i had not drunk quite so much

Notes/Hints --A trie can be used to implement this efficiently--

5 Upvotes

8 comments sorted by

View all comments

Show parent comment

1

u/tomekanco Jan 16 '19 edited Jan 16 '19

I guess it wouldn't be too hard

words = '[a-zA-Z]+[a-zA-Z]?'
sentences = '[a-zA-Z]+[^,.!?a-zA-Z]?|[,.!?]'

import re
from antigravity import trie
from collections import defaultdict

def parse_to_edge_cost(inx):
    L = [x.lower() for x in re.findall(words,inx)]
    edges = defaultdict(int)
    word_count = defaultdict(int)
    for (x,y) in zip(L,L[1:]):
        if x not in word_count:
            trie.add(x)
        edges[(x,y)]  += 1
        word_count[x] += 1
    return word_count, edges, trie

You end up with a weighted directed graph.

To apply to this problem:

Build the trie, edges and word_count from a language sample. The trie also contains all the words which are prefixes of others. Comparing these to the word_count will already give you a strong indication of preferred path. The edges can be used to do a neighborhood search.

To add some sugar, you could add approximations for adding capitalization and punctuation.