【问题标题】:re.split to remove url in input text preprocessingre.split 删除输入文本预处理中的 url
【发布时间】:2020-06-11 08:13:24
【问题描述】:

在之前的数据集中,我已经在 csv 文件中进行了文本预处理,所以我这样做了

df = pd.read_csv('dataset.csv', dtype=str).apply(lambda x: x.astype(str).str.lower())
#open and lower casing all the data 

#DELETE URL (HTTPS/HTTP)
df['DEL_URL'] = df['text'].apply(lambda x: re.split('https:\/\/.*|http:\/\/.*', str(x))[0])
#delete all the text in the columns called 'text' that have https://(anything) and delete all the text that have http://(anything)
df['DEL_URL'] = df['DEL_URL'].apply(lambda x: re.split('pic.tw*', str(x))[0]) 
#delete all the text in the columns 'DEL_URL' that have pic.tw(anything) because i have text that have pic.twitter sometimes

我在 csv 中作为输入的数据集是:

1. test this is my test https:///testing.com
2. test this is my test pic.twitter:///testing.com
output :
1. test this is my test
2. test this is my test

现在,我想在输入文本上使用小写字母并删除上面的 url,而不将其保存为 csv,所以我有这个代码来预测分类器并做一些特征选择 我运行代码,然后输入文本“测试这是我的测试https:///testing.compic.twitter/xxd123”

import re
##class
class prepro():
    def __init__(self):
        pass
    def text_pre(self,new_doc):
        lower_case = docs_new.lower()
        #print("lowercase: ",lower_case) #work fine it give me (test this is my test https:///testing.com pic.twitter/xxd123)
        del_url = re.split('https:\/\/.*|http:\/\/.*'," ",lower_case)
        print("delete https or http: ",del_url)  
        #output ['test this is my test ', ''] something wrong with that
        del_url2 = re.split('pic.tw*',del_url) #error 
        print ('del url 2: ',del_url2)#error
        #output error
cs = prepro()

new_doc = input('input text: ') 
#input a text example (test this is my test https:///testing.com)
process = cs.text_pre(new_doc)
x_newtfidf = tfidf.transform(process)  
selectionfeature= seleksi.transform(x_newtfidf) 
predicted = classifier.predict(selectionfeature)
print(predicted)

第一个问题为什么 del_rul 给我输出 ['测试这是我的测试','']我只希望像['测试这是我的测试'] 那为什么 del_url2 给我错误

Traceback (most recent call last):

  File "C:\Users\xd\OneDrive\Desktop\something\filename.py", line 180, in <module>
    proses = process.text_pre(new_doc)

  File "C:\Users\xd\OneDrive\Desktop\something\filename.py", line 154, in low
    del_url2 = re.split('pic.tw*',[del_url])

  File "E:\anaconda3\lib\re.py", line 213, in split
    return _compile(pattern, flags).split(string, maxsplit)

TypeError: expected string or bytes-like object```


【问题讨论】:

  • del_url 是一个列表。如果你将它作为第二个参数传递给 re.split,你会得到一个 TypeError。
  • 也许替换函数re.sub 会是这个用例的更好选择。您可以通过用空字符串替换它们来简单地删除 url,例如del_url = re.sub(r'https?://.*', '', lower_case).

标签: python python-3.x regex pandas


【解决方案1】:

TLDR:这是解决您的问题的有效解决方案: codeblock

import re

class prepro():
    def __init__(self):
        pass
    def text_pre(self,new_doc):
        lower_case = new_doc.lower()
        #print("lowercase: ",lower_case) #work fine it give me (test this is my test https:///testing.com pic.twitter/xxd123)
        del_url = re.split('https:\/\/.*|http:\/\/.*',lower_case)
        print("delete https or http: ",del_url)  
        del_url2 = re.split('pic.tw*',del_url[0])
        print ('del url 2: ',del_url2)
        return del_url2[0]

cs = prepro()
new_doc = 'test this is my test https:///testing.com pic.twitter/xxd123'
#new_doc = input('input text: ') 
process = cs.text_pre(new_doc)
x_newtfidf = tfidf.transform(process)  
selectionfeature= seleksi.transform(x_newtfidf) 
predicted = classifier.predict(selectionfeature)
print(predicted)

首先我想让你知道,我假设你打算使用: lower_case = new_doc.lower() 而不是 lower_case = docs_new.lower()

除此之外,del_url2 正确地产生了错误,因为 .split() 方法输入了一个 STRING 并返回一个由给定模式拆分的元素列表。 因此,根据您的代码,您首先将一个字符串拆分为列表,然后尝试拆分列表,这会正确产生错误。

相反,您应该访问列表的元素以进一步拆分,然后在第二次拆分操作之后您再次拥有一个列表。因此,您应该只返回与您相关的元素,如果是 del_url2[0]

我希望这会有所帮助。干杯

附言。我注意到的最后一件事是您使用了 del_url = re.split('https:\/\/.*|http:\/\/.*',"",lower_case),这会产生错误,因为小写字母是作为第三个参数传递的。

【讨论】:

    猜你喜欢
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 2019-12-16
    • 2019-02-27
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    相关资源
    最近更新 更多