Secvențe comune de cuvinte

Introducere în Spark SQL în Python

Mark Plutowski

Data Scientist

Antrenament

Introducere în Spark SQL în Python

Predicție

Introducere în Spark SQL în Python

Predicția cuvântului final

Introducere în Spark SQL în Python

Secvență

Introducere în Spark SQL în Python

Ultimul element din secvență

Introducere în Spark SQL în Python

Vulpea brună rapidă

Introducere în Spark SQL în Python

Paranteza propoziției

Introducere în Spark SQL în Python

Alt tip de agregare

Introducere în Spark SQL în Python

Videoclipuri

Introducere în Spark SQL în Python

Date categoriale

Introducere în Spark SQL în Python

Categoriale vs. Ordinale

  • Categoriale: he, hi, she, that, they
  • Ordinale: 1, 2, 3, 4, 5
Introducere în Spark SQL în Python

Analiza secvențelor

Introducere în Spark SQL în Python

Cuvânt precedent și următor

Introducere în Spark SQL în Python

3-tuple

query3 = """
   SELECT 
   id,
   word AS w1,
   LEAD(word,1) OVER(PARTITION BY part ORDER BY id ) AS w2,
   LEAD(word,2) OVER(PARTITION BY part ORDER BY id ) AS w3
   FROM df
""" 
Introducere în Spark SQL în Python

Funcție fereastră SQL ca subinterogare

query3agg = """
SELECT w1, w2, w3, COUNT(*) as count FROM (
   SELECT 
   word AS w1,
   LEAD(word,1) OVER(PARTITION BY part ORDER BY id ) AS w2,
   LEAD(word,2) OVER(PARTITION BY part ORDER BY id ) AS w3
   FROM df
)
GROUP BY w1, w2, w3 
ORDER BY count DESC
""" 

spark.sql(query3agg).show()
Introducere în Spark SQL în Python

Funcție fereastră SQL ca subinterogare – rezultat

+-----+-----+-----+-----+
|   w1|   w2|   w3|count|
+-----+-----+-----+-----+
|  one|   of|  the|   49|
|    i|think| that|   46|
|   it|   is|    a|   46|
|   it|  was|    a|   45|
| that|   it|  was|   38|
|  out|   of|  the|   35|
|.....|.....|.....|.....|
Introducere în Spark SQL în Python

Cele mai frecvente 3-tuple

+-----+-----+-----+-----+
|   w1|   w2|   w3|count|
+-----+-----+-----+-----+
|  one|   of|  the|   49|
|    i|think| that|   46|
|   it|   is|    a|   46|
|   it|  was|    a|   45|
| that|   it|  was|   38|
|  out|   of|  the|   35|
| that|    i| have|   35|
|there|  was|    a|   34|
|    i|   do|  not|   34|
| that|   it|   is|   33|
| that|   he|  was|   30|
| that|   he|  had|   30|
| that|    i|  was|   28|
+-----+-----+-----+-----+
Introducere în Spark SQL în Python

Alt tip de agregare

query3agg = """
SELECT w1, w2, w3, length(w1)+length(w2)+length(w3) as length FROM (
   SELECT 
   word AS w1,
   LEAD(word,1) OVER(PARTITION BY part ORDER BY id ) AS w2,
   LEAD(word,2) OVER(PARTITION BY part ORDER BY id ) AS w3
   FROM df
   WHERE part <> 0 and part <> 13
)
GROUP BY w1, w2, w3 
ORDER BY length DESC
""" 

spark.sql(query3agg).show(truncate=False)
Introducere în Spark SQL în Python

Alt tip de agregare

+-------------------+-------------------+---------------+------+
|                 w1|                 w2|             w3|length|
+-------------------+-------------------+---------------+------+
|comfortable-looking|           building|    two-storied|    38|
|         widespread|comfortable-looking|       building|    37|
|      extraordinary|      circumstances|      connected|    35|
|      simple-minded|      nonconformist|      clergyman|    35|
|       particularly|          malignant|  boot-slitting|    34|
|       unsystematic|        sensational|     literature|    33|
|       oppressively|        respectable|     frock-coat|    33|
|         relentless|        keen-witted|   ready-handed|    33|
|   travelling-cloak|                and|  close-fitting|    32|
|        ruddy-faced|      white-aproned|       landlord|    32|
|  fellow-countryman|            colonel|       lysander|    32|
+-------------------+-------------------+---------------+------+
Introducere în Spark SQL în Python

Să exersăm!

Introducere în Spark SQL în Python

Preparing Video For Download...