python - AttributeError:“列表”对象没有属性“isdigit”。有效地指定句子列表中每个单词的 POS?
问题描述
假设我有句子列表(在大型语料库中)作为标记词的集合。样本格式如下:
tokenized_raw_data的格式如下:
[['arxiv', ':', 'astro-ph/9505066', '.'], ['seds', 'page', 'on', '``',
'globular', 'star', 'clusters', "''", 'douglas', 'scott', '``', 'independent',
'age', 'estimates', "''", 'krysstal', '``', 'the', 'scale', 'of', 'the',
'universe', "''", 'space', 'and', 'time', 'scaled', 'for', 'the', 'beginner',
'.'], ['icosmos', ':', 'cosmology', 'calculator', '(', 'with', 'graph',
'generation', ')', 'the', 'expanding', 'universe', '(', 'american',
'institute', 'of', 'physics', ')']]
我想申请pos_tag
.
到目前为止,我尝试过的内容如下。
import os, nltk, re
from nltk.corpus import stopwords
from unidecode import unidecode
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.tag import pos_tag
def read_data():
global tokenized_raw_data
with open("path//merge_text_results_pu.txt", 'r', encoding='utf-8', errors = 'replace') as f:
raw_data = f.read()
tokenized_raw_data = '\n'.join(nltk.line_tokenize(raw_data))
read_data()
def function1():
tokens_sentences = sent_tokenize(tokenized_raw_data.lower())
unfiltered_tokens = [[word for word in word_tokenize(word)] for word in tokens_sentences]
tagged_tokens = nltk.pos_tag(unfiltered_tokens)
nouns = [word.encode('utf-8') for word,pos in tagged_tokens
if (pos == 'NN' or pos == 'NNP' or pos == 'NNS' or pos == 'NNPS')]
joined_nouns_text = (' '.join(map(bytes.decode, nouns))).strip()
noun_tokens = [t for t in wordpunct_tokenize(joined_nouns_text)]
stop_words = set(stopwords.words("english"))
function1()
我收到以下错误。
> AttributeError: 'list' object has no attribute 'isdigit'
请帮助如何以省时的方式克服这个错误?我哪里错了?
注意:我在 Windows 10 上使用 Python 3.7。
解决方案
尝试这个-
word_list=[]
for i in range(len(unfiltered_tokens)):
word_list.append([])
for i in range(len(unfiltered_tokens)):
for word in unfiltered_tokens[i]:
if word[1:].isalpha():
word_list[i].append(word[1:])
然后在做之后
tagged_tokens=[]
for token in word_list:
tagged_tokens.append(nltk.pos_tag(token))
你会得到你想要的结果!希望这有帮助。
推荐阅读
- ios - 带有嵌入式框架的命令行符号 Xcode 项目
- sql-server - 需要帮助在预先编写的 SQL 语句中添加 WHERE 子句
- facebook - 如何在浏览器中测试 FBInstant.payments?
- excel - 运行 PowerShell 命令的 VBA 宏
- r - 将第 1-12 周变异到第 1 季度
- jquery - 为什么 jQuery .done() 不能识别外部函数
- linux - 将字符串与数组连接以在 bash 中递归复制文件
- javascript - 打印 JavaScript Promise 的解析值
- perl - Removing lines around a given matching line
- templates - 将 HTML 保存到 golang 模板变量