Python NLTK 删除不属于 URL 的内部标点符号
Python NLTK Remove Internal Punctuation NOT Part of a URL
我在 Python 中使用 NLTK,我在尝试从文本中删除内部标点符号时遇到问题,因为在句点之后应该有 space新句子。
这里有几个例子:
'on.How'
'time.Jerry'
'me?What'
如何删除前面示例中的标点符号,但仍保留 whosebug.com 或 nltk.org 等网址中的标点符号?
谢谢!
嗯,您可以使用 Spacy 提取 URLs。然后你可以在列表中使用 URLs 并使用简单的 python 你可以完成你的工作。假设我们在名为 URL...
的列表中获取了 URL
sentence = "hi.hello.whosebug.com or nltk.org"
URL = ["whosebug.com", "nltk.org"]
# to remove URLs from our sentence and place a number so we can later replace
# our URLs there
x=0
for i in URL:
sentence = sentence.replace(i,str(x))
x+=1
# put period with space
sentence=sentence.split(".")
sentence = ". ".join(sentence)
# replace our URLs in the correct place
x=0
for i in URL:
sentence = sentence.replace(str(x),i)
x+=1
print(sentence)
# output - "hi. hello. whosebug.com or nltk.org"
我在 Python 中使用 NLTK,我在尝试从文本中删除内部标点符号时遇到问题,因为在句点之后应该有 space新句子。
这里有几个例子:
'on.How'
'time.Jerry'
'me?What'
如何删除前面示例中的标点符号,但仍保留 whosebug.com 或 nltk.org 等网址中的标点符号?
谢谢!
嗯,您可以使用 Spacy 提取 URLs。然后你可以在列表中使用 URLs 并使用简单的 python 你可以完成你的工作。假设我们在名为 URL...
的列表中获取了 URLsentence = "hi.hello.whosebug.com or nltk.org"
URL = ["whosebug.com", "nltk.org"]
# to remove URLs from our sentence and place a number so we can later replace
# our URLs there
x=0
for i in URL:
sentence = sentence.replace(i,str(x))
x+=1
# put period with space
sentence=sentence.split(".")
sentence = ". ".join(sentence)
# replace our URLs in the correct place
x=0
for i in URL:
sentence = sentence.replace(str(x),i)
x+=1
print(sentence)
# output - "hi. hello. whosebug.com or nltk.org"