我有很少的短信(SMS),我想用句点('。')作为分隔符对它们进行分段。我无法处理以下类型的消息。如何在Python中使用Regex对这些消息进行分段。
分割前:
'hyper count 16.8mmol/l.plz review b4 5pm.just to inform u.thank u' 'no of beds 8.please inform person in-charge.tq'
分割后:
'hyper count 16.8mmol/l' 'plz review b4 5pm' 'just to inform u' 'thank u' 'no of beds 8' 'please inform person in-charge' 'tq'
每一行都是单独的消息
更新:
我正在进行自然语言处理,我认为可以将'16.8mmmol/l'
和'no of beds 8.2 cups of tea.'
视为一样。 80%的准确度对我来说已足够,但我希望尽可能减少False Positive
。
答案 0 :(得分:5)
几个星期前,我搜索了一个正则表达式,它会捕获表示字符串中数字的每个字符串,无论数字的编写形式如何,即使是科学记数法中的数字,甚至是带有逗号的印度数字:see { {3}}
我在以下代码中使用此正则表达式来解决您的问题。
与其他答案相反,在我的解决方案中,'8。'中的点不被视为必须进行拆分的点,因为它可以被读作浮点数点后没有数字。
import re
regx = re.compile('(?<![\d.])(?!\.\.)'
'(?<![\d.][eE][+-])(?<![\d.][eE])(?<!\d[.,])'
'' #---------------------------------
'([+-]?)'
'(?![\d,]*?\.[\d,]*?\.[\d,]*?)'
'(?:0|,(?=0)|(?<!\d),)*'
'(?:'
'((?:\d(?!\.[1-9])|,(?=\d))+)[.,]?'
'|\.(0)'
'|((?<!\.)\.\d+?)'
'|([\d,]+\.\d+?))'
'0*'
'' #---------------------------------
'(?:'
'([eE][+-]?)(?:0|,(?=0))*'
'(?:'
'(?!0+(?=\D|\Z))((?:\d(?!\.[1-9])|,(?=\d))+)[.,]?'
'|((?<!\.)\.(?!0+(?=\D|\Z))\d+?)'
'|([\d,]+\.(?!0+(?=\D|\Z))\d+?))'
'0*'
')?'
'' #---------------------------------
'(?![.,]?\d)')
simpler_regex = re.compile('(?<![\d.])0*(?:'
'(\d+)\.?|\.(0)'
'|(\.\d+?)|(\d+\.\d+?)'
')0*(?![\d.])')
def split_outnumb(string, regx=regx, a=0):
excluded_pos = [x for mat in regx.finditer(string) for x in range(*mat.span()) if string[x]=='.']
li = []
for xdot in (x for x,c in enumerate(string) if c=='.' and x not in excluded_pos):
li.append(string[a:xdot])
a = xdot + 1
li.append(string[a:])
return li
for sentence in ('hyper count 16.8mmol/l.plz review b4 5pm.just to inform u.thank u',
'no of beds 8.please inform person in-charge.tq',
'no of beds 8.2 cups of tea.tarabada',
'this number .977 is a float',
'numbers 214.21E+45 , 478945.E-201 and .12478E+02 are in scientific.notation',
'an indian number 12,45,782.258 in this.sentence and 45,78,325. is another',
'no dot in this sentence',
''):
print 'sentence =',sentence
print 'splitted eyquem =',split_outnumb(sentence)
print 'splitted eyqu 2 =',split_outnumb(sentence,regx=simpler_regex)
print 'splitted gurney =',re.split(r"\.(?!\d)", sentence)
print 'splitted stema =',re.split('(?<!\d)\.|\.(?!\d)',sentence)
print
结果
sentence = hyper count 16.8mmol/l.plz review b4 5pm.just to inform u.thank u
splitted eyquem = ['hyper count 16.8mmol/l', 'plz review b4 5pm', 'just to inform u', 'thank u']
splitted eyqu 2 = ['hyper count 16.8mmol/l', 'plz review b4 5pm', 'just to inform u', 'thank u']
splitted gurney = ['hyper count 16.8mmol/l', 'plz review b4 5pm', 'just to inform u', 'thank u']
splitted stema = ['hyper count 16.8mmol/l', 'plz review b4 5pm', 'just to inform u', 'thank u']
sentence = no of beds 8.please inform person in-charge.tq
splitted eyquem = ['no of beds 8.please inform person in-charge', 'tq']
splitted eyqu 2 = ['no of beds 8.please inform person in-charge', 'tq']
splitted gurney = ['no of beds 8', 'please inform person in-charge', 'tq']
splitted stema = ['no of beds 8', 'please inform person in-charge', 'tq']
sentence = no of beds 8.2 cups of tea.tarabada
splitted eyquem = ['no of beds 8.2 cups of tea', 'tarabada']
splitted eyqu 2 = ['no of beds 8.2 cups of tea', 'tarabada']
splitted gurney = ['no of beds 8.2 cups of tea', 'tarabada']
splitted stema = ['no of beds 8.2 cups of tea', 'tarabada']
sentence = this number .977 is a float
splitted eyquem = ['this number .977 is a float']
splitted eyqu 2 = ['this number .977 is a float']
splitted gurney = ['this number .977 is a float']
splitted stema = ['this number ', '977 is a float']
sentence = numbers 214.21E+45 , 478945.E-201 and .12478E+02 are in scientific.notation
splitted eyquem = ['numbers 214.21E+45 , 478945.E-201 and .12478E+02 are in scientific', 'notation']
splitted eyqu 2 = ['numbers 214.21E+45 , 478945.E-201 and .12478E+02 are in scientific', 'notation']
splitted gurney = ['numbers 214.21E+45 , 478945', 'E-201 and .12478E+02 are in scientific', 'notation']
splitted stema = ['numbers 214.21E+45 , 478945', 'E-201 and ', '12478E+02 are in scientific', 'notation']
sentence = an indian number 12,45,782.258 in this.sentence and 45,78,325. is another
splitted eyquem = ['an indian number 12,45,782.258 in this', 'sentence and 45,78,325. is another']
splitted eyqu 2 = ['an indian number 12,45,782.258 in this', 'sentence and 45,78,325. is another']
splitted gurney = ['an indian number 12,45,782.258 in this', 'sentence and 45,78,325', ' is another']
splitted stema = ['an indian number 12,45,782.258 in this', 'sentence and 45,78,325', ' is another']
sentence = no dot in this sentence
splitted eyquem = ['no dot in this sentence']
splitted eyqu 2 = ['no dot in this sentence']
splitted gurney = ['no dot in this sentence']
splitted stema = ['no dot in this sentence']
sentence =
splitted eyquem = ['']
splitted eyqu 2 = ['']
splitted gurney = ['']
splitted stema = ['']
我添加了一个 simpler_regex 检测数字,来自this thread的我的帖子
我没有用科学记数法检测出印度数字和数字,但它实际上给出了相同的结果
答案 1 :(得分:2)
你可以使用负前瞻断言来匹配“。”没有后跟数字,并使用re.split
:
>>> import re
>>> splitter = r"\.(?!\d)"
>>> s = 'hyper count 16.8mmol/l.plz review b4 5pm.just to inform u.thank u'
>>> re.split(splitter, s)
['hyper count 16.8mmol/l', 'plz review b4 5pm', 'just to inform u', 'thank u']
>>> s = 'no of beds 8.please inform person in-charge.tq'
>>> re.split(splitter, s)
['no of beds 8', 'please inform person in-charge', 'tq']
答案 2 :(得分:1)
怎么样?
re.split('(?<!\d)\.|\.(?!\d)', 'hyper count 16.8mmol/l.plz review b4 5pm.just to inform u.thank u')
外观确保在一侧或另一侧不是数字。所以这也涵盖了16.8
案例。如果两边都有数字,则表达式不会拆分。
答案 3 :(得分:0)
这取决于您的确切句子,但您可以尝试:
.*?[a-zA-Z0-9]\.(?!\d)
看看是否有效。这将保留在引号中,但您可以根据需要将其删除。
答案 4 :(得分:-1)
"...".split(".")
split
是一个Python内置函数,用于分隔特定字符的字符串。