Skip to main content

Text processing with pandas DataFrames.

Project description

tidytext-py

Handy text processing in python, using pandas DataFrames.

This library is a python port of the R package tidytext.

Install

pip install tidytext

This will also install the nltk package. However, you will need to download additional resources to use tidytext, using the code below.

nltk.download("punkt")

Functions implemented

  • bind_tfidf
  • unnest_tokens

Examples

unnest_tokens

import pandas as pd

pd.set_option("display.max_rows", 6)

zen = """
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""

zen_split = zen.splitlines()


df = pd.DataFrame({
    "zen": zen_split,
    "line": list(range(len(zen_split)))
})

df
zen line
0 0
1 The Zen of Python, by Tim Peters 1
2 2
... ... ...
19 If the implementation is hard to explain, it's a bad idea. 19
20 If the implementation is easy to explain, it may be a good idea. 20
21 Namespaces are one honking great idea -- let's do more of those! 21

22 rows × 2 columns

from tidytext import unnest_tokens

unnest_tokens(df, "word", "zen")
line word
0 0 NaN
1 1 the
1 1 zen
... ... ...
21 21 more
21 21 of
21 21 those

145 rows × 2 columns

bind_tf_idf

from tidytext import unnest_tokens, bind_tf_idf 
from siuba import _, count, arrange

(df
  >> unnest_tokens(_.word, _.zen)
  >> count(_.line, _.word)
  >> bind_tf_idf(_.word, _.line, _.n)
  >> arrange(-_.tf_idf)
)
line word n tf idf tf_idf
37 9 counts 1 0.500000 2.995732 1.497866
38 9 readability 1 0.500000 2.995732 1.497866
56 13 explicitly 1 0.333333 2.995732 0.998577
... ... ... ... ... ... ...
99 18 is 1 0.125000 0.693147 0.086643
112 19 is 1 0.090909 0.693147 0.063013
124 20 is 1 0.076923 0.693147 0.053319

140 rows × 6 columns

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tidytext-0.0.1.tar.gz (4.3 kB view details)

Uploaded Source

File details

Details for the file tidytext-0.0.1.tar.gz.

File metadata

  • Download URL: tidytext-0.0.1.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.6.8

File hashes

Hashes for tidytext-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a17cf0b3878b95552e3017dc55f0c9304347f0253f9d90d71f7803e7dcafe404
MD5 34f6afe28eb9b5e29dac906c4167a2f8
BLAKE2b-256 c10c07991d5b50a105d37242d616ebfe4d8e03f5fc13bb5b0c671778a4f2925d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page