【问题标题】:How to remove the stranger word from text如何从文本中删除陌生单词
【发布时间】:2019-10-27 12:39:56
【问题描述】:

我正在建立一个情感分析的新项目,并想删除任何陌生的单词、字符、电子邮件或任何带有 @ 或任何 ling 空格的名称,清除文本中的任何噪音

input text ="@maggieNYT KFC must be out chicken.  This guy itأ?آ?أ?آ?أ?آ?s losing his shit."

input text ="‰??Aye babe. Why is Pizza hut calling you at 10 PM?‰?? "

input text ="The team will be in @KingstonLibrary tomorrow from 2:30 - 5:30pm. Providingأ?آپ#HIVأ?آپ/ #STI tests &أ?آپ#freeأ?آپcondoms, along with information & advice onأ?آپ#PrEP #contraceptionأ?آپ& otherأ?آپ#sexualhealthأ?آپissues.

【问题讨论】:

    标签: python preprocessor


    【解决方案1】:

    可以通过 python 中的 re 库使用 regular expressions 完成您的要求。您可以将正则表达式视为一种高级查找和替换功能。

    用户@Abijit 提供了一个正则表达式,它将执行this answer 中的相关任务。

    ...以下正则表达式只是删除 URL(不仅仅是 http)、任何标点符号、用户名或任何非字母数字字符。它还用一个空格分隔单词....

    这就是我的建议。

    ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",x).split())
    

    在您的示例字符串上对此进行测试,它似乎也适用于您的情况。这是我的代码。

    import re  # Python regex library
    original: str = input()
    # This following line uses @Abijit's regex
    cleaned: str = ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",original).split())
    print(cleaned)
    

    每一个的输出如下:

    • KFC must be out chicken This guy it s losing his shit
    • Aye babe Why is Pizza hut calling you at 10 PM
    • The team will be in tomorrow from 2 30 5 30pm Providing HIV STI tests amp free condoms along with information amp advice on PrEP contraception amp other sexualhealth issues

    【讨论】:

      猜你喜欢
      • 2016-01-19
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多