请在hackerrank上找到原始问题
虽然我的解决方案还不完整,但有人能帮我理解我哪里出错了吗?(在第二个函数中,标记器返回一个2个字母的标记,尽管问题要求输入一个3个字母的标记。谢谢你,谢谢
import re
import nltk
import string
final_tagged = ""
raw_input(strs)
def tokenize_two(i):
temp = i
global strs
"remove /?? and pos tag"
for ch in ['/??']:
if ch in i:
i=i.replace(ch,"")
#pos tagging
tag = nltk.pos_tag([i])
for item in tag:
for ch in ['??']:
if ch in temp:
temp = temp.replace(ch,item[1])
replace = i+"/??"
strs = string.replace(strs,replace,temp)
return temp;
def tokenize_three(i):
"remove /??? and pos tag"
temp = i
global strs
for ch in ['/???']:
if ch in i:
i=i.replace(ch,"")
tag = nltk.pos_tag([i])
for item in tag:
for ch in ['???']:
if ch in temp:
temp = temp.replace(ch,item[1])
replace = i+"/???"
strs = string.replace(strs,replace,temp)
return temp;
a = [w for w in re.split('\s+',strs)]
for i in a :
if(i.endswith("/??")):
tagged = tokenize_two(i)
if(i.endswith("/???")):
final_tagged = tokenize_three(i)
print strs
字符串
2条答案
按热度按时间igetnqfo1#
字符串
词性标注依赖于上下文。您需要将整个标记化的句子作为参数传递给
pos_tag
,而不是为每个未知单词调用一次pos_tag
。6yt4nkrj2#
您所指的问题是询问Penn的POS标记,而不是NLTK库中嵌入的POS标记。因此,它不会按照你的期望要求的答案。