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.

Files for tidytext, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size tidytext-0.0.1.tar.gz (4.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page