Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Python: Natural Language Toolkit - Exercises, Solutions

Python NLTK

NLTK is a leading platform for building Python programs to work with human language data. It provides easy-to-use interfaces to over 50 corpora and lexical resources such as WordNet, along with a suite of text processing libraries for classification, tokenization, stemming, tagging, parsing, and semantic reasoning, wrappers for industrial-strength NLP libraries.

The best way we learn anything is by practice and exercise questions. We have started this section for those (beginner to intermediate) who are familiar with Python, Natural Language Toolkit.

Hope, these exercises help you to improve your Python-NLTK coding skills. Currently, following sections are available, we are working hard to add more exercises .... Happy Coding!

List of Python NLTK Exercises :

Installing NLTK:

NLTK requires Python versions 2.7, 3.5, 3.6, or 3.7

Mac/Unix:

  • Install NLTK: run sudo pip install -U nltk
  • Install Numpy (optional): run sudo pip install -U numpy
  • Test installation: run python then type import nltk

Windows:

These instructions assume that you do not already have Python installed on your machine.

  • Install Python 3.7: https://www.python.org/downloads
  • Install Numpy (optional): https://www.scipy.org/scipylib/download.html
  • Install NLTK: https://pypi.python.org/pypi/nltk
  • Test installation: Start>Python37, then type import nltk

anaconda / packages / nltk 3.4.1 Installers:

conda install 
  •  linux-ppc64le  v3.4.1
  •  osx-32  v3.0.4
  •  linux-64  v3.4.1
  •  win-32  v3.4.1
  •  osx-64  v3.4.1
  •  linux-32  v3.4
  •  win-64  v3.4.1

To install this package with conda run:

  conda install -c anaconda nltk 

[ Want to contribute to Python exercises? Send your code (attached with a .zip file) to us at w3resource[at]yahoo[dot]com. Please avoid copyrighted materials.]

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m