Best Python code snippet using avocado_python
statistics.py
Source:statistics.py
1from src.constants import PREPROCCESSED_DATA_DIR, STATISTICS_BASE_DIR2from src.data.statistics import statistics3from src.data.class_infos import Instance as classes_info4import csv5import os6def compute_statistics(preprocessed_base_dir, flags="abcdefghi", output_dir=STATISTICS_BASE_DIR):7 first_line = "=" * 10 + " Sentiment Emojizer Data Information " + "=" * 10 + "\n"8 log_str = "=" * 10 + " Sentiment Emojizer Data Information " + "=" * 10 + "\n"9 10 if ('a' in flags):11 data_count = statistics.count_data(preprocessed_base_dir)12 partial_str = "Label's rows count:\n"13 csv_columns = ['label', 'data_count']14 csv_data =[]15 for key in data_count:16 class_name = classes_info.get_class_name(key)17 partial_str += f"{class_name}: {data_count[key]}\n"18 data = {"label": class_name, 'data_count':data_count[key]}19 csv_data.append(data)20 if(output_dir is not None):21 save_csv(csv_columns, csv_data, output_dir, "DataCount")22 log_str += partial_str23 log_str += "=" * (len(first_line) - 1) + "\n"24 if 'b' in flags:25 tokens_count = statistics.count_tokens(preprocessed_base_dir)26 partial_str = "Label's tokens count:\n"27 csv_columns = ['label', 'token_count']28 csv_data =[]29 for key in tokens_count:30 class_name = classes_info.get_class_name(key)31 partial_str += f"{class_name}: {tokens_count[key]}\n"32 data = {"label": class_name, 'token_count': tokens_count[key]}33 csv_data.append(data)34 if(output_dir is not None):35 save_csv(csv_columns, csv_data, output_dir, "TokenCount")36 log_str += partial_str37 log_str += "=" * (len(first_line) - 1) + "\n" 38 if 'c' in flags:39 tokens_count = statistics.unique_tokens(preprocessed_base_dir)40 partial_str = "Label's unique tokens count:\n"41 csv_columns = ['label', 'token_count']42 csv_data =[]43 for key in tokens_count:44 class_name = classes_info.get_class_name(key)45 partial_str += f"{class_name}: {len(tokens_count[key])}\n"46 data = {"label": class_name, 'token_count':tokens_count[key]}47 csv_data.append(data)48 if(output_dir is not None):49 save_csv(csv_columns, csv_data, output_dir, "UniqueTokenCount")50 log_str += partial_str51 log_str += "=" * (len(first_line) - 1) + "\n"52 if 'd' in flags:53 common_tokens = statistics.common_tokens(preprocessed_base_dir)54 partial_str = "Label's common tokens count:\n"55 csv_columns = ['label', 'common_tokens']56 csv_data =[]57 for key in common_tokens:58 id1, id2 =key59 class_name1 = classes_info.get_class_name(id1)60 class_name2 = classes_info.get_class_name(id2)61 partial_str += f"{class_name1}-{class_name2}: {len(common_tokens[key])}\n"62 data = {"label" : f"{class_name1}-{class_name2}", "common_tokens": len(common_tokens[key])}63 csv_data.append(data)64 if(output_dir is not None):65 save_csv(csv_columns, csv_data, output_dir, "CommonTokensCount")66 log_str += partial_str67 log_str += "=" * (len(first_line) - 1) + "\n"68 if 'e' in flags:69 uncommon_tokens = statistics.uncommon_tokens(preprocessed_base_dir)70 partial_str = "Label's uncommon tokens count:\n"71 csv_columns = ['label', 'uncommon_tokens']72 csv_data =[]73 for key in uncommon_tokens:74 id1, id2 =key75 class_name1 = classes_info.get_class_name(id1)76 class_name2 = classes_info.get_class_name(id2)77 partial_str += f"{class_name1}-{class_name2}: {len(uncommon_tokens[key])}\n"78 data = {"label" : f"{class_name1}-{class_name2}", "uncommon_tokens": len(uncommon_tokens[key])}79 csv_data.append(data)80 if(output_dir is not None):81 save_csv(csv_columns, csv_data, output_dir, "UncommonTokensCount")82 log_str += partial_str83 log_str += "=" * (len(first_line) - 1) + "\n"84 if 'f' in flags:85 uncommon_tokens = statistics.most_repeated_uncommon_tokens(preprocessed_base_dir)86 partial_str = "Label's most repeated uncommon tokens: (word, repeated_count)\n"87 for key in uncommon_tokens:88 class_name = classes_info.get_class_name(key)89 partial_str += f"{class_name}: {uncommon_tokens[key][:10]}\n"90 log_str += partial_str91 log_str += "=" * (len(first_line) - 1) + "\n"92 if 'g' in flags:93 common_tokens = statistics.common_tokens_relfreq(preprocessed_base_dir)94 partial_str = "Label's common tokens sorted by RelativeNormalizeFreq: (word, relfreq)\n"95 csv_columns = ['token', 'relfreq']96 for key in common_tokens:97 id1, id2 =key98 class_name1 = classes_info.get_class_name(id1)99 class_name2 = classes_info.get_class_name(id2)100 partial_str += f"{class_name1}-{class_name2}: {common_tokens[key][:10]}\n"101 csv_data = []102 for word, relfreq in common_tokens[key][:10]:103 csv_data.append({"token": word, "relfreq": relfreq}) 104 if (output_dir is not None):105 save_csv(csv_columns, csv_data, output_dir, f"{class_name1}-{class_name2}_RelFreq")106 log_str += partial_str107 log_str += "=" * (len(first_line) - 1) + "\n"108 if 'h' in flags:109 tokens = statistics.sorted_words_tfidf(preprocessed_base_dir)110 partial_str = "Label's tokens sorted by TF-IDF: (word, tfidf)\n"111 csv_columns = ['token', 'tfidf']112 for key in tokens:113 class_name = classes_info.get_class_name(key)114 partial_str += f"{class_name}: {tokens[key][:10]}\n"115 csv_data =[]116 for word, tfidf in tokens[key][:10]:117 csv_data.append({"token": word, "tfidf": tfidf}) 118 if output_dir is not None:119 save_csv(csv_columns, csv_data, output_dir, f"{class_name}_TFIDF")120 log_str += partial_str121 log_str += "=" * (len(first_line) - 1) + "\n"122 if 'i' in flags:123 #TODO plot histogram and save it124 pass125 return log_str126# compute_statistics(PREPROCCESSED_DATA_DIR)127def save_csv(csv_columns, csv_data, base_dir, name):128 if not os.path.exists(base_dir):129 os.makedirs(base_dir, exist_ok=True)130 131 path = os.path.join(base_dir, f"{name}.csv")132 with open(path, 'w') as csvfile:133 writer = csv.DictWriter(csvfile, fieldnames=csv_columns)134 writer.writeheader()135 for data in csv_data:136 writer.writerow(data)137 138import argparse139import json140parser = argparse.ArgumentParser()141parser.add_argument("--flags", type=str ,default="abcdefghijkl")142parser.add_argument("--input", type=str, default=PREPROCCESSED_DATA_DIR)143parser.add_argument("--out", type=str, default=None)144args = parser.parse_args()145if __name__ == "__main__":146 preprocessed_base_dir = args.input147 flags = args.flags148 output_dir = args.out149 # print(output_dir)...
00017_letter_combination_of_a_phone_number.py
Source:00017_letter_combination_of_a_phone_number.py
1from typing import *2class Solution:3 def letterCombinations(self, digits: str) -> List[str]:4 d = {5 "2": "abc",6 "3": "def",7 "4": "ghi",8 "5": "jkl",9 "6": "mno",10 "7": "pqrs",11 "8": "tuv",12 "9": "wxyz",13 }14 n = len(digits)15 result = []16 def rec(i, partial_str):17 nonlocal digits18 nonlocal n19 nonlocal result20 if i >= n:21 if len(partial_str) > 0:22 result.append(partial_str)23 return24 digit = digits[i]25 candidates = d[digit]26 for char in candidates:27 rec(i + 1, partial_str + char)28 rec(0, "")29 return result30s = Solution()...
BOJ. 16916.py
Source:BOJ. 16916.py
1full_str = input()2partial_str = input()3# print(1 if partial_str in full_str else 0)4start_idx = 05end_idx = start_idx + len(partial_str) - 1 # idxëê¹ -1ì í´ì¤ì¼ í¨6str_to_check = full_str[start_idx: end_idx + 1]7for _ in range(len(full_str)-len(partial_str)+1): #8 if str_to_check == partial_str:9 print(1)10 break11 end_idx += 112 if end_idx == len(full_str):13 continue14 else:15 str_to_check = str_to_check[1:] + full_str[end_idx]16else:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!