如何从我的 CSV 文件中删除除 'VBD' 和 'VBN' 之外的所有 POS 标签?
How can I remove all POS tags except for 'VBD' and 'VBN' from my CSV file?
我想从我的 CSV 文件中删除标有特定词性标签 VBD
和 VBN
的词。但是,我在输入以下代码后收到错误 "IndexError: list index out of range":
for word in POS_tag_text_clean:
if word[1] !='VBD' and word[1] !='VBN':
words.append(word[0])
我的 CSV 文件有 10 条评论,来自 10 个人,行名称是 Comment
。
这是我的完整代码:
df_Comment = pd.read_csv("myfile.csv")
def clean(text):
stop = set(stopwords.words('english'))
exclude = set(string.punctuation)
lemma = WordNetLemmatizer()
tagged = nltk.pos_tag(text)
text = text.rstrip()
text = re.sub(r'[^a-zA-Z]', ' ', text)
stop_free = " ".join([i for i in text.lower().split() if((i not in stop) and (not i.isdigit()))])
punc_free = ''.join(ch for ch in stop_free if ch not in exclude)
normalized = " ".join(lemma.lemmatize(word) for word in punc_free.split())
return normalized
text_clean = []
for text in df)Comment['Comment']:
text_clean.append(clean(text).split())
print(text_clean)
POS_tag_text_clean = [nltk.pos_tag(t) for t in text_clean]
print(POS_tag_text_clean)
words=[]
for word in POS_tag_text_clean:
if word[1] !='VBD' and word[1] !='VBN':
words.append(word[0])
如何修复错误?
如果没有示例和相应的输出,很难理解您的问题,但可能是这样的:
假设 text
是一个字符串,text_clean
将是一个字符串列表的列表,其中每个字符串代表一个单词。在词性标注之后,POS_tag_text_clean
因此将是元组列表的列表,每个元组包含一个词及其标签。
如果我是对的,那么你的最后一个循环实际上循环遍历数据框中的项目而不是单词,正如变量名称所暗示的那样。如果一个项目只有一个词(这不太可能,因为你在 clean()
中过滤了很多),你对 word[1]
的调用将失败,并出现类似于你报告的错误。
改为尝试此代码:
words = []
for item in POS_tag_text_clean:
words_in_item = []
for word in item:
if word[1] !='VBD' and word[1] !='VBN':
words_in_item .append(word[0])
words.append(words_in_item)
我想从我的 CSV 文件中删除标有特定词性标签 VBD
和 VBN
的词。但是,我在输入以下代码后收到错误 "IndexError: list index out of range":
for word in POS_tag_text_clean:
if word[1] !='VBD' and word[1] !='VBN':
words.append(word[0])
我的 CSV 文件有 10 条评论,来自 10 个人,行名称是 Comment
。
这是我的完整代码:
df_Comment = pd.read_csv("myfile.csv")
def clean(text):
stop = set(stopwords.words('english'))
exclude = set(string.punctuation)
lemma = WordNetLemmatizer()
tagged = nltk.pos_tag(text)
text = text.rstrip()
text = re.sub(r'[^a-zA-Z]', ' ', text)
stop_free = " ".join([i for i in text.lower().split() if((i not in stop) and (not i.isdigit()))])
punc_free = ''.join(ch for ch in stop_free if ch not in exclude)
normalized = " ".join(lemma.lemmatize(word) for word in punc_free.split())
return normalized
text_clean = []
for text in df)Comment['Comment']:
text_clean.append(clean(text).split())
print(text_clean)
POS_tag_text_clean = [nltk.pos_tag(t) for t in text_clean]
print(POS_tag_text_clean)
words=[]
for word in POS_tag_text_clean:
if word[1] !='VBD' and word[1] !='VBN':
words.append(word[0])
如何修复错误?
如果没有示例和相应的输出,很难理解您的问题,但可能是这样的:
假设 text
是一个字符串,text_clean
将是一个字符串列表的列表,其中每个字符串代表一个单词。在词性标注之后,POS_tag_text_clean
因此将是元组列表的列表,每个元组包含一个词及其标签。
如果我是对的,那么你的最后一个循环实际上循环遍历数据框中的项目而不是单词,正如变量名称所暗示的那样。如果一个项目只有一个词(这不太可能,因为你在 clean()
中过滤了很多),你对 word[1]
的调用将失败,并出现类似于你报告的错误。
改为尝试此代码:
words = []
for item in POS_tag_text_clean:
words_in_item = []
for word in item:
if word[1] !='VBD' and word[1] !='VBN':
words_in_item .append(word[0])
words.append(words_in_item)