0

I am trying to get start and end offset of paragraph of text file in python. I tried following code it gives the start and end offset but if paragraph starts with white space or tab then it will not consider it as paragraph.

  paraStartOffset = []
  paraEndOffset = []

  for match in re.finditer(r'(?s)((?:[^\n]?)+)', textFile):
      paraStartOffset.append(match.start())
      paraEndOffset.append(match.end())

  print "start Offset --> ",paraStartOffset
  print "end Offset --> ",paraEndOffset

Can someone guide me as where am I missing something. Thanks.

Dhairya
  • 743
  • 2
  • 11
  • 29

1 Answers1

0

I think this question / answer pretty much discusses what you are looking for. The code (taken from the answer) pretty much works if I test it also with leading whitespaces at the beginning of a paragraph.

for match in re.finditer(r'(?s)((?:[^\n][\n]?)+)', DATA):
    print match.start(), match.end()

It returns the following when I run it on my test text (taken from Bram Stoker's Dracula)First paragraph is a standard on. Second one starts with SPACES. The third one starts with TAB.

Result: (showing the start, end offsets of each paragraph)

0 630
631 1029
1030 1125

Test Text: (I cant get the format in exactly as raw but anyhow...)

_3 May. Bistritz._--Left Munich at 8:35 P. M., on 1st May, arriving at
Vienna early next morning; should have arrived at 6:46, but train was an
hour late. Buda-Pesth seems a wonderful place, from the glimpse which I
got of it from the train and the little I could walk through the
streets. I feared to go very far from the station, as we had arrived
late and would start as near the correct time as possible. The
impression I had was that we were leaving the West and entering the
East; the most western of splendid bridges over the Danube, which is
here of noble width and depth, took us among the traditions of Turkish
rule.

  "My Friend.--Welcome to the Carpathians. I am anxiously expecting
you. Sleep well to-night. At three to-morrow the diligence will
start for Bukovina; a place on it is kept for you. At the Borgo
Pass my carriage will await you and will bring you to me. I trust
that your journey from London has been a happy one, and that you
will enjoy your stay in my beautiful land.

    Just before I was leaving, the old lady came up to my room and said in a
very hysterical way:
Community
  • 1
  • 1
klaas
  • 1,661
  • 16
  • 24