def main(): parser = argparse.ArgumentParser(description="Delete a synset") parser.add_argument('id', metavar='ID', type=str, help="The identifier of the synset to delete") parser.add_argument('--reason', type=str, help="A justification for deleting this synset") args = parser.parse_args() # Slightly speeds up the loading of WordNet if not os.path.exists("wn.pickle") or os.path.getmtime("wn.pickle") < os.path.getmtime("wn.xml"): print("Loading wordnet") wn = wordnet.parse_wordnet("wn.xml") pickle.dump(wn, open("wn.pickle", "wb")) else: wn = pickle.load(open("wn.pickle", "rb")) synset = wn.synset_by_id(args.id) if not synset: print("Could not find the synset %s" % args.id) sys.exit(-1) input("This will permanently delete a synset. Are you sure this wouldn't be better handled with a merge? Press any key to continue...") delete_synset(wn, synset)
def delete_rel(source, target): """Delete all relationships between two synsets""" print("Delete %s =*=> %s" % (source.id, target.id)) wn_source = wordnet.parse_wordnet("src/wn-%s.xml" % source.lex_name) ss = wn_source.synset_by_id(source.id) ss.synset_relations = [r for r in ss.synset_relations if r.target != target.id] with open("src/wn-%s.xml" % source.lex_name, "w") as out: wn_source.to_xml(out, True)
def main(): parser = argparse.ArgumentParser( description="Change a definition within the wordnet") parser.add_argument( 'id', metavar='ID', type=str, nargs="?", help="The ID of the synset (sense) for the relationship") parser.add_argument( '--add', action='store_true', help= "Add the new definition and retain the previous definition (otherwise this definition replaces previous definitions)" ) parser.add_argument('--defn', type=str, help="The new definition") parser.add_argument('--ili', action='store_true', help="Set the ILI definition") args = parser.parse_args() # Slightly speeds up the loading of WordNet if not os.path.exists("wn.pickle") or os.path.getmtime( "wn.pickle") < os.path.getmtime("wn.xml"): print("Loading wordnet") wn = wordnet.parse_wordnet("wn.xml") pickle.dump(wn, open("wn.pickle", "wb")) else: wn = pickle.load(open("wn.pickle", "rb")) if not args.id: id = "oewn-" + input("Enter synset ID : oewn-") else: id = args.id synset = wn.synset_by_id(id) if not synset: print("Could not find the synset %s" % args.id) sys.exit(-1) if args.ili: if not args.defn: args.defn = synset.definitions[0].text update_ili_def(wn, synset, args.defn) else: if not args.defn: print("Definition : " + synset.definitions[0].text) defn = input("New Definition : ") else: defn = args.defn update_def(wn, synset, defn, args.add) change_manager.save_all_xml(wn)
def delete_ex(wn, synset, example): wn_synset = wordnet.parse_wordnet("src/xml/wn-%s.xml" % synset.lex_name) ss = wn_synset.synset_by_id(synset.id) n_exs = len(ss.examples) ss.examples = [ex for ex in ss.examples if ex.text != example] if len(ss.examples) == n_exs: print("No change") else: with open("src/xml/wn-%s.xml" % synset.lex_name, "w") as out: wn_synset.to_xml(out, True)
def insert_sense_rel(wn, source, rel_type, target): """Insert a single relation between two senses""" print("Insert %s =%s=> %s" % (source, rel_type, target)) (source_synset, source_entry) = decompose_sense_id(source) lex_name = wn.synset_by_id(source_synset).lex_name wn_source = wordnet.parse_wordnet("src/wn-%s.xml" % lex_name) entry = wn_source.entry_by_id(source_entry) sense = [sense for sense in entry.senses if sense.id == source][0] sense.sense_relations.append(wordnet.SenseRelation(target, rel_type)) with open("src/wn-%s.xml" % lex_name, "w") as out: wn_source.to_xml(out, True)
def update_def(wn, synset, defn, add): print("Previous definitions:") for d in synset.definitions: print("> " + d.text) wn_synset = wordnet.parse_wordnet("src/wn-%s.xml" % synset.lex_name) ss = wn_synset.synset_by_id(synset.id) if add: ss.definitions = ss.definitions + [wordnet.Definition(defn)] else: ss.definitions = [wordnet.Definition(defn)] with open("src/wn-%s.xml" % synset.lex_name, "w") as out: wn_synset.to_xml(out, True)
def delete_sense_rel(wn, source, target): """Delete all relationships between two senses""" print("Delete %s =*=> %s" % (source, target)) (source_synset, source_entry) = decompose_sense_id(source) lex_name = wn.synset_by_id(source_synset).lex_name wn_source = wordnet.parse_wordnet("src/wn-%s.xml" % lex_name) entry = wn_source.entry_by_id(source_entry) sense = [sense for sense in entry.senses if sense.id == source][0] sense.sense_relations = [ r for r in sense.sense_relations if r.target != target ] with open("src/wn-%s.xml" % lex_name, "w") as out: wn_source.to_xml(out, True)
def insert_rel(source, rel_type, target): """Insert a single relation between two synsets""" print("Insert %s =%s=> %s" % (source.id, rel_type, target.id)) wn_source = wordnet.parse_wordnet("src/wn-%s.xml" % source.lex_name) ss = wn_source.synset_by_id(source.id) if [ r for r in ss.synset_relations if r.target == target.id and r.rel_type == rel_type ]: print("Already exists") return ss.synset_relations.append(wordnet.SynsetRelation(target.id, rel_type)) with open("src/wn-%s.xml" % source.lex_name, "w") as out: wn_source.to_xml(out, True)
def update_def(wn, synset, defn, add): spell = Speller(lang='en') if any([spell(w) != w for w in defn.split()]): if input("There may be spelling errors in this definition. Proceed [y/N] : ") != "y": sys.exit(-1) print("Previous definitions:") for d in synset.definitions: print("> " + d.text) wn_synset = wordnet.parse_wordnet("src/wn-%s.xml" % synset.lex_name) ss = wn_synset.synset_by_id(synset.id) if add: ss.definitions = ss.definitions + [wordnet.Definition(defn)] else: ss.definitions = [wordnet.Definition(defn)] with open("src/wn-%s.xml" % synset.lex_name, "w") as out: wn_synset.to_xml(out, True)
def main(): parser = argparse.ArgumentParser( description="Add (or delete) an example of a synset") parser.add_argument( 'id', metavar='ID', type=str, help="The ID of the synset (sense) for the relationship") parser.add_argument('--delete', action='store_true', help="Delete this definition instead of adding it") parser.add_argument('--example', type=str, help="The new example") args = parser.parse_args() # Slightly speeds up the loading of WordNet if not os.path.exists("wn.pickle") or os.path.getmtime( "wn.pickle") < os.path.getmtime("wn.xml"): print("Loading wordnet") wn = wordnet.parse_wordnet("wn.xml") pickle.dump(wn, open("wn.pickle", "wb")) else: wn = pickle.load(open("wn.pickle", "rb")) synset = wn.synset_by_id(args.id) if not synset: print("Could not find the synset %s" % args.id) sys.exit(-1) if not args.example: print("Please specify an example") sys.exit(-1) if not args.example.startswith("\""): print("Examples must start and end with a quotation") sys.exit(-1) if args.delete: delete_ex(wn, synset, args.example) else: add_ex(wn, synset, args.example) change_manager.save_all_xml(wn)
def main(): parser = argparse.ArgumentParser( description="Change a definition within the wordnet") parser.add_argument( 'id', metavar='ID', type=str, help="The ID of the synset (sense) for the relationship") parser.add_argument( '--add', action='store_true', help= "Add the new definition and retain the previous definition (otherwise this definition replaces previous definitions)" ) parser.add_argument('--defn', type=str, help="The new definition") args = parser.parse_args() # Slightly speeds up the loading of WordNet if not os.path.exists("wn.pickle") or os.path.getmtime( "wn.pickle") < os.path.getmtime("wn.xml"): print("Loading wordnet") wn = wordnet.parse_wordnet("wn.xml") pickle.dump(wn, open("wn.pickle", "wb")) else: wn = pickle.load(open("wn.pickle", "rb")) synset = wn.synset_by_id(args.id) if not synset: print("Could not find the synset %s" % args.id) sys.exit(-1) if not args.defn: print("Please specify a definition") sys.exit(-1) update_def(wn, synset, args.defn, args.add)
def main(): wn = wordnet.parse_wordnet("wn.xml") errors = 0 for entry in wn.entries: if not is_valid_id(entry.id): print("ERROR: Invalid ID " + entry.id) errors += 1 for sense in entry.senses: if not is_valid_id(sense.id): print("ERROR: Invalid ID " + sense.id) errors += 1 for synset in wn.synsets: if not is_valid_id(synset.id): print("ERROR: Invalid ID " + synset.id) errors += 1 if errors > 0: print("Validation failed. %d errors" % errors) sys.exit(-1) else: print("No validity issues")
def update_ili_def(wn, synset, defn): wn_synset = wordnet.parse_wordnet("src/xml/wn-%s.xml" % synset.lex_name) ss = wn_synset.synset_by_id(synset.id) ss.ili_definition = wordnet.Definition(defn) with open("src/xml/wn-%s.xml" % synset.lex_name, "w") as out: wn_synset.to_xml(out, True)
import wordnet import nltk wn = wordnet.parse_wordnet("wn31.xml") for entry in wn.entries: if " " in entry.lemma.written_form: tokens = nltk.word_tokenize(entry.lemma.written_form) tags = nltk.pos_tag(tokens) if (all(tags[i][1] == "JJ" for i in range(len(tags)-1)) and (tags[-1][1] == "NN" or tags[-1][1] == "NNS")): print(entry.lemma.written_form)
def add_ex(wn, synset, example): wn_synset = wordnet.parse_wordnet("src/xml/wn-%s.xml" % synset.lex_name) ss = wn_synset.synset_by_id(synset.id) ss.examples = ss.examples + [wordnet.Example(example)] with open("src/xml/wn-%s.xml" % synset.lex_name, "w") as out: wn_synset.to_xml(out, True)
def main(): parser = argparse.ArgumentParser( description="Change a relationship within the wordnet") parser.add_argument( 'source_id', metavar='SOURCE_ID', type=str, help="The ID of the source synset (sense) for the relationship") parser.add_argument( 'target_id', metavar='TARGET_ID', type=str, help="The ID of the target synset (sense) for the relationship") parser.add_argument('--new-source', type=str, help="The ID of the new source synset") parser.add_argument('--new-target', type=str, help="The ID of the new target synset") parser.add_argument('--new-relation', type=str, help="The type of the new relationship") parser.add_argument('--add', action='store_true', help="Add this relation as a new relation") parser.add_argument('--delete', action='store_true', help="Remove this relation (do not replace or change)") args = parser.parse_args() # Slightly speeds up the loading of WordNet if not os.path.exists("wn.pickle") or os.path.getmtime( "wn.pickle") < os.path.getmtime("wn.xml"): print("Loading wordnet") wn = wordnet.parse_wordnet("wn.xml") pickle.dump(wn, open("wn.pickle", "wb")) else: wn = pickle.load(open("wn.pickle", "rb")) if sense_id_re.match(args.source_id): (source_id, source_entry_id) = decompose_sense_id(args.source_id) else: source_id = args.source_id source_entry_id = None source_synset = wn.synset_by_id(source_id) if not source_synset: print("Could not find the source synset %s" % source_id) sys.exit(-1) if sense_id_re.match(args.target_id): (target_id, target_entry_id) = decompose_sense_id(args.target_id) else: target_id = args.target_id target_entry_id = None target_synset = wn.synset_by_id(target_id) if not target_synset: print("Could not find the target synset %s" % target_id) sys.exit(-1) if args.new_source: if args.new_target or args.new_relation: print("Please perform a single change at a time") sys.exit(-1) if args.add or args.delete: print( "Specifying new source when adding or deleting does not make sense" ) sys.exit(-1) if source_entry_id or target_entry_id: if not sense_exists(wn, args.source_id): print("Source sense %d does not exist" % args.source_id) sys.exit(-1) if not sense_exists(wn, args.target_id): print("Target sense %d does not exist" % args.target_id) sys.exit(-1) if not sense_exists(wn, args.new_source): print("New source sense %d does not exist" % args.new_source) sys.exit(-1) update_source_sense(wn, args.source_id, args.target_id, args.new_source) else: new_source = wn.synset_by_id(args.new_source) if not new_source: print("Could not find the new source synset %s" % args.new_source) sys.exit(-1) update_source(wn, source_synset, target_synset, new_source) elif args.new_target: if args.new_source or args.new_relation: print("Please perform a single change at a time") sys.exit(-1) if args.add or args.delete: print( "Specifying new source when adding or deleting does not make sense" ) sys.exit(-1) if source_entry_id or target_entry_id: if not sense_exists(wn, args.source_id): print("Source sense %d does not exist" % args.source_id) sys.exit(-1) if not sense_exists(wn, args.target_id): print("Target sense %d does not exist" % args.target_id) sys.exit(-1) if not sense_exists(wn, args.new_target): print("New target sense %d does not exist" % args.new_target) sys.exit(-1) update_target_sense(wn, args.source_id, args.target_id, args.new_target) else: new_target = wn.synset_by_id(args.new_target) if not new_target: print("Could not find the new target synset %s" % args.new_target) sys.exit(-1) update_target(wn, source_synset, target_synset, new_target) elif args.new_relation: if args.new_source or args.new_target: print("Please perform a single change at a time") sys.exit(-1) if source_entry_id: if args.new_relation not in wordnet.SenseRelType._value2member_map_: print("Not a valid relation type %s" % args.new_relation) sys.exit(-1) else: if args.new_relation not in wordnet.SynsetRelType._value2member_map_: print("Not a valid relation type %s" % args.new_relation) sys.exit(-1) if args.add: if args.delete: print("Cannot both add and delete a relation") sys.exit(-1) if source_entry_id or target_entry_id: if not sense_exists(wn, args.source_id): print("Source sense %d does not exist" % args.source_id) sys.exit(-1) if not sense_exists(wn, args.target_id): print("Target sense %d does not exist" % args.target_id) sys.exit(-1) add_sense_relation(wn, args.source_id, args.target_id, wordnet.SenseRelType(args.new_relation)) else: add_relation(wn, source_synset, target_synset, wordnet.SynsetRelType(args.new_relation)) elif args.delete: if source_entry_id or target_entry_id: if not sense_exists(wn, args.source_id): print("Source sense %d does not exist" % args.source_id) sys.exit(-1) if not sense_exists(wn, args.target_id): print("Target sense %d does not exist" % args.target_id) sys.exit(-1) delete_sense_relation(wn, args.source_id, args.target_id) else: delete_relation(wn, source_synset, target_synset) else: if source_entry_id or target_entry_id: if not sense_exists(wn, args.source_id): print("Source sense %d does not exist" % args.source_id) sys.exit(-1) if not sense_exists(wn, args.target_id): print("Target sense %d does not exist" % args.target_id) sys.exit(-1) update_sense_relation(wn, args.source_id, args.target_id, wordnet.SenseRelType(args.new_relation)) else: update_relation(wn, source_synset, target_synset, wordnet.SynsetRelType(args.new_relation)) elif args.delete: if args.add: print("Cannot both add and delete a relation") sys.exit(-1) if source_entry_id or target_entry_id: if not sense_exists(wn, args.source_id): print("Source sense %d does not exist" % args.source_id) sys.exit(-1) if not sense_exists(wn, args.target_id): print("Target sense %d does not exist" % args.target_id) sys.exit(-1) delete_sense_relation(wn, args.source_id, args.target_id) else: delete_relation(wn, source_synset, target_synset) else: print("No change specified")