From ffaeb2626e38a0b0cb185c235445e6274fabfe8a Mon Sep 17 00:00:00 2001 From: 9harshit <9arshit@gmail.com> Date: Tue, 29 Mar 2022 18:40:52 -0300 Subject: [PATCH 01/12] feat: added ngram algorithm --- strings/ngram.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 strings/ngram.py diff --git a/strings/ngram.py b/strings/ngram.py new file mode 100644 index 000000000000..561ac9777da7 --- /dev/null +++ b/strings/ngram.py @@ -0,0 +1,30 @@ +""" +https://en.wikipedia.org/wiki/N-gram + + +""" + + +def create_ngram(sentence: str, ngram_size: int) -> list: + """ + Create ngrams from a sentence + :param sentence: str + :param ngram_size: int + :return: list + + >>> create_ngram("I am a sentence", 2) + ['I ', ' a', 'am', 'm ', ' a', 'a ', ' s', 'se', 'en', 'nt', 'te', 'en', 'nc', 'ce'] + >>> create_ngram("I am an NLPer", 2) + ['I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er'] + """ + + ngrams = [] + for i in range(len(sentence) - ngram_size + 1): + ngrams.append(sentence[i : i + ngram_size]) + return ngrams + + +if __name__ == "__main__": + from doctest import testmod + + testmod() From e3a4b20b6e7d38ddc06c870360b050788d918792 Mon Sep 17 00:00:00 2001 From: 9harshit <9arshit@gmail.com> Date: Tue, 29 Mar 2022 18:45:00 -0300 Subject: [PATCH 02/12] feat: added ngram algorithm --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ab74d28e167a..33da02fb72ad 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,7 +14,7 @@ repos: - id: requirements-txt-fixer - repo: https://github.com/psf/black - rev: 22.1.0 + rev: 22.3.0 hooks: - id: black From 4541ff8121a9d8598362b83c0da343088a1769c3 Mon Sep 17 00:00:00 2001 From: 9harshit <9arshit@gmail.com> Date: Tue, 29 Mar 2022 18:51:29 -0300 Subject: [PATCH 03/12] feat: added ngram algorithm --- arithmetic_analysis/in_static_equilibrium.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/arithmetic_analysis/in_static_equilibrium.py b/arithmetic_analysis/in_static_equilibrium.py index 2ac3e7213fda..5d091eb9cdf2 100644 --- a/arithmetic_analysis/in_static_equilibrium.py +++ b/arithmetic_analysis/in_static_equilibrium.py @@ -13,7 +13,7 @@ def polar_force( Resolves force along rectangular components. (force, angle) => (force_x, force_y) >>> polar_force(10, 45) - [7.071067811865477, 7.0710678118654755] + [7.0710678118654755, 7.071067811865475] >>> polar_force(10, 3.14, radian_mode=True) [-9.999987317275396, 0.01592652916486828] """ @@ -50,7 +50,11 @@ def in_static_equilibrium( if __name__ == "__main__": # Test to check if it works forces = array( - [polar_force(718.4, 180 - 30), polar_force(879.54, 45), polar_force(100, -90)] + [ + polar_force(718.4, 180 - 30), + polar_force(879.54, 45), + polar_force(100, -90), + ] ) location = array([[0, 0], [0, 0], [0, 0]]) From 1f237cd911604a822e20e2ef6812d81eb80dc902 Mon Sep 17 00:00:00 2001 From: 9harshit <9arshit@gmail.com> Date: Wed, 30 Mar 2022 12:10:47 -0300 Subject: [PATCH 04/12] added list comprehension --- strings/ngram.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/strings/ngram.py b/strings/ngram.py index 561ac9777da7..22762df04180 100644 --- a/strings/ngram.py +++ b/strings/ngram.py @@ -18,9 +18,9 @@ def create_ngram(sentence: str, ngram_size: int) -> list: ['I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er'] """ - ngrams = [] - for i in range(len(sentence) - ngram_size + 1): - ngrams.append(sentence[i : i + ngram_size]) + ngrams = [ + sentence[i : i + ngram_size] for i in range(len(sentence) - ngram_size + 1) + ] return ngrams From 2bf6ea8b28be972bc6c1eba9edf6b1ee83ba7572 Mon Sep 17 00:00:00 2001 From: Harshit Agarwal <43147421+9harshit@users.noreply.github.com> Date: Wed, 30 Mar 2022 20:20:18 -0300 Subject: [PATCH 05/12] Update strings/ngram.py Co-authored-by: Christian Clauss --- strings/ngram.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/strings/ngram.py b/strings/ngram.py index 22762df04180..84290455dfc8 100644 --- a/strings/ngram.py +++ b/strings/ngram.py @@ -18,10 +18,7 @@ def create_ngram(sentence: str, ngram_size: int) -> list: ['I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er'] """ - ngrams = [ - sentence[i : i + ngram_size] for i in range(len(sentence) - ngram_size + 1) - ] - return ngrams + return [sentence[i : i + ngram_size] for i in range(len(sentence) - ngram_size + 1)] if __name__ == "__main__": From 988fdda48274c03392fb8bceeefa8a120b66ed0b Mon Sep 17 00:00:00 2001 From: Harshit Agarwal <43147421+9harshit@users.noreply.github.com> Date: Wed, 30 Mar 2022 20:22:04 -0300 Subject: [PATCH 06/12] Update strings/ngram.py Co-authored-by: Christian Clauss --- strings/ngram.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/strings/ngram.py b/strings/ngram.py index 84290455dfc8..a0a05145d03b 100644 --- a/strings/ngram.py +++ b/strings/ngram.py @@ -16,6 +16,8 @@ def create_ngram(sentence: str, ngram_size: int) -> list: ['I ', ' a', 'am', 'm ', ' a', 'a ', ' s', 'se', 'en', 'nt', 'te', 'en', 'nc', 'ce'] >>> create_ngram("I am an NLPer", 2) ['I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er'] + >>> create_ngram("This is short", 50) + ??? """ return [sentence[i : i + ngram_size] for i in range(len(sentence) - ngram_size + 1)] From a4e5d83c847a8bdcc44d6db0ef1a9997eb602c7b Mon Sep 17 00:00:00 2001 From: Harshit Agarwal <43147421+9harshit@users.noreply.github.com> Date: Thu, 31 Mar 2022 11:18:37 -0300 Subject: [PATCH 07/12] Update strings/ngram.py Co-authored-by: Christian Clauss --- strings/ngram.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/strings/ngram.py b/strings/ngram.py index a0a05145d03b..724e11de22a7 100644 --- a/strings/ngram.py +++ b/strings/ngram.py @@ -8,9 +8,6 @@ def create_ngram(sentence: str, ngram_size: int) -> list: """ Create ngrams from a sentence - :param sentence: str - :param ngram_size: int - :return: list >>> create_ngram("I am a sentence", 2) ['I ', ' a', 'am', 'm ', ' a', 'a ', ' s', 'se', 'en', 'nt', 'te', 'en', 'nc', 'ce'] From e8b19a4f8f16ae83148d20f1b107df440800f240 Mon Sep 17 00:00:00 2001 From: Harshit Agarwal <43147421+9harshit@users.noreply.github.com> Date: Thu, 31 Mar 2022 11:19:52 -0300 Subject: [PATCH 08/12] Update ngram.py --- strings/ngram.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/ngram.py b/strings/ngram.py index 724e11de22a7..b7ec264d58d2 100644 --- a/strings/ngram.py +++ b/strings/ngram.py @@ -14,7 +14,7 @@ def create_ngram(sentence: str, ngram_size: int) -> list: >>> create_ngram("I am an NLPer", 2) ['I ', ' a', 'am', 'm ', ' a', 'an', 'n ', ' N', 'NL', 'LP', 'Pe', 'er'] >>> create_ngram("This is short", 50) - ??? + [] """ return [sentence[i : i + ngram_size] for i in range(len(sentence) - ngram_size + 1)] From c0c5d0107dbd93bef884ac70bbfaeac80b7415ae Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Mon, 4 Apr 2022 08:51:32 +0530 Subject: [PATCH 09/12] chore: add return list element type --- strings/ngram.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/strings/ngram.py b/strings/ngram.py index b7ec264d58d2..0b13e34a4732 100644 --- a/strings/ngram.py +++ b/strings/ngram.py @@ -1,11 +1,9 @@ """ https://en.wikipedia.org/wiki/N-gram - - """ -def create_ngram(sentence: str, ngram_size: int) -> list: +def create_ngram(sentence: str, ngram_size: int) -> list[str]: """ Create ngrams from a sentence @@ -16,7 +14,6 @@ def create_ngram(sentence: str, ngram_size: int) -> list: >>> create_ngram("This is short", 50) [] """ - return [sentence[i : i + ngram_size] for i in range(len(sentence) - ngram_size + 1)] From 43be367cc01c33afd8bb786d3a603871018d5630 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Mon, 4 Apr 2022 08:55:31 +0530 Subject: [PATCH 10/12] fix(test): use `round` to match approx for float --- arithmetic_analysis/in_static_equilibrium.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/arithmetic_analysis/in_static_equilibrium.py b/arithmetic_analysis/in_static_equilibrium.py index 5d091eb9cdf2..c89cde5c38ee 100644 --- a/arithmetic_analysis/in_static_equilibrium.py +++ b/arithmetic_analysis/in_static_equilibrium.py @@ -12,8 +12,11 @@ def polar_force( """ Resolves force along rectangular components. (force, angle) => (force_x, force_y) - >>> polar_force(10, 45) - [7.0710678118654755, 7.071067811865475] + >>> force = polar_force(10, 45) + >>> round(force[0], 13) + 7.0710678118654 + >>> round(force[1], 13) + 7.0710678118654 >>> polar_force(10, 3.14, radian_mode=True) [-9.999987317275396, 0.01592652916486828] """ From 369a790cb8a7958c8c1a4f91ea45524a3111a291 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Mon, 4 Apr 2022 08:59:26 +0530 Subject: [PATCH 11/12] fix: round down one more digit --- arithmetic_analysis/in_static_equilibrium.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arithmetic_analysis/in_static_equilibrium.py b/arithmetic_analysis/in_static_equilibrium.py index c89cde5c38ee..b02ccac419b5 100644 --- a/arithmetic_analysis/in_static_equilibrium.py +++ b/arithmetic_analysis/in_static_equilibrium.py @@ -13,10 +13,10 @@ def polar_force( Resolves force along rectangular components. (force, angle) => (force_x, force_y) >>> force = polar_force(10, 45) - >>> round(force[0], 13) - 7.0710678118654 - >>> round(force[1], 13) - 7.0710678118654 + >>> round(force[0], 12) + 7.071067811865 + >>> round(force[1], 12) + 7.071067811865 >>> polar_force(10, 3.14, radian_mode=True) [-9.999987317275396, 0.01592652916486828] """ From f69589e392a142427b4a9da80a7c2253bd4722ae Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Mon, 4 Apr 2022 09:02:18 +0530 Subject: [PATCH 12/12] chore: use math.isclose instead of round --- arithmetic_analysis/in_static_equilibrium.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/arithmetic_analysis/in_static_equilibrium.py b/arithmetic_analysis/in_static_equilibrium.py index b02ccac419b5..ed0d1eb98cf3 100644 --- a/arithmetic_analysis/in_static_equilibrium.py +++ b/arithmetic_analysis/in_static_equilibrium.py @@ -12,11 +12,12 @@ def polar_force( """ Resolves force along rectangular components. (force, angle) => (force_x, force_y) + >>> import math >>> force = polar_force(10, 45) - >>> round(force[0], 12) - 7.071067811865 - >>> round(force[1], 12) - 7.071067811865 + >>> math.isclose(force[0], 7.071067811865477) + True + >>> math.isclose(force[1], 7.0710678118654755) + True >>> polar_force(10, 3.14, radian_mode=True) [-9.999987317275396, 0.01592652916486828] """