Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes @prefix counting (in case of 0) & adds new cmd 'nssort' & minor things #23

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ rdf.sh currently provides these subcommands:
* nscollect: collects prefix declarations of a list of ttl/n3 files
* nsdist: distributes prefix declarations from one file
to a list of other ttl/n3 files
* nssort: sorts the prefix declarations in files
* put: replaces an existing linked data resource via LDP
* split: split an RDF file into pieces of max X triple and output the file names
* turtleize: outputs an RDF file in turtle,
Expand Down
95 changes: 85 additions & 10 deletions rdf
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ do_nscollect()
countBefore=0
fi

files=($(find . -name "*.ttl" | grep -v "$prefixfile"))
mapfile -t files < <(find . -name "*.ttl" | grep -v "$prefixfile")
rm -f "$prefixfile"
for file in "${files[@]}"
do
Expand Down Expand Up @@ -1000,24 +1000,29 @@ do_nsdist ()

if [ "${2:-}" == "" ]
then
files=($(find . -name "*.ttl" | grep -v "$prefixfile"))
mapfile -t files < <(find . -name "*.ttl" | grep -v "$prefixfile")
else
files=("$@")
fi

_count_prefixes() {
local ttl_file
ttl_file="$1"

grep "@prefix " -r "$ttl_file" 2> /dev/null | wc -l || true
}

count=$(wc -l < "$prefixfile")
tmpfile=$(_getTempFile)
for target in "${files[@]}"
do
if [ -f "$target" ]
then
# shellcheck disable=SC2126
before=$(grep "@prefix " < "$target" | wc -l || echo 0)
grep -v "@prefix " < "$target" >"$tmpfile" || true
cat $prefixfile >"$target"
cat "$tmpfile" >>"$target"
# shellcheck disable=SC2126
after=$(grep "@prefix " < "$target" | wc -l || echo 0)
before=$(_count_prefixes "$target")
grep -v "@prefix " < "$target" > "$tmpfile" || true
cat $prefixfile > "$target"
cat "$tmpfile" >> "$target"
after=$(_count_prefixes "$target")
result=$((after - before)) || true
if [ "$result" -ge "0" ]
then
Expand All @@ -1030,6 +1035,76 @@ do_nsdist ()
rm "$tmpfile"
}

docu_nssort () { echo "sorts the prefix declarations in files, re-inserting all at the line of the first found prefix";}
do_nssort ()
{
local before prefixes after
local -a files

_checkTool awk mktemp

if [ "${2:-}" == "" ]
then
>&2 echo "Syntax: $this $command <target-turtle-files>"
>&2 echo "($(docu_nssort))"
exit 1
else
files=("$@")
fi

tmpfile=$(_getTempFile)
for target in "${files[@]}"
do
if [ -f "$target" ]
then
awk '
BEGIN {
nBef = 1
nPref = 1
nAft = 1
}

/^@prefix/ {
lStart = sprintf("%s %-8s", $1, $2)
$1=$2=""
aPref[nPref++] = sprintf("%s%s", lStart, $0)
# skips processing this line further
next
}

{
if (nPref < 2) {
aBef[nBef++] = $0
} else {
aAft[nAft++] = $0
}
}

END {
# sorts the prefixes
# NOTE This is a GNU AWK (gawk) function.
# If your AWK does not suport it, install gawk
# or add an asort implemention here in the code.
nPref = asort(aPref)
# Do not know why, but this is necessary
nBef--
nAft--
for (i = 1; i <= nBef; i++) {
print aBef[i]
}
for (i = 1; i <= nPref; i++) {
print aPref[i]
}
for (i = 1; i <= nAft; i++) {
print aAft[i]
}
}' < "$target" > "$tmpfile"
mv "$tmpfile" "$target"
echo "$target: prefixes sorted"
fi
done
}

docu_turtleize() { echo "outputs an RDF file in turtle, using as much as possible prefix declarations"; }
do_turtleize ()
{
Expand Down Expand Up @@ -1077,7 +1152,7 @@ do_skolemize ()
tmpOutput="/tmp/$RANDOM-$(basename "$file")"
rapper -i guess "$file" 2>/dev/null | sort -u >"$tmpInput" || true

# in order to have stable uuids with the same file, change seed to md5sum of the file
# in order to have stable UUIDs with the same file, change seed to md5sum of the file
seed="$tmpInput/"
awk -v domain="$domain" -v seed="$seed" '
BEGIN{FS=" "; OFS=" "; ORS=""}
Expand Down
1 change: 1 addition & 0 deletions rdf.1
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ This includes inspecting resources and namespaces, listing of schema elements an
ns -- curls the namespace from prefix.cc
nscollect -- collects prefix declarations of a list of ttl/n3 files
nsdist -- distributes prefix declarations from one file to a list of other ttl/n3 files
nssort -- sorts the prefix declarations in files, inserting all at the line of the first found prefix
ping -- sends a semantic pingback request from a source to a target or to all possible targets
pingall -- sends a semantic pingback request to all possible targets of a given resource
put -- replaces an existing linked data resource via LDP
Expand Down