#one annotation
modifiers = itemData.instantiateFromCSVtoitemData("/Users/alec/Box Sync/Bucher_Surgical_MIMICIII/pyConText_implement/modifiers.tsv")
targets = itemData.instantiateFromCSVtoitemData(
    "file:///Users/alec/Box Sync/Bucher_Surgical_MIMICIII/pyConText_implement/targets.tsv")

input_report = '/Users/alec/Box Sync/Bucher_Surgical_MIMICIII/Radiology_Annotation/Adjudication/Batch_3/corpus/Yes_28226_116465_05-29-93.txt'
report = ''
with open(input_report,'r') as f0:
    report += f0.read()
len(report)
report_name = os.path.basename(input_report)
report_name

context = fc.create_context_doc(report, modifiers, targets)

len(context.getSectionMarkups())

#importlib.reload(fc)

#Apply logic, classify tagObjects
annotations1=fc.fluid_collection_classifier(context,"textfile.txt")


annotation_strings = []
print(type(annotations1))
print(type(annotations1[0]))
for annotation in annotations1:
    annotation_strings.append(annotation.getXML())
    print(annotation.getMentionClass())
#definitive_mention = fc.fluid_collection_classifier(context)[0][0]
#definitive_mention.has_edge()
#Will this work to catch the appropriate anatomical modifiers?
#[x.getXML() for x in annotations1]


#print(type(definitive_mention))
#tagObject1 = definitive_mention.nodes()[0]
#tagObject1.getSpan()

"""
Explanation: fcFinder
I'm designing a function to work as a pipeline using the module fcFinder. Here is an outline of how the function works:
- As input, it takes:
    - a file name (the report)
    - an outpath to save the XML file
    - modifiers and targets as itemData objects

The function then follows this order, as demonstrated below:
Reads in the file
Creates a conText Document that is made up of markup objects
Classifies each tagObject as being either definitive evidence of fluid collection, negated evidence, or indication (need to get it to work for the relevant anatomic modifiers)
Creates mentionAnnotation objects out of those tabObjects
Writes one XML .txt.knowtator file. This still needs to be worked on so that it is compatible with eHOST, but the basic structure is there.
End of explanation
"""


#annotation1 = fcClasses.createAnnotations(definitive_mention,'fluid collection-definitive','Yes_28226_116465_05-29-93.txt')[0]
annotation1 = annotations1[0]
XML1 = annotation1.getXML()
print(XML1)

#XML1 = fcClasses.getXML(annotation1)

#print(XML1)
#importlib.reload(fc)

print(len(annotation_strings))
#print(len(fc.writeKnowtator(annotation_strings,report_name,os.path.join('..','XML_examples'))))
print(fc.writeKnowtator(annotation_strings,report_name,os.path.join('/Users/alec/Desktop/fcFinder_test','saved')))

"""
Explanation: XML
This part of the pipeline takes all of the annotations from above and writes it into a Knowtator XML file. It is not yet able to be imported into eHOST
End of explanation
"""


input_report2 = '/Users/alec/Box Sync/Adjudication/Batch_3/corpus/No_69411_129942_04-30-58.txt'
os.getcwd()

def fcFinder(input_report, output_dir, modifiers=modifiers, targets=targets):
    if not os.path.exists(output_dir):
        os.mkdir(os.path.join(output_dir,'saved'))
    report = ''
    with open(input_report,'r') as f0:
        report += f0.read()
    context = fc.create_context_doc(report, modifiers, targets)
    report_name = os.path.basename(input_report)
    annotations = fc.fluid_collection_classifier(context,input_report)
    XML_strings = [x.getXML() for x in annotations]
    return fc.writeKnowtator(XML_strings,report_name,os.path.join(output_dir,'saved'))

fcFinder(input_report,'/Users/alec/Desktop/fcFinder_test')

"""
Explanation: One pipelined function for one file
End of explanation
"""


### Need to work on this ###
def fcFinder_batches(batch_path, batch_schema,output_dir, targets=targets,modifiers=modifiers):
    batches = batch_path+'//%s'%batch_schema
    #return os.listdir(batch_path)
    listOfBatches = glob.glob(batches)
    counter = 0
    #return listOfBatches
    for batch in listOfBatches:
        #print(batch)
        corpus = os.path.join(batch,'corpus')
        for file in os.listdir(corpus):
            file_path = os.path.isfile(os.path.join(batch,corpus,file))
            #print(file)
            counter += 1
            if os.path.isfile(os.path.join(batch,corpus,file)):
                #print(file)
                if not os.path.exists(os.path.join(output_dir,batch)):
                    os.mkdir(os.path.exists(output_dir,bat))
                fcFinder(file_path,modifiers,targets,os.path.join(output_dir,batch))
                    
    return counter
    return output_dir
    return listOfBatches
fcFinder_batches('/Users/alec/Box Sync/Adjudication','Batch*','/Users/alec/Desktop/fcFinder_test')

fcFinder(input_report, modifiers, targets, os.getcwd())

fcFinder(input_report2, modifiers, targets, os.getcwd())

"""
Explanation: Batches of Documents (not ready yet)
End of explanation
"""


markup2 = context.getSectionMarkups()[0]
markup2

"""
Explanation: Catching anatomical modifiers and creating annotations out of them
End of explanation
"""


