当前位置:   article > 正文

Python-Word标记化

nltk_tokens=nltk.word

Python-Word标记化 (Python - Word Tokenization)

Word tokenization is the process of splitting a large sample of text into words. This is a requirement in natural language processing tasks where each word needs to be captured and subjected to further analysis like classifying and counting them for a particular sentiment etc. The Natural Language Tool kit(NLTK) is a library used to achieve this. Install NLTK before proceeding with the python program for word tokenization.

单词标记化是将大量文本样本拆分为单词的过程。 这是自然语言处理任务的要求,在自然语言处理任务中,每个单词都需要捕获并进行进一步的分析,例如根据特定的情感对它们进行分类和计数等。自然语言工具套件(NLTK)是用于实现此目的的库。 在继续python程序进行单词标记化之前,请安装NLTK。

  1. conda install -c anaconda nltk

Next we use the word_tokenize method to split the paragraph into individual words.

接下来,我们使用word_tokenize方法将段落拆分为单个单词。

  1. import nltk
  2. word_data = "It originated from the idea that there are readers who prefer learning new skills from the comforts of their drawing rooms"
  3. nltk_tokens = nltk.word_tokenize(word_data)
  4. print (nltk_tokens)

When we execute the above code, it produces the following result.

当我们执行上面的代码时,它产生以下结果。

  1. ['It', 'originated', 'from', 'the', 'idea', 'that', 'there', 'are', 'readers',
  2. 'who', 'prefer', 'learning', 'new', 'skills', 'from', 'the',
  3. 'comforts', 'of', 'their', 'drawing', 'rooms']

标记化句子 (Tokenizing Sentences)

We can also tokenize the sentences in a paragraph like we tokenized the words. We use the method sent_tokenize to achieve this. Below is an example.

我们也可以像标记词一样标记段落中的句子。 我们使用send_tokenize方法来实现此目的。 下面是一个例子。

  1. import nltk
  2. sentence_data = "Sun rises in the east. Sun sets in the west."
  3. nltk_tokens = nltk.sent_tokenize(sentence_data)
  4. print (nltk_tokens)

When we execute the above code, it produces the following result.

当我们执行上面的代码时,它产生以下结果。

  1. ['Sun rises in the east.', 'Sun sets in the west.']

翻译自: https://www.tutorialspoint.com/python_data_science/python_word_tokenization.htm

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/353091
推荐阅读
相关标签
  

闽ICP备14008679号