如何获得以下函数中每个元素的结果
How do I get the result of every element in the following function
我有一个函数 return 每个词的词性以元组列表的形式。
当我执行它时,我只得到第一个元素(第一个元组)的结果。我想获得该列表中每个元素(元组)的结果。例如:
get_word_pos("I am watching")
我得到的结果是:
[('I', 'PRP'), ('am', 'VBP'), ('watching', 'VBG')]
'n'
但是我想要的结果如下
"n"
"v"
"v"
我编写的函数包含多个 return 语句,这就是我只获取第一个元素作为输出的原因。如果有人可以修改我的功能以便我获得所需的输出,请。
代码如下:
training = state_union.raw("2005-GWBush.txt")
tokenizer = nltk.tokenize.punkt.PunktSentenceTokenizer(training)
def get_word_pos(word):
sample = word
tokenized = tokenizer.tokenize(sample)
for i in tokenized:
words = nltk.word_tokenize(i)
tagged = nltk.pos_tag(words)
print(tagged)
for letter in tagged:
#print(letter[1])
if letter[1].startswith('J'):
return wordnet.ADJ
elif letter[1].startswith('V'):
return wordnet.VERB
elif letter[1].startswith('N'):
return wordnet.NOUN
elif letter[1].startswith('R'):
return wordnet.ADV
else:
return wordnet.NOUN
```
当您遍历 tagged 时,您会 return 第一项的值。你需要积累它们。将它们附加到列表中是一种方法。例如:
from nltk import word_tokenize, pos_tag
from nltk.corpus import state_union
from nltk.tokenize import PunktSentenceTokenizer
from nltk.corpus import wordnet
training = state_union.raw('2005-GWBush.txt')
tokenizer = PunktSentenceTokenizer(training)
def get_word_pos(word):
result = []
for token in tokenizer.tokenize(word):
words = word_tokenize(token)
for t in pos_tag(words):
match t[1][0]:
case 'J':
result.append(wordnet.ADJ)
case 'V':
result.append(wordnet.VERB)
case 'R':
result.append(wordnet.ADV)
case _:
result.append(wordnet.NOUN)
return result
print(get_word_pos('I am watching'))
输出:
['n', 'v', 'v']
我有一个函数 return 每个词的词性以元组列表的形式。 当我执行它时,我只得到第一个元素(第一个元组)的结果。我想获得该列表中每个元素(元组)的结果。例如:
get_word_pos("I am watching")
我得到的结果是:
[('I', 'PRP'), ('am', 'VBP'), ('watching', 'VBG')]
'n'
但是我想要的结果如下
"n"
"v"
"v"
我编写的函数包含多个 return 语句,这就是我只获取第一个元素作为输出的原因。如果有人可以修改我的功能以便我获得所需的输出,请。 代码如下:
training = state_union.raw("2005-GWBush.txt")
tokenizer = nltk.tokenize.punkt.PunktSentenceTokenizer(training)
def get_word_pos(word):
sample = word
tokenized = tokenizer.tokenize(sample)
for i in tokenized:
words = nltk.word_tokenize(i)
tagged = nltk.pos_tag(words)
print(tagged)
for letter in tagged:
#print(letter[1])
if letter[1].startswith('J'):
return wordnet.ADJ
elif letter[1].startswith('V'):
return wordnet.VERB
elif letter[1].startswith('N'):
return wordnet.NOUN
elif letter[1].startswith('R'):
return wordnet.ADV
else:
return wordnet.NOUN
```
当您遍历 tagged 时,您会 return 第一项的值。你需要积累它们。将它们附加到列表中是一种方法。例如:
from nltk import word_tokenize, pos_tag
from nltk.corpus import state_union
from nltk.tokenize import PunktSentenceTokenizer
from nltk.corpus import wordnet
training = state_union.raw('2005-GWBush.txt')
tokenizer = PunktSentenceTokenizer(training)
def get_word_pos(word):
result = []
for token in tokenizer.tokenize(word):
words = word_tokenize(token)
for t in pos_tag(words):
match t[1][0]:
case 'J':
result.append(wordnet.ADJ)
case 'V':
result.append(wordnet.VERB)
case 'R':
result.append(wordnet.ADV)
case _:
result.append(wordnet.NOUN)
return result
print(get_word_pos('I am watching'))
输出:
['n', 'v', 'v']