# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="s4ljYpQNp50r"
# ![JohnSnowLabs](https://nlp.johnsnowlabs.com/assets/images/logo.png)
#
# [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/JohnSnowLabs/nlu/blob/master/examples/colab/component_examples/dependency_parsing/NLU_untyped_dependency_parsing_example.ipynb)
#
#
# # Untyped Dependency Parsing with NLU. 
# ![](https://nlp.johnsnowlabs.com/assets/images/dependency_parser.png)
#
# Each word in a sentence has a grammatical relation to other words in the sentence.     
# These relation pairs can be typed (i.e. subject or pronouns)     or they can be untyped, in which case only the edges between the tokens will be predicted, withouth the label.
#
# With NLU you can get these relations in just 1 line of code! 
# # 1. Install Java and NLU

# + id="SF5-Z-U4jukd"
import os
# ! apt-get update -qq > /dev/null   
# Install java
# ! apt-get install -y openjdk-8-jdk-headless -qq > /dev/null
os.environ["JAVA_HOME"] = "/usr/lib/jvm/java-8-openjdk-amd64"
os.environ["PATH"] = os.environ["JAVA_HOME"] + "/bin:" + os.environ["PATH"]
# ! pip install nlu pyspark==2.4.7 > /dev/null


# + [markdown] id="kHtLKNXDtZf5"
# # 2. Load the Dependency model and predict some sample relationships

# + id="7GJX5d6mjk5j" colab={"base_uri": "https://localhost:8080/", "height": 512} executionInfo={"status": "ok", "timestamp": 1604907666230, "user_tz": -60, "elapsed": 128480, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjqAD-ircKP-s5Eh6JSdkDggDczfqQbJGU_IRb4Hw=s64", "userId": "14469489166467359317"}} outputId="7b5f4b95-706e-4c79-cf4b-9abcf40b3a01"
import nlu
dependency_pipe  = nlu.load('dep.untyped')
dependency_pipe.predict('Untyped dependencies describe with their relationship a directed graph')

# + [markdown] id="5lrDNzw3tcqT"
# # 3.1 Download sample dataset

# + id="gpeS8DWBlrun" colab={"base_uri": "https://localhost:8080/", "height": 607} executionInfo={"status": "ok", "timestamp": 1604907674240, "user_tz": -60, "elapsed": 136471, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjqAD-ircKP-s5Eh6JSdkDggDczfqQbJGU_IRb4Hw=s64", "userId": "14469489166467359317"}} outputId="c8a9f120-9018-4903-c44a-58f8b3b789b8"
import pandas as pd
# Download the dataset 
# ! wget -N https://s3.amazonaws.com/auxdata.johnsnowlabs.com/public/resources/en/sarcasm/train-balanced-sarcasm.csv -P /tmp
# Load dataset to Pandas
df = pd.read_csv('/tmp/train-balanced-sarcasm.csv')
df

# + [markdown] id="uLWu8DG3tfjz"
# ## 3.2 Predict on sample dataset
# NLU expects a text column, thus we must create it from the column that contains our text data

# + id="3V5l-B6nl43U" colab={"base_uri": "https://localhost:8080/", "height": 380} executionInfo={"status": "ok", "timestamp": 1604907690243, "user_tz": -60, "elapsed": 152462, "user": {"displayName": "<NAME>", "photoUrl": "https://lh3.googleusercontent.com/a-/AOh14GjqAD-ircKP-s5Eh6JSdkDggDczfqQbJGU_IRb4Hw=s64", "userId": "14469489166467359317"}} outputId="36136418-2a70-4184-83ba-59b40dd1d9ef"
dependency_pipe  = nlu.load('dep.untyped')
df['text'] = df['comment']
dependency_predictions = dependency_pipe.predict(df['text'].iloc[0:1])
dependency_predictions

StarcoderdataJupyterScriptsDedupFiltered