Best Python code snippet using green
ssa_case_complex_no_sugar.py
Source:ssa_case_complex_no_sugar.py
1import os2import random3from collections import Counter, defaultdict4import random5from nltk.tag import StanfordNERTagger6from nltk.tokenize import word_tokenize7from nltk import pos_tag8from nltk.chunk import conlltags2tree9from nltk.tree import Tree10import pandas as pd11from htrc_features import FeatureReader12import geocoder13import folium14from pprint import pprint15from tqdm import tqdm16USERNAME = os.getenv('USERNAME')17st = StanfordNERTagger(18 '/usr/local/share/stanford-ner/classifiers/english.all.3class.distsim.crf.ser.gz'19 , '/usr/local/share/stanford-ner/stanford-ner.jar', encoding='utf-8')20def stanfordNE2BIO(tagged_sent):21 bio_tagged_sent = []22 prev_tag = 'O'23 for token, tag in tagged_sent:24 if tag == 'O':25 bio_tagged_sent.append((token, tag))26 prev_tag = tag27 continue28 if tag != 'O' and prev_tag == 'O':29 bio_tagged_sent.append((token, 'B-' + tag))30 prev_tag = tag31 elif prev_tag != 'O' and prev_tag == tag:32 bio_tagged_sent.append((token, 'I-' + tag))33 prev_tag = tag34 elif prev_tag != 'O' and prev_tag != tag:35 bio_tagged_sent.append((token, 'B-' + tag))36 prev_tag = tag37 return bio_tagged_sent38def stanfordNE2tree(ne_tagged_sent):39 bio_tagged_sent = stanfordNE2BIO(ne_tagged_sent)40 sent_tokens, sent_ne_tags = zip(*bio_tagged_sent)41 _hidden_sent_pos_tags = []42 for token, pos in pos_tag(sent_tokens):43 _hidden_sent_pos_tags.append(pos)44 sent_pos_tags = _hidden_sent_pos_tags45 _hidden_sent_conlltags = []46 for token, pos, ne in zip(sent_tokens, sent_pos_tags, sent_ne_tags):47 _hidden_sent_conlltags.append((token, pos, ne))48 sent_conlltags = _hidden_sent_conlltags49 ne_tree = conlltags2tree(sent_conlltags)50 return ne_tree51########### example52htid = 'wu.89079728994'53fr = FeatureReader(ids=[htid])54for vol in fr:55 tokens = vol.tokenlist()56temp = tokens.index.values.tolist()57########################58counts = pd.DataFrame.from_records(temp, columns=['page', 'section',59 'token', 'pos'])60counts['count'] = tokens['count'].tolist()61counts[:10]62_hidden_res_71_17 = zip(counts['token'].tolist(), counts['count'].tolist())63text_data = list(_hidden_res_71_17)64text_list = []65for w, c in text_data:66 for i in range(0, c):67 text_list.append(w)68random.shuffle(text_list)69text_reconstruction = ' '.join(text_list)70tokens = word_tokenize(text_reconstruction)71tagged_tokens = st.tag(tokens)72_hidden_tagged_tokens = []73for item in tagged_tokens:74 if item[0] != '':75 _hidden_tagged_tokens.append(item)76tagged_tokens = _hidden_tagged_tokens77ne_tree = stanfordNE2tree(tagged_tokens)78ne_in_sent = []79for subtree in ne_tree:80 if type(subtree) == Tree:81 ne_label = subtree.label()82 _hidden_res_88_29 = []83 for token, pos in subtree.leaves():84 _hidden_res_88_29.append(token)85 ne_string = ' '.join(_hidden_res_88_29)86 ne_in_sent.append((ne_string, ne_label))87_hidden_locations = []88for tag in ne_in_sent:89 if tag[1] == 'LOCATION':90 _hidden_locations.append(tag[0].title())91locations = _hidden_locations92print(locations)93most_common_locations = Counter(locations).most_common(10)94pprint(most_common_locations)95_hidden_places_list = []96for name, _ in most_common_locations:97 _hidden_places_list.append(name)98places_list = _hidden_places_list[:3]99most_common_locations = dict(most_common_locations)100geocoder_results = []101for place in places_list:102 results = geocoder.geonames(place, maxRows=5, key=USERNAME)103 jsons = []104 for result in results:105 jsons.append(result.json)106 geocoder_results.append(jsons)107countries = []108for results in geocoder_results:109 for item in results:110 if 'country' in item.keys():111 countries.append(item['country'])112top_country = sorted(Counter(countries))[0]113print(top_country)114coordinates = []115for i, results in enumerate(geocoder_results):116 for item in results:117 if item['country'] == top_country:118 coordinates.append((float(item['lat']), float(item['lng'])))119 break120print(places_list)121print(coordinates)122basemap = folium.Map(location=[37.97945, 23.71622], zoom_start=8, tiles=123 'cartodbpositron', width=960, height=512)124for i, c in enumerate(coordinates):125 folium.CircleMarker([c[0], c[1]], radius=most_common_locations[126 places_list[i]] * 0.25, color='#3186cc', fill=True, fill_opacity=127 0.5, fill_color='#3186cc', popup=128 '{} ({}, {}) appears {} times in book.'.format(places_list[i], c[0],129 c[1], most_common_locations[places_list[i]])).add_to(basemap)130print(131 'Map of relevant locations in Broneer et al.\'s "Ancient Corinth: A guide to the excavations," weighted by frequency.'132 )133basemap134page = 87135test = counts[counts['page'] == page]['token'].tolist()136print(test)137print(len(test))138from nltk.corpus import stopwords139_hidden_res_137_12 = stopwords.words('english')140stops = set(_hidden_res_137_12)141pns_list = []142for i in range(1, max(counts['page']) + 1):143 tokens = counts[counts['page'] == i]['token'].tolist()144 _hidden_tokens = []145 for token in tokens:146 if token.lower() not in stops and len(token) > 2:147 _hidden_tokens.append(token)148 tokens = _hidden_tokens149 _hidden_pns = []150 for token in tokens:151 if token[0].isupper():152 _hidden_pns.append(token)153 pns = _hidden_pns154 _hidden_combs = []155 for x, y in combinations(pns, 2):156 _hidden_combs.append(f'{x} {y}')157 combs = _hidden_combs158 pns_list.extend(combs)159print([x for x, y in Counter(pns_list).most_common(25)])160geocoder_results = []161for place in pns_list[:15]:162 results = geocoder.geonames(place, maxRows=5, key=USERNAME)163 jsons = []164 for result in results:165 jsons.append(result.json)166 geocoder_results.append(jsons)167geocoder_results168results = geocoder.geonames('Roman Forum', maxRows=5, key=USERNAME)169print(next(result.address for result in results))170g = geocoder.google('Al-Fayum')171print(g.latlng)172coordinates = [g.latlng]173basemap = folium.Map(location=[37.97945, 23.71622], zoom_start=8, tiles=174 'cartodbpositron', width=960, height=512)175for i, c in enumerate(coordinates):176 folium.CircleMarker([c[0], c[1]], radius=most_common_locations[177 places_list[i]] * 0.25, color='#3186cc', fill=True, fill_opacity=178 0.5, fill_color='#3186cc').add_to(basemap)179basemap180_hidden_test = []181for x, y in Counter(pns_list).most_common(10):182 _hidden_test.append(x)183test = _hidden_test184places = []185coordinates = []186for item in test:187 g = geocoder.google(item)188 if g:189 places.append(g.address)190 print(g.address)191 coordinates.append(g.latlng)192basemap = folium.Map(location=[37.97945, 23.71622], zoom_start=8, tiles=193 'cartodbpositron', width=960, height=512)194for i, c in enumerate(coordinates):195 folium.CircleMarker([c[0], c[1]], color='#3186cc', fill=True,196 fill_opacity=0.5, fill_color='#3186cc').add_to(basemap)...
test_constructs.py
Source:test_constructs.py
1import copy2from neuralogic.core.constructs.relation import BaseRelation, WeightedRelation3from neuralogic.core import R, Activation, Aggregation, Metadata, ActivationAgg, V4from neuralogic.core.constructs.rule import Rule5def test_predicate_creation() -> None:6 """Test different ways of predicate creation"""7 relation = R.lorem_ipsum8 predicate = relation / 19 assert predicate.arity == 110 assert predicate.name == "lorem_ipsum"11 assert predicate.special is False12 assert predicate.hidden is False13 predicate = relation / 214 assert predicate.arity == 215 assert relation.predicate.arity == 016 assert relation.predicate.name == "lorem_ipsum"17 assert relation.predicate.special is False18 assert relation.predicate.hidden is False19 relation = R.special.lorem_ipsum20 predicate = relation / 121 assert predicate.special is True22 assert predicate.hidden is False23 assert relation.predicate.special is True24 assert relation.predicate.hidden is False25 relation = R.hidden.lorem_ipsum26 predicate = relation / 127 assert predicate.special is False28 assert predicate.hidden is True29 assert relation.predicate.special is False30 assert relation.predicate.hidden is True31 relation = R.special.hidden.lorem_ipsum32 predicate = relation / 133 assert predicate.special is True34 assert predicate.hidden is True35 assert relation.predicate.special is True36 assert relation.predicate.hidden is True37 relation = R.special._hidden_test38 predicate = relation / 139 assert predicate.hidden is True40 assert predicate.name == "hidden_test"41 assert predicate.special is True42 relation = R._hidden_test43 predicate = relation / 144 assert predicate.hidden is True45 assert predicate.special is False46 assert predicate.name == "hidden_test"47 relation = R.get("_hidden_test")48 predicate = relation / 149 assert predicate.hidden is True50 assert predicate.special is False51 assert predicate.name == "hidden_test"52 predicate_metadata = R.shortest / 2 | [Activation.SIGMOID]53 assert predicate_metadata.metadata is not None54 assert predicate_metadata.metadata.aggregation is None55 assert predicate_metadata.metadata.activation == Activation.SIGMOID56 predicate_metadata = R.shortest / 2 | [Activation.SIGMOID(ActivationAgg.MAX)]57 assert predicate_metadata.metadata is not None58 assert predicate_metadata.metadata.aggregation is None59 assert str(predicate_metadata.metadata.activation) == "max-sigmoid"60 predicate_metadata = R.shortest / 2 | [ActivationAgg.MIN]61 assert predicate_metadata.metadata is not None62 assert predicate_metadata.metadata.aggregation is None63 assert str(predicate_metadata.metadata.activation) == "min-identity"64 predicate_metadata = R.shortest / 2 | Metadata(activation=ActivationAgg.MAX(Activation.TANH))65 assert predicate_metadata.metadata is not None66 assert predicate_metadata.metadata.aggregation == None67 assert str(predicate_metadata.metadata.activation) == "max-tanh"68def test_relation_creation() -> None:69 """Test relation creation related operations and properties"""70 relation = R.my_atom71 assert len(relation.terms) == 072 assert isinstance(relation, BaseRelation)73 relation = relation("a", "b")74 assert len(relation.terms) == 275 assert isinstance(relation, BaseRelation)76 relation = relation[1, 2]77 assert len(relation.terms) == 278 assert isinstance(relation, WeightedRelation)79 assert relation.weight == (1, 2)80 relation = R.my_atom["abc":1, 2]81 assert len(relation.terms) == 082 assert isinstance(relation, WeightedRelation)83 assert relation.weight == (1, 2)84 assert relation.weight_name == "abc"85 relation = relation.fixed()86 assert relation.is_fixed is True87 relation = R.my_atom88 neg_relation = -relation89 assert neg_relation.function is Activation.REVERSE90 neg_relation = ~relation91 assert neg_relation.function is Activation.REVERSE92 # t_relation = relation.T93 # assert t_relation.function is Activation.TRANSP94def test_rule_metadata():95 rule = (R.a <= R.b) | [Activation.SIGMOID, Aggregation.AVG]96 assert rule.metadata is not None97 assert rule.metadata.aggregation == Aggregation.AVG98 assert rule.metadata.activation == Activation.SIGMOID99 rule = (R.a <= R.b) | Metadata(activation=Activation.IDENTITY, aggregation=Aggregation.MAX)100 assert rule.metadata is not None101 assert rule.metadata.aggregation == Aggregation.MAX102 assert rule.metadata.activation == Activation.IDENTITY103 rule = R.a <= R.b104 assert rule.metadata is None105 rule = (R.a <= R.b) | [ActivationAgg.MAX(Activation.SIGMOID), Aggregation.AVG]106 assert rule.metadata is not None107 assert rule.metadata.aggregation == Aggregation.AVG108 assert str(rule.metadata.activation) == "max-sigmoid"109def test_rules():110 my_rule: Rule = R.a(V.X) <= R.special.alldiff(...)111 assert len(my_rule.body[0].terms) == 1112 assert my_rule.body[0].terms[0] == V.X113 my_rule: Rule = R.a(V.X) <= (R.special.alldiff(...), R.b(V.Y, V.Z))114 assert len(my_rule.body[0].terms) == 3115 terms = sorted(my_rule.body[0].terms)116 assert terms[0] == V.X117 assert terms[1] == V.Y118 assert terms[2] == V.Z119 my_rule = R.a <= R.b120 assert len(my_rule.body) == 1...
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!!