import sys
import json
from indra.sources.trips.drum_reader import DrumReader
from indra.sources.trips import process_xml

def read_content(content):
    sentences = []
    for k, v in content.items():
        sentences += v
    dr = DrumReader(to_read=sentences)
    try:
        dr.start()
    except SystemExit:
        pass
    statements = []
    for extraction in dr.extractions:
        statements += process_xml(extraction).statements
    return statements

if __name__ == '__main__':
    file_name = sys.argv[1]
    with open(file_name, 'rt') as fh:
        content = json.load(fh)
    statements = read_content(content)
    print(statements)

import sys
import json
import time
import pickle
from indra.sources.trips import process_xml
from indra.sources.trips.drum_reader import DrumReader


def set_pmid(statements, pmid):
    for stmt in statements:
        for evidence in stmt.evidence:
            evidence.pmid = pmid


def read_content(content, host):
    all_statements = []
    for pmid, sentences in content.items():
        print('================================')
        print('Processing %d sentences for %s' % (len(sentences), pmid))
        ts = time.time()
        dr = DrumReader(to_read=sentences, host=host)
        try:
            dr.start()
        except SystemExit:
            pass
        statements = []
        for extraction in dr.extractions:
            tp = process_xml(extraction)
            statements += tp.statements
        set_pmid(statements, pmid)
        te = time.time()
        print('Reading took %d seconds and produced %d Statements.' %
              (te-ts, len(statements)))
        all_statements += statements
    return all_statements


def save_results(statements, out_fname):
    with open(out_fname, 'wb') as fh:
        pickle.dump(statements, fh)


if __name__ == '__main__':
    host = sys.argv[1]
    file_name = sys.argv[2]
    with open(file_name, 'rt') as fh:
        content = json.load(fh)
    statements = read_content(content, host)
    save_results(statements, 'results.pkl')

Improve batch Drum reading implementation
Improve batch Drum reading implementation

  import sys
  import json
+ import time
+ import pickle
+ from indra.sources.trips import process_xml
  from indra.sources.trips.drum_reader import DrumReader
- from indra.sources.trips import process_xml
  
+ 
+ def set_pmid(statements, pmid):
+     for stmt in statements:
+         for evidence in stmt.evidence:
+             evidence.pmid = pmid
+ 
+ 
- def read_content(content):
+ def read_content(content, host):
-     sentences = []
+     all_statements = []
-     for k, v in content.items():
+     for pmid, sentences in content.items():
-         sentences += v
+         print('================================')
+         print('Processing %d sentences for %s' % (len(sentences), pmid))
+         ts = time.time()
-     dr = DrumReader(to_read=sentences)
+         dr = DrumReader(to_read=sentences, host=host)
-     try:
+         try:
-         dr.start()
+             dr.start()
-     except SystemExit:
+         except SystemExit:
-         pass
+             pass
-     statements = []
+         statements = []
-     for extraction in dr.extractions:
+         for extraction in dr.extractions:
-         statements += process_xml(extraction).statements
+             tp = process_xml(extraction)
+             statements += tp.statements
+         set_pmid(statements, pmid)
+         te = time.time()
+         print('Reading took %d seconds and produced %d Statements.' %
+               (te-ts, len(statements)))
+         all_statements += statements
-     return statements
+     return all_statements
+ 
+ 
+ def save_results(statements, out_fname):
+     with open(out_fname, 'wb') as fh:
+         pickle.dump(statements, fh)
+ 
  
  if __name__ == '__main__':
+     host = sys.argv[1]
-     file_name = sys.argv[1]
+     file_name = sys.argv[2]
      with open(file_name, 'rt') as fh:
          content = json.load(fh)
-     statements = read_content(content)
+     statements = read_content(content, host)
-     print(statements)
+     save_results(statements, 'results.pkl')
  
Improve batch Drum reading implementation
## Code Before:
import sys
import json
from indra.sources.trips.drum_reader import DrumReader
from indra.sources.trips import process_xml

def read_content(content):
    sentences = []
    for k, v in content.items():
        sentences += v
    dr = DrumReader(to_read=sentences)
    try:
        dr.start()
    except SystemExit:
        pass
    statements = []
    for extraction in dr.extractions:
        statements += process_xml(extraction).statements
    return statements

if __name__ == '__main__':
    file_name = sys.argv[1]
    with open(file_name, 'rt') as fh:
        content = json.load(fh)
    statements = read_content(content)
    print(statements)

## Instruction:
Improve batch Drum reading implementation
## Code After:
import sys
import json
import time
import pickle
from indra.sources.trips import process_xml
from indra.sources.trips.drum_reader import DrumReader


def set_pmid(statements, pmid):
    for stmt in statements:
        for evidence in stmt.evidence:
            evidence.pmid = pmid


def read_content(content, host):
    all_statements = []
    for pmid, sentences in content.items():
        print('================================')
        print('Processing %d sentences for %s' % (len(sentences), pmid))
        ts = time.time()
        dr = DrumReader(to_read=sentences, host=host)
        try:
            dr.start()
        except SystemExit:
            pass
        statements = []
        for extraction in dr.extractions:
            tp = process_xml(extraction)
            statements += tp.statements
        set_pmid(statements, pmid)
        te = time.time()
        print('Reading took %d seconds and produced %d Statements.' %
              (te-ts, len(statements)))
        all_statements += statements
    return all_statements


def save_results(statements, out_fname):
    with open(out_fname, 'wb') as fh:
        pickle.dump(statements, fh)


if __name__ == '__main__':
    host = sys.argv[1]
    file_name = sys.argv[2]
    with open(file_name, 'rt') as fh:
        content = json.load(fh)
    statements = read_content(content, host)
    save_results(statements, 'results.pkl')
