有人能告诉我 remove_punct_dict 命令在做什么吗??最后一行命令的输出是什么?
can someone tell whats remove_punct_dict command is doing ?? and whats the output of last line command?
def LemTokens(tokens):
return [lemmer.lemmatize(token) for token in tokens]
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)
def LemNormalize(text):
return LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))
谁能告诉我 remove_punct_dict 命令在做什么??最后一行命令的输出是什么?
remove_punct_dict
实际上是在string.punctuation中找到的所有标点符号的Unicode值的dict集合,值为None
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)
它简单地分解为为字符串中的每个标点符号创建一个 dict(ord(punct),None)
,其中 ord 是 python 中的内置函数,用于返回相应字符的 Unicode 值。
让我们回顾一下最后一个函数:
def LemNormalize(text):
return LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))
此方法开始将给定的文本变为小写,然后使用我们之前创建的 remove_punct_dict 的引用删除文本中的标点符号。
例如因此 Hello World
变为 hello world!
然后 hello world
然后它继续标记单词,因此现在 Hello World
我们有 hello
和 world
.
最后一个功能是将词干化为最简单的形式。您可以阅读有关词干提取的更多信息 here。 hello
和 world
已经是使用 Porter 词干提取器提取的词干,因此将保持不变。因此,我的示例的最终输出只是
hello
和 world
.
示例:
import string
import nltk;
text = "Hello World! My name is bob and i own a dog, a cat and a chicken."
lemmer = nltk.stem.WordNetLemmatizer()
def LemTokens(tokens):
return [lemmer.lemmatize(token) for token in tokens]
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)
def LemNormalize(text):
return LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))
print(LemNormalize(text)) # ['hello', 'world', 'my', 'name', 'is', 'bob', 'and', 'i', 'own', 'a', 'dog', 'a', 'cat', 'and', 'a', 'chicken']
def LemTokens(tokens):
return [lemmer.lemmatize(token) for token in tokens]
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)
def LemNormalize(text):
return LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))
谁能告诉我 remove_punct_dict 命令在做什么??最后一行命令的输出是什么?
remove_punct_dict
实际上是在string.punctuation中找到的所有标点符号的Unicode值的dict集合,值为None
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)
它简单地分解为为字符串中的每个标点符号创建一个 dict(ord(punct),None)
,其中 ord 是 python 中的内置函数,用于返回相应字符的 Unicode 值。
让我们回顾一下最后一个函数:
def LemNormalize(text):
return LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))
此方法开始将给定的文本变为小写,然后使用我们之前创建的 remove_punct_dict 的引用删除文本中的标点符号。
例如因此 Hello World
变为 hello world!
然后 hello world
然后它继续标记单词,因此现在 Hello World
我们有 hello
和 world
.
最后一个功能是将词干化为最简单的形式。您可以阅读有关词干提取的更多信息 here。 hello
和 world
已经是使用 Porter 词干提取器提取的词干,因此将保持不变。因此,我的示例的最终输出只是
hello
和 world
.
示例:
import string
import nltk;
text = "Hello World! My name is bob and i own a dog, a cat and a chicken."
lemmer = nltk.stem.WordNetLemmatizer()
def LemTokens(tokens):
return [lemmer.lemmatize(token) for token in tokens]
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)
def LemNormalize(text):
return LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))
print(LemNormalize(text)) # ['hello', 'world', 'my', 'name', 'is', 'bob', 'and', 'i', 'own', 'a', 'dog', 'a', 'cat', 'and', 'a', 'chicken']