[CWB] Using the Perl API

Stefan Evert stefanML at collocations.de
Mon Sep 6 16:08:55 CEST 2010


> I'm trying to use the Perl API for version 3.0 and I have encountered a problem:
> 
> The "system commands" (such as show corpora, choosing a corpus to use and so on) work fine and I can receive their output (using getline or the output of exec method);
> however, running  a query (no matter how simple it is and no matter how many results I get when running it from the CQP command line), I get nothing from the API methods.

The Perl interface runs CQP in a special mode as a backend process.  One of the consequences is that the results of a query are no longer displayed automatically -- you have to run a "cat" command explicitly.

The "standard" way of running queries in CWB::CQP is the following:

	$cqp = new CWB::CQP;  # or whatever initialisation you need
	# activate your corpus etc.

	$cqp->exec("Matches = <your query>");
	($n_matches) = $cqp->exec("size Matches");
	# now you can skip processing if there are 0 matches, or "reduce Matches" if there are too many
	@lines = $cqp->exec("cat Matches"); # or use "tabulate" to get exactly the information you need

	# somewhat slower but more memory-efficient for large result sets:
	$cqp->run("cat Matches");
	while (defined $line = $cqp->getline) {
		# process this result line
	}

If you want to see interactively what this special mode looks like, run CQP as follows:

	cqp -c  # -c = "child process mode"
	set PrettyPrint off;

Note that there are no input prompts or command-line editing facilities in this mode.

Best,
Stefan



More information about the CWB mailing list