在 NLP 中预处理数据时如何处理文本数据中的 URL 链接

How to handle URL links in text data while preprocessing data in NLP

我有一个数据框,其中有一列包含 URL 链接。有人能告诉我在 NLP 中预处理数据时如何处理这些链接吗? 例如,df 列看起来类似于此-

  likes      text 
   11        https://www.facebook.com
   12        https://www.facebook.com
   13        https://www.facebook.com
   14        Good morning
   15        How are.....you?

我们需要完全删除这些 URL 链接还是有其他方法来处理它们?

过滤掉 URL,因为它们不是自然语言。 写这样的谓词应该不会太难, 也许像 str(word).startswith('http') 这样简单的东西就足够了。 或者使用正则表达式:

import re


url_re = re.compile(r'^https?://', re.IGNORECASE)


def is_url(word):
    return url_re.search(word) is not None


def keep_row(row):
    return not is_url(row['text'])


df = df[df.apply(keep_row, axis=1)]