import conceptnet to work with weights
@@ -20,8 +20,13 @@ def add_arguments(parser: KGTKArgumentParser):
parser.add_input_file(positional=True)
parser.add_argument('--english_only', action="store_true", help="Only english conceptnet?")
parser.add_output_file()
+ parser.add_output_file(who="A KGTK output file that will contain only the weights.",
+ dest="weights_file",
+ options=["--weights-file"],
+ metavar="WEIGHTS_FILE",
+ optional=True)
-def run(input_file: KGTKFiles, english_only, output_file: KGTKFiles):
+def run(input_file: KGTKFiles, english_only, output_file: KGTKFiles, weights_file: KGTKFiles):
# import modules locally
import sys # type: ignore
@@ -43,6 +48,13 @@ def run(input_file: KGTKFiles, english_only, output_file: KGTKFiles):
def make_rel_label(rel):
return KgtkFormat.stringify(split_camel_case(rel.split('/')[-1]))
+ def make_weight_edge(row):
+
+ node1='%s-%s-%s-0000' % (row[2], row[1], row[3])
+ rel='weight'
+ node2=str(json.loads(row[-1])['weight'])
+ return [node1, rel, node2]
+
def row_to_edge(row, cols):
edge={}
@@ -82,11 +94,26 @@ def run(input_file: KGTKFiles, english_only, output_file: KGTKFiles):
#very_verbose=self.very_verbose
)
+ if weights_file:
+ info_kgtk_file: Path = KGTKArgumentParser.get_output_file(weights_file)
+ ew_aux: KgtkWriter = KgtkWriter.open(out_columns[:3],
+ info_kgtk_file,
+ #mode=input_kr.mode,
+ require_all_columns=False,
+ prohibit_extra_columns=True,
+ fill_missing_columns=True,
+ gzip_in_parallel=False,
+ #verbose=self.verbose,
+ #very_verbose=self.very_verbose
+ )
+
with open(filename, 'r') as f:
reader = csv.reader(f, delimiter='\t', quotechar='"')
for row in reader:
if not english_only or (row[2].startswith('/c/en/') and row[3].startswith('/c/en/')):
ew.write(row_to_edge(row, out_columns))
+ if weights_file and 'weight' in json.loads(row[-1]).keys():
+ ew_aux.write(make_weight_edge(row))
except Exception as e:
kgtk_exception_auto_handler(e)
