Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/machow/tidytext-py
A python port of Julia Silge and Dave Robinson's tidytext package
https://github.com/machow/tidytext-py
Last synced: 3 months ago
JSON representation
A python port of Julia Silge and Dave Robinson's tidytext package
- Host: GitHub
- URL: https://github.com/machow/tidytext-py
- Owner: machow
- License: mit
- Created: 2020-08-13T18:07:00.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-08-15T20:19:57.000Z (over 4 years ago)
- Last Synced: 2023-08-11T10:31:34.210Z (over 1 year ago)
- Language: Python
- Size: 30.3 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
tidytext-py
===========[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/machow/tidytext-py/master)
Handy text processing in python, using pandas DataFrames.
This library is a python port of the [R package tidytext](https://github.com/juliasilge/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.```python
nltk.download("punkt")
```Functions implemented
---------------------* bind_tfidf
* unnest_tokensExamples
--------Check out the [examples folder using binder](https://mybinder.org/v2/gh/machow/tidytext-py/master?urlpath=notebooks/examples).
### unnest_tokens
```{python tags=c("hide-input")}
import pandas as pd# DataFrame display --------
pd.set_option("display.max_rows", 6)
from IPython import get_ipython# special ipython function to get the html formatter
html_formatter = get_ipython().display_formatter.formatters['text/html']# here, we avoid the default df._repr_html_ method, since it inlines css
# (which makes github angry)
html_formatter.for_type(
pd.DataFrame,
lambda df: df.to_html(max_rows = pd.get_option("display.max_rows"), show_dimensions = True)
)
``````{python}
import pandas as pdpd.set_option("display.max_rows", 6)
zen = """
The Zen of Python, by Tim PetersBeautiful 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
``````{python}
from tidytext import unnest_tokensunnest_tokens(df, "word", "zen")
```### bind_tf_idf
```{python}
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)
)
```