[英]Split more than one word in python
如何用python编写一个可以拆分多个单词或字符的程序? 例如,我有这些句子: Hi, This is a test. Are you surprised?
Hi, This is a test. Are you surprised?
在此示例中,我需要我的程序将这些句子分隔为',','!','?' 和'。'。 我知道str
库和NLTK
split,但我需要知道是否有任何内部pythonic方式(例如split)?
使用re.split:
string = 'Hi, This is a test. Are you surprised?'
words = re.split('[,!?.]', string)
print(words)
[u'Hi', u' This is a test', u' Are you surprised', u'']
您正在寻找NLTK软件包的tokenize
功能。 NLTK
代表自然语言工具包
或尝试从re
模块re
re.split
。
从re doc。
>>> re.split('\W+', 'Words, words, words.')
['Words', 'words', 'words', '']
>>> re.split('(\W+)', 'Words, words, words.')
['Words', ', ', 'words', ', ', 'words', '.', '']
>>> re.split('\W+', 'Words, words, words.', 1)
['Words', 'words, words.']
>>> re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE)
['0', '3', '9']
我想我找到了一个棘手的方法。 我不需要为此使用任何模块。 我可以使用str库的replace
方法并替换类似的单词!
还是?
与.
。 然后,我可以使用split
方法对文本进行逐字分割.
。
def get_words(s):
l = []
w = ''
for c in s:
if c in '-!?,. ':
if w != '':
l.append(w)
w = ''
else:
w = w + c
if w != '':
l.append(w)
return l
>>> s = "Hi, This is a test. Are you surprised?"
>>> print get_words(s)
['Hi', 'This', 'is', 'a', 'test', 'Are', 'you', 'surprised']
If you change '-!?,. ' into '-!?,.'
The output will be:
['Hi', ' This is a test', ' Are you surprised']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.