# import the modules we need
import os
import re

os.listdir('data')

data = open('data/LexisNexis practice.TXT').read()

# 5 of 54 DOCUMENTS
data.count('of 54 DOCUMENTS')

'This is a string of words'.split('i')

docs = data.split('of 54 DOCUMENTS')

len(docs)

for dnum in [1,2,3,4]:
    print('This is doc number {}'.format(dnum))

docs[0]

data = open('data/LexisNexis practice.TXT').read()

docs = data.split('of 54 DOCUMENTS')

dnum=1
for doc in docs[1:]:
    # open a new file
    output = open('data/doc{}.txt'.format(dnum), 'w')
    # write the document to the file
    output.write(doc)
    dnum=dnum+1


print(docs[3])

for doc in docs[1:]:
    print(doc.count('LANGUAGE:'))

# body is the text between LENGTH: number and LANGUAGE:

start = docs[1].find('LENGTH:')
end = docs[1].find('LANGUAGE:')

body = docs[1][start:end]

doc_body
for doc in docs[1:]:
    start = doc.find('LENGTH:')
    end = doc.find('LANGUAGE:')
    doc_body.append(doc[start:end])

for doc in docs[1:]:
    start = doc.find('LENGTH:')
    end = doc.find('LANGUAGE:')
    pre_body = doc[:start]
    body = doc[start:end]
    post_body = doc[end:]

import csv

a=[1,2,3,4,'abds','adsds']  # list

a[0]

a[4]

a[2:5]

"""
Explanation: Processing LexisNexus results
End of explanation
"""


d1 = { 'item1': 'This is item 1', 'item2': 'This is item 2'}

d1

d1['item2']

d1['item1']

d1['item3'] = a

d1

d1['item3']

d1['item3'][2]

"""
Explanation: Dictionary
name : value - pairs
key : value - pair
End of explanation
"""


data = [
   { 'col1': 1, 'col2': 't', 'col3': 2},   
    { 'col1': 2, 'col2': 'x', 'col3': 5},
    { 'col1': 3, 'col2': 'a', 'col3': 5}
]

data

with open('data/test.csv', 'w') as outfile:
    out = csv.DictWriter(outfile, 
                         fieldnames=['col1','col2','col3'])
    out.writeheader()
    out.writerows(data)

open('data/test.txt','w')

fh = open('data/test.csv')
for row in csv.DictReader(fh):
    print(row)

csv_data = [r for r in csv.DictReader(open('data/test.csv'))]

csv_data

csv_data[0]

csv_data[1]['col2']

doc_data = []

for doc in docs[1:]:
    start = doc.find('LENGTH:')
    end = doc.find('LANGUAGE:')
    pre_body = doc[:start]
    body = doc[start:end]
    post_body = doc[end:]
    
    row_dict = { 'pre_body': pre_body, 
                 'body': body,
                 'post_body': post_body }
    
    doc_data.append(row_dict)
    

print(doc_data[2]['pre_body'])

with open('data/docs.csv', 'w') as outfile:
    out = csv.DictWriter(outfile, 
                         fieldnames=['pre_body','body','post_body'])
    out.writeheader()
    out.writerows(doc_data)

"""
Explanation: Spreadsheet structure
       col1 col2 col3
row1     1   t    2
row2     2   x    5
row3     3   a    5
End of explanation
"""


re.findall('[A-Z]+:',doc, re.MULTILINE)

print('\n\n=====\n\n'.join(doc.split('\n\n')))

"""
Explanation: Some regular expression matching
End of explanation
"""


