Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tidytext

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tidytext

Text processing with pandas DataFrames.

  • 0.0.1
  • PyPI
  • Socket score

Maintainers
1

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
zenline
00
1The Zen of Python, by Tim Peters1
22
.........
19If the implementation is hard to explain, it's a bad idea.19
20If the implementation is easy to explain, it may be a good idea.20
21Namespaces 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")
lineword
00NaN
11the
11zen
.........
2121more
2121of
2121those

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)
)
linewordntfidftf_idf
379counts10.5000002.9957321.497866
389readability10.5000002.9957321.497866
5613explicitly10.3333332.9957320.998577
.....................
9918is10.1250000.6931470.086643
11219is10.0909090.6931470.063013
12420is10.0769230.6931470.053319

140 rows × 6 columns

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc