-1

I have a unicode encoded malayalam text file say a.txt. i am storing each word from that text file in to a list. so the list contains each word in the text.

example

text="അവള്‍ പൊട്ടിക്കരഞ്ഞുകൊണ്ട് നൈല്‍ നദീതീരം മുഴുവന്‍ തന്‍റെ
      കാമുകന്‍റെ ശരീരഭാഗങ്ങള്‍ക്കായി അലഞ്ഞുനടന്നു. ഒരുപക്ഷെ, മറെറാരു
      പുരാണ-ഐതിഹ്യ കാവ്യങ്ങളിലും ഇത്ര ഹൃദയസ്പര്‍ശിയായ ഒരു തിരച്ചിലിന്‍റെ
      കഥ വിവരിക്കപ്പെട്ടിട്ടുണ്ടാവില്ല." 

but the sentence boundaries are also attached to the last word in the sentence. like [അലഞ്ഞുനടന്നു.] i want to make it separate like [അലഞ്ഞുനടന്നു] [.] [വിവരിക്കപ്പെട്ടിട്ടുണ്ടാവില്ല] [.]

I am doing like this

with codecs.open(r"C:\Users\cusat\Documents\Python Scripts\test document.txt",encoding="utf-8") as fpnr:
        text=fpnr.read() 
        text_of_sentences=segmentize(text)
        fpnr.close()  

    for sentence in text_of_sentences:
        if len(sentence) > 1:
            sentences.append(worder(sentence))
            #print sentences


    for sentence in sentences:
        #print sentence
        for word in sentence:
            #print word
            trimdwrds=trim(word)
            wordses.append(trimdwrds)
    for word in wordses:
           if len(word) >= 1:
               re.sub(r'([\u0900-\u097F]+)(\.)(\s*)', r'\1 \2', word)  
BioGeek
  • 21,897
  • 23
  • 83
  • 145
ajees
  • 33
  • 3

1 Answers1

2
import re

words = [word for word in re.split('\s|(\.)', text) if word]

print(words)

['അവള്', 'പൊട്ടിക്കരഞ്ഞുകൊണ്ട്', 'നൈല്', 'നദീതീരം', 'മുഴുവന്', 'തന്റെ', 'കാമുകന്റെ', 'ശരീരഭാഗങ്ങള്ക്കായി', 'അലഞ്ഞുനടന്നു', '.', 'ഒരുപക്ഷെ,', 'മറെറാരു', 'പുരാണ-ഐതിഹ്യ', 'കാവ്യങ്ങളിലും', 'ഇത്ര', 'ഹൃദയസ്പര്ശിയായ', 'ഒരു', 'തിരച്ചിലിന്റെ', 'കഥ', 'വിവരിക്കപ്പെട്ടിട്ടുണ്ടാവില്ല', '.']

This splits your text string by either whitespace or . but it keeps the . since it is in a capturing group in the regex. Then it filters out the empty strings from the split.

bunji
  • 5,063
  • 1
  • 17
  • 36