程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了无法借助 nltk 的正则表达式删除特殊字符大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决无法借助 nltk 的正则表达式删除特殊字符?

开发过程中遇到无法借助 nltk 的正则表达式删除特殊字符的问题如何解决?下面主要结合日常开发的经验,给出你关于无法借助 nltk 的正则表达式删除特殊字符的解决方法建议,希望对你解决无法借助 nltk 的正则表达式删除特殊字符有所启发或帮助;

在正则表达式和 nltk 的帮助下无法删除特殊字符。

代码是

X,y = data.comments,data.sentiment

关注

documents = []

from nltk.stem import WordNetLemmatizer

stemmer = WordNetLemmatizer()

for sen in range(0,len(X)):
    # Remove all the special characters
    document = re.sub(r'\W',' ',str(X[sen]))
    
    # remove all single characters
    document = re.sub(r'\s+[a-zA-Z]\s+',document)
    
    # Remove single characters from the start
    document = re.sub(r'\^[a-zA-Z]\s+',document) 
    
    # Substituting multiple spaces with single space
    document = re.sub(r'\s+',document,flags=re.I)
    
    # Removing prefixed 'b'
    document = re.sub(r'^b\s+','',document)
    
    # Converting to Lowercase
    document = document.lower()
    
    # Lemmatization
    document = document.split()

    document = [stemmer.lemmatize(word) for word in document]
    document = ' '.join(document)
    
    documents.append(document)

返回的错误如下

KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self,key,method,tolerance)
   3079             try:
-> 3080                 return self._engine.get_loc(casted_key)
   3081             except KeyError as err:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64Hashtable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64Hashtable.get_item()

KeyError: 9

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-16-f9320c54a8cb> in <module>
      7 for sen in range(0,len(X)):
      8     # Remove all the special characters
----> 9     document = re.sub(r'\W',str(X[sen]))
     10 
     11     # remove all single characters

~\anaconda3\lib\site-packages\pandas\core\serIEs.py in __getitem__(self,key)
    822 
    823         elif key_is_scalar:
--> 824             return self._get_value(key)
    825 
    826         if is_hashable(key):

~\anaconda3\lib\site-packages\pandas\core\serIEs.py in _get_value(self,label,takeable)
    930 
    931         # Similar to Index.get_value,but we do not fall back to positional
--> 932         loc = self.index.get_loc(label)
    933         return self.index._get_values_for_loc(self,loc,label)
    934 

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self,tolerance)
   3080                 return self._engine.get_loc(casted_key)
   3081             except KeyError as err:
-> 3082                 raise KeyError(key) from err
   3083 
   3084         if tolerance is not None:

KeyError: 9

我不知道为什么它不起作用,仅删除特殊字符代码不起作用。我正在尝试清理数据,目标变量是 y,问题案例是二元分类。仅删除特殊字符不起作用。

解决方法

将此行上的索引从纯切片更改为iloc

document = re.sub(r'\W',' ',str(X.iloc[sen]))

大佬总结

以上是大佬教程为你收集整理的无法借助 nltk 的正则表达式删除特殊字符全部内容,希望文章能够帮你解决无法借助 nltk 的正则表达式删除特殊字符所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: