Skip to content

Commit

Permalink
treesearch: add --no-filename option
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasvc committed Oct 17, 2018
1 parent 07e8f69 commit 647522e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
12 changes: 7 additions & 5 deletions discodop/treesearch.py
Expand Up @@ -1276,7 +1276,7 @@ def main():
shortoptions = 'e:m:M:stcbnofih'
options = ('engine= macros= numproc= max-count= slice= '
'trees sents brackets counts indices breakdown only-matching '
'line-number file ignore-case csv help')
'line-number file ignore-case no-filename csv help')
try:
opts, args = gnu_getopt(sys.argv[2:], shortoptions, options.split())
query, corpora = args[0], args[1:]
Expand All @@ -1302,6 +1302,8 @@ def main():
start, end = (int(start) if start else None), (int(end) if end else None)
# FIXME: support negative indices? why 1-based indices?
ignorecase = '--ignore-case' in opts or '-i' in opts
printfname = (len(corpora) > 1 and '--no-filename' not in opts
and '-h' not in opts)
if ignorecase and engine != 'regex':
raise ValueError('--ignore-case is only supported with --engine=regex')
if engine == 'tgrep2':
Expand Down Expand Up @@ -1332,10 +1334,10 @@ def main():
else:
for filename, cnt in searcher.counts(
query, start=start, end=end, indices=indices).items():
if len(corpora) > 1:
if printfname:
print('\x1b[%dm%s\x1b[0m:' % (
ANSICOLOR['magenta'], filename), end='')
print(cnt)
print(list(cnt) if indices else cnt)
elif '--trees' in opts or '-t' in opts:
results = searcher.trees(
query, start=start, end=end, maxresults=maxresults)
Expand All @@ -1355,7 +1357,7 @@ def main():
len(x.leaves()) if isinstance(x, Tree) else 1)
out = DrawTree(tree, sent, highlight=high).text(
unicodelines=True, ansi=True)
if len(corpora) > 1:
if printfname:
print('\x1b[%dm%s\x1b[0m:' % (
ANSICOLOR['magenta'], filename), end='')
if '--line-number' in opts or '-n' in opts:
Expand Down Expand Up @@ -1392,7 +1394,7 @@ def main():
for n, char in enumerate(sent))
else:
out = applyhighlight(sent, high1, high2)
if len(corpora) > 1:
if printfname:
print('\x1b[%dm%s\x1b[0m:' % (
ANSICOLOR['magenta'], filename), end='')
if '--line-number' in opts or '-n' in opts:
Expand Down
10 changes: 6 additions & 4 deletions docs/cli/treesearch.rst
Expand Up @@ -23,7 +23,7 @@ Options:
-s, --sents Output sentences (default); multiple queries can be given.
-t, --trees Output visualizations of trees.
-b, --brackets Output raw trees in the original corpus format.
--indices Report a sentence numebers of matches for each corpus
--indices Report sentence numbers of matches instead of counts
--breakdown Report counts of types that match query.
--csv Report counts in CSV format instead of the default flat format.
-f, --file Read queries (one per line) from filename given as first argument.
Expand All @@ -32,12 +32,14 @@ Options:
M may be left out; slice indexing is 1-based and inclusive.
-m N, --max-count=N
Stop after finding N matches; 0 for no limit.
-n, --line-number
Prefix each line of output with the sentence number within
its input file.
-o, --only-matching
Only output the matching portion
with ``--sents``, ``--trees``, and ``--brackets``.
-n, --line-number
Prefix each line of output with the sentence number within
its input file.
-h, --no-filename
Suppress the file name prefix when searching multiple files.
-i, --ignore-case
Ignore case in regex queries.
-M X, --macros=X
Expand Down

0 comments on commit 647522e

Please sign in to comment.