1

I'm trying to separate dots in German sentences from words but not from digits, e.g.:

"Der 17. Januar war ein toller Tag. Heute ist es auch schön."

should end in

"Der 17. Januar war ein toller Tag . Heute ist es auch schön . "

But I can't find a solution for this. I tried to use the re module in Python without success.

line = re.sub(r'[^0-9]+\.', ' . ', line)

would just end in

"Der 17. Januar war ein toller Ta . Heute ist es auch schö . "
phd
  • 82,685
  • 13
  • 120
  • 165
ke_let
  • 71
  • 1
  • 5
  • Maybe an XY-problem. If this is supposed to be part of an NLP pipeline, you should use a proper tokenizer. Like in [NLTK](https://stackoverflow.com/a/15057966/1346276) or [spacy](https://spacy.io/docs/usage/processing-text) (I know that spacy comes with a German model built-in; not sure about NLTK.). – phipsgabler Oct 24 '17 at 15:36

2 Answers2

2

You have to use a positive lookbehind in your regex:

import re
s = "Der 17. Januar war ein toller Tag. Heute ist es auch schön."
final_string = re.sub("(?<=[a-zA-Z])\.(\s|$)", ' . ', s)
print(final_string)

Output:

Der 17. Januar war ein toller Tag . Heute ist es auch schön .
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • Thanks! The regex did work very well and in addition I could easily modify it for a few similar problems :) – ke_let Oct 26 '17 at 07:40
1

Just in case, you don't want to use regex. Here is an alternative.

def tokenize_using_dot(s_input):
    s_list = s_input.split()

    for idx in range(len(s_list)):
        if s_list[idx][-1] == '.' and not s_list[idx][0:-1].isdigit():
            s_list[idx] = s_list[idx].replace('.', ' .')
    return' '.join(s_list)


s = "Der 17. Januar war ein toller Tag. Heute ist es auch schön."
print(tokenize_using_dot(s))

output:

 Der 17. Januar war ein toller Tag . Heute ist es auch schön .

As @phg commented, it would be a good idea to use a proper tokenizer from nltk suit for these type of tasks.

utengr
  • 3,225
  • 3
  • 29
  • 68