How to use add_char method in hypothesis

Best Python code snippet using hypothesis

phonetics.py

Source:phonetics.py Github

copy

Full Screen

1# ----------------------------------------------------------2# AdvaS Advanced Search 3# module for phonetic algorithms4#5# (C) 2002 - 2005 Frank Hofmann, Chemnitz, Germany6# email fh@efho.de7# ----------------------------------------------------------8# changed 2005-01-249import string10import re11def soundex (term):12 "Return the soundex value to a string argument."13 # Create and compare soundex codes of English words.14 #15 # Soundex is an algorithm that hashes English strings into16 # alpha-numerical value that represents what the word sounds17 # like. For more information on soundex and some notes on the18 # differences in implemenations visit:19 # http://www.bluepoof.com/Soundex/info.html20 #21 # This version modified by Nathan Heagy at Front Logic Inc., to be22 # compatible with php's soundexing and much faster.23 #24 # eAndroid / Nathan Heagy / Jul 29 200025 # changes by Frank Hofmann / Jan 02 200526 # generate translation table only once. used to translate into soundex numbers27 #table = string.maketrans('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', '0123012002245501262301020201230120022455012623010202')28 table = string.maketrans('ABCDEFGHIJKLMNOPQRSTUVWXYZ', '01230120022455012623010202')29 # check parameter30 if not term:31 return "0000" # could be Z000 for compatibility with other implementations32 # end if33 # convert into uppercase letters34 term = string.upper(term)35 first_char = term[0]36 # translate the string into soundex code according to the table above37 term = string.translate(term[1:], table)38 39 # remove all 0s40 term = string.replace(term, "0", "")41 # remove duplicate numbers in-a-row42 str2 = first_char43 for x in term:44 if x != str2[-1]:45 str2 = str2 + x46 # end if47 # end for48 # pad with zeros49 str2 = str2+"0"*len(str2)50 # take the first four letters51 return_value = str2[:4]52 # return value53 return return_value54def metaphone (term):55 "returns metaphone code for a given string"56 # implementation of the original algorithm from Lawrence Philips57 # extended/rewritten by M. Kuhn58 # improvements with thanks to John Machin <sjmachin@lexicon.net>59 # define return value60 code = ""61 i = 062 term_length = len(term)63 if (term_length == 0):64 # empty string ?65 return code66 # end if67 # extension #1 (added 2005-01-28)68 # convert to lowercase69 term = string.lower(term)70 71 # extension #2 (added 2005-01-28)72 # remove all non-english characters, first73 term = re.sub(r'[^a-z]', '', term)74 if len(term) == 0:75 # nothing left76 return code77 # end if78 79 # extension #3 (added 2005-01-24)80 # conflate repeated letters81 firstChar = term[0]82 str2 = firstChar83 for x in term:84 if x != str2[-1]:85 str2 = str2 + x86 # end if87 # end for88 89 # extension #4 (added 2005-01-24)90 # remove any vowels unless a vowel is the first letter91 firstChar = str2[0]92 str3 = firstChar93 for x in str2[1:]:94 if (re.search(r'[^aeiou]', x)):95 str3 = str3 + x96 # end if97 # end for98 99 term = str3100 term_length = len(term)101 if term_length == 0:102 # nothing left103 return code104 # end if105 106 # check for exceptions107 if (term_length > 1):108 # get first two characters109 first_chars = term[0:2]110 # build translation table111 table = {112 "ae":"e",113 "gn":"n",114 "kn":"n",115 "pn":"n",116 "wr":"n",117 "wh":"w"118 }119 120 if first_chars in table.keys():121 term = term[2:]122 code = table[first_chars]123 term_length = len(term)124 # end if125 126 elif (term[0] == "x"):127 term = ""128 code = "s"129 term_length = 0130 # end if131 # define standard translation table132 st_trans = {133 "b":"b",134 "c":"k",135 "d":"t",136 "g":"k",137 "h":"h",138 "k":"k",139 "p":"p",140 "q":"k",141 "s":"s",142 "t":"t",143 "v":"f",144 "w":"w",145 "x":"ks",146 "y":"y",147 "z":"s"148 }149 i = 0150 while (i<term_length):151 # init character to add, init basic patterns152 add_char = ""153 part_n_2 = ""154 part_n_3 = ""155 part_n_4 = ""156 part_c_2 = ""157 part_c_3 = ""158 # extract a number of patterns, if possible159 if (i < (term_length - 1)):160 part_n_2 = term[i:i+2]161 if (i>0):162 part_c_2 = term[i-1:i+1]163 part_c_3 = term[i-1:i+2]164 # end if165 # end if166 if (i < (term_length - 2)):167 part_n_3 = term[i:i+3]168 # end if169 if (i < (term_length - 3)):170 part_n_4 = term[i:i+4]171 # end if172 # use table with conditions for translations173 if (term[i] == "b"):174 add_char = st_trans["b"]175 if (i == (term_length - 1)):176 if (i>0):177 if (term[i-1] == "m"):178 add_char = ""179 # end if180 # end if181 # end if182 elif (term[i] == "c"):183 add_char = st_trans["c"]184 if (part_n_2 == "ch"):185 add_char = "x"186 elif (re.search(r'c[iey]', part_n_2)):187 add_char = "s"188 # end if189 if (part_n_3 == "cia"):190 add_char = "x"191 # end if192 if (re.search(r'sc[iey]', part_c_3)):193 add_char = ""194 # end if195 elif (term[i] == "d"):196 add_char = st_trans["d"]197 if (re.search(r'dg[eyi]', part_n_3)):198 add_char = "j"199 # end if200 elif (term[i] == "g"):201 add_char = st_trans["g"]202 if (part_n_2 == "gh"):203 if (i == (term_length - 2)):204 add_char = ""205 # end if206 elif (re.search(r'gh[aeiouy]', part_n_3)):207 add_char = ""208 elif (part_n_2 == "gn"):209 add_char = ""210 elif (part_n_4 == "gned"):211 add_char = ""212 elif (re.search(r'dg[eyi]',part_c_3)):213 add_char = ""214 elif (part_n_2 == "gi"):215 if (part_c_3 != "ggi"):216 add_char = "j"217 # end if218 elif (part_n_2 == "ge"):219 if (part_c_3 != "gge"):220 add_char = "j"221 # end if222 elif (part_n_2 == "gy"):223 if (part_c_3 != "ggy"):224 add_char = "j"225 # end if226 elif (part_n_2 == "gg"):227 add_char = ""228 # end if229 elif (term[i] == "h"):230 add_char = st_trans["h"]231 if (re.search(r'[aeiouy]h[^aeiouy]', part_c_3)):232 add_char = ""233 elif (re.search(r'[csptg]h', part_c_2)):234 add_char = ""235 # end if236 elif (term[i] == "k"):237 add_char = st_trans["k"]238 if (part_c_2 == "ck"):239 add_char = ""240 # end if241 elif (term[i] == "p"):242 add_char = st_trans["p"]243 if (part_n_2 == "ph"):244 add_char = "f"245 # end if246 elif (term[i] == "q"):247 add_char = st_trans["q"]248 elif (term[i] == "s"):249 add_char = st_trans["s"]250 if (part_n_2 == "sh"):251 add_char = "x"252 # end if253 if (re.search(r'si[ao]', part_n_3)):254 add_char = "x"255 # end if256 elif (term[i] == "t"):257 add_char = st_trans["t"]258 if (part_n_2 == "th"):259 add_char = "0"260 # end if261 if (re.search(r'ti[ao]', part_n_3)):262 add_char = "x"263 # end if264 elif (term[i] == "v"):265 add_char = st_trans["v"]266 elif (term[i] == "w"):267 add_char = st_trans["w"]268 if (re.search(r'w[^aeiouy]', part_n_2)):269 add_char = ""270 # end if271 elif (term[i] == "x"):272 add_char = st_trans["x"]273 elif (term[i] == "y"):274 add_char = st_trans["y"]275 elif (term[i] == "z"):276 add_char = st_trans["z"]277 else:278 # alternative279 add_char = term[i]280 # end if281 code = code + add_char282 i += 1283 # end while284 # return metaphone code285 return code286def nysiis (term):287 "returns New York State Identification and Intelligence Algorithm (NYSIIS) code for the given term"288 code = ""289 i = 0290 term_length = len(term)291 if (term_length == 0):292 # empty string ?293 return code294 # end if295 # build translation table for the first characters296 table = {297 "mac":"mcc",298 "ph":"ff",299 "kn":"nn",300 "pf":"ff",301 "k":"c",302 "sch":"sss"303 }304 for table_entry in table.keys():305 table_value = table[table_entry] # get table value306 table_value_len = len(table_value) # calculate its length307 first_chars = term[0:table_value_len]308 if (first_chars == table_entry):309 term = table_value + term[table_value_len:]310 break311 # end if312 # end for313 # build translation table for the last characters314 table = {315 "ee":"y",316 "ie":"y",317 "dt":"d",318 "rt":"d",319 "rd":"d",320 "nt":"d",321 "nd":"d",322 }323 for table_entry in table.keys():324 table_value = table[table_entry] # get table value325 table_entry_len = len(table_entry) # calculate its length326 last_chars = term[(0 - table_entry_len):]327 #print last_chars, ", ", table_entry, ", ", table_value328 if (last_chars == table_entry):329 term = term[:(0 - table_value_len + 1)] + table_value330 break331 # end if332 # end for333 # initialize code334 code = term335 # transform ev->af336 code = re.sub(r'ev', r'af', code)337 # transform a,e,i,o,u->a338 code = re.sub(r'[aeiouy]', r'a', code)339 340 # transform q->g341 code = re.sub(r'q', r'g', code)342 343 # transform z->s344 code = re.sub(r'z', r's', code)345 # transform m->n346 code = re.sub(r'm', r'n', code)347 # transform kn->n348 code = re.sub(r'kn', r'n', code)349 # transform k->c350 code = re.sub(r'k', r'c', code)351 # transform sch->sss352 code = re.sub(r'sch', r'sss', code)353 # transform ph->ff354 code = re.sub(r'ph', r'ff', code)355 # transform h-> if previous or next is nonvowel -> previous356 occur = re.findall(r'([a-z]{0,1}?)h([a-z]{0,1}?)', code)357 #print occur358 for occur_group in occur:359 occur_item_previous = occur_group[0]360 occur_item_next = occur_group[1]361 if ((re.match(r'[^aeiouy]', occur_item_previous)) or (re.match(r'[^aeiouy]', occur_item_next))):362 if (occur_item_previous != ""):363 # make substitution364 code = re.sub (occur_item_previous + "h", occur_item_previous * 2, code, 1)365 # end if366 # end if367 # end for368 369 # transform w-> if previous is vowel -> previous370 occur = re.findall(r'([aeiouy]{1}?)w', code)371 #print occur372 for occur_group in occur:373 occur_item_previous = occur_group[0]374 # make substitution375 code = re.sub (occur_item_previous + "w", occur_item_previous * 2, code, 1)376 # end for377 378 # check last character379 # -s, remove380 code = re.sub (r's$', r'', code)381 # -ay, replace by -y382 code = re.sub (r'ay$', r'y', code)383 # -a, remove384 code = re.sub (r'a$', r'', code)385 386 # return nysiis code387 return code388def caverphone (term):389 "returns the language key using the caverphone algorithm 2.0"390 # Developed at the University of Otago, New Zealand.391 # Project: Caversham Project (http://caversham.otago.ac.nz)392 # Developer: David Hood, University of Otago, New Zealand393 # Contact: caversham@otago.ac.nz394 # Project Technical Paper: http://caversham.otago.ac.nz/files/working/ctp150804.pdf395 # Version 2.0 (2004-08-15)396 code = ""397 i = 0398 term_length = len(term)399 if (term_length == 0):400 # empty string ?401 return code402 # end if403 # convert to lowercase404 code = string.lower(term)405 # remove anything not in the standard alphabet (a-z)406 code = re.sub(r'[^a-z]', '', code)407 # remove final e408 if code.endswith("e"):409 code = code[:-1]410 # if the name starts with cough, rough, tough, enough or trough -> cou2f (rou2f, tou2f, enou2f, trough)411 code = re.sub(r'^([crt]|(en)|(tr))ough', r'\1ou2f', code)412 # if the name starts with gn -> 2n413 code = re.sub(r'^gn', r'2n', code)414 # if the name ends with mb -> m2415 code = re.sub(r'mb$', r'm2', code)416 # replace cq -> 2q417 code = re.sub(r'cq', r'2q', code)418 419 # replace c[i,e,y] -> s[i,e,y]420 code = re.sub(r'c([iey])', r's\1', code)421 422 # replace tch -> 2ch423 code = re.sub(r'tch', r'2ch', code)424 425 # replace c,q,x -> k426 code = re.sub(r'[cqx]', r'k', code)427 428 # replace v -> f429 code = re.sub(r'v', r'f', code)430 431 # replace dg -> 2g432 code = re.sub(r'dg', r'2g', code)433 434 # replace ti[o,a] -> si[o,a]435 code = re.sub(r'ti([oa])', r'si\1', code)436 437 # replace d -> t438 code = re.sub(r'd', r't', code)439 440 # replace ph -> fh441 code = re.sub(r'ph', r'fh', code)442 # replace b -> p443 code = re.sub(r'b', r'p', code)444 445 # replace sh -> s2446 code = re.sub(r'sh', r's2', code)447 448 # replace z -> s449 code = re.sub(r'z', r's', code)450 # replace initial vowel [aeiou] -> A451 code = re.sub(r'^[aeiou]', r'A', code)452 # replace all other vowels [aeiou] -> 3453 code = re.sub(r'[aeiou]', r'3', code)454 # replace j -> y455 code = re.sub(r'j', r'y', code)456 # replace an initial y3 -> Y3457 code = re.sub(r'^y3', r'Y3', code)458 459 # replace an initial y -> A460 code = re.sub(r'^y', r'A', code)461 # replace y -> 3462 code = re.sub(r'y', r'3', code)463 464 # replace 3gh3 -> 3kh3465 code = re.sub(r'3gh3', r'3kh3', code)466 467 # replace gh -> 22468 code = re.sub(r'gh', r'22', code)469 # replace g -> k470 code = re.sub(r'g', r'k', code)471 # replace groups of s,t,p,k,f,m,n by its single, upper-case equivalent472 for single_letter in ["s", "t", "p", "k", "f", "m", "n"]:473 otherParts = re.split(single_letter + "+", code)474 code = string.join(otherParts, string.upper(single_letter))475 476 # replace w[3,h3] by W[3,h3]477 code = re.sub(r'w(h?3)', r'W\1', code)478 # replace final w with 3479 code = re.sub(r'w$', r'3', code)480 # replace w -> 2481 code = re.sub(r'w', r'2', code)482 # replace h at the beginning with an A483 code = re.sub(r'^h', r'A', code)484 # replace all other occurrences of h with a 2485 code = re.sub(r'h', r'2', code)486 # replace r3 with R3487 code = re.sub(r'r3', r'R3', code)488 # replace final r -> 3489 code = re.sub(r'r$', r'3', code)490 # replace r with 2491 code = re.sub(r'r', r'2', code)492 # replace l3 with L3493 code = re.sub(r'l3', r'L3', code)494 495 # replace final l -> 3496 code = re.sub(r'l$', r'3', code)497 498 # replace l with 2499 code = re.sub(r'l', r'2', code)500 # remove all 2's501 code = re.sub(r'2', r'', code)502 # replace the final 3 -> A503 code = re.sub(r'3$', r'A', code)504 505 # remove all 3's506 code = re.sub(r'3', r'', code)507 # extend the code by 10 '1' (one)508 code += '1' * 10509 510 # take the first 10 characters511 caverphoneCode = code[:10]512 513 # return caverphone code...

Full Screen

Full Screen

#3-substring-unmatched.py

Source:#3-substring-unmatched.py Github

copy

Full Screen

1####### Problem 3 #############2# Assuming there are two words, wrd1 & wrd2, we say wrd1 is a sub-sequence of wrd2 if all the letters in wrd1 appear in wrd2 in the same order (but not necessarily contiguous). That is, you can add letters to any position in wrd1 to get wrd2. For instance, "song" is a substring of "absorbing" and "coast" is a substring of "contrast".3# Create a function add_char in recursion, taking in wrd1 & wrd2, where wrd1 is a substring of wrd2, which means that wrd1 is shorter than wrd2, and return value is a string containing the characters you need to add to wrd1 to get wrd2. 4# In the example above, you need to add the characters "abrbi" to "song" to get "absorbing", and you need to add "ntr" to "coast" to get "contrast".5# The letters in the string you return should be in the order you have to add them from left to right. If there are multiple characters in the wrd2 that could correspond to characters in wrd1, use the leftmost one. For example, add_char("toy", "tacophony") should return "acphon", not "caphon" because the first "t" in "toy" corresponds to the first "t" in "tacophony".6# def add_char(wrd1, wrd1):7# """8# Return a string containing the characters you need to add to wrd1 to get wrd2.9# add_char("eel", "heel")10# 'h'11# add_char ("we", "wean")12# 'an'13# add_char("rot", "rotation")14# 'ation'15# add_char("a", "appropriate") 16# 'ppropriate'17# add_char ("vax", "anit-vaxxer")18# 'anti-xer'19# add_char("facy", "efficacy")20# 'efic'21# add_char("toy", "tacophony")22# 'acphon'23 24# """25# """ Write your program here"""26def add_char(wrd1, wrd2):27 if(not(len(wrd1))): #if no more substring left to check, return all remaining 28 return wrd229 elif (wrd1 in wrd2): #if entire word matches, remove matching string from second word30 removeWord = wrd131 wrd1 = wrd1.replace(removeWord, '', 1)32 wrd2 = wrd2.replace(removeWord, '', 1)33 return add_char(wrd1, wrd2)34 else: #check letter by letter and remove matching letter from both word35 removeWord = wrd1[0]36 wrd1 = wrd1.replace(removeWord, '', 1)37 wrd2 = wrd2.replace(removeWord, '', 1)38 return add_char(wrd1, wrd2)39print(add_char("eel", "heel"))40print(add_char("we", "wean"))41print(add_char("rot", "rotation"))42print(add_char("a", "appropriate")) 43print(add_char("vax", "anti-vaxxer"))44print(add_char("facy", "efficacy"))45print(add_char("toy", "tacophony"))...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful