pattern.[ch]: Added CAPTURE feature counting number of stones.
[pachi.git] / tools / pattern_byplayer.sh
blobbf35d629b4895bffe1f10c9a7cdf8312a3aea6f4
1 #!/bin/sh
2 # pattern_byplayer - Build a per-player database of used patterns
4 # Invoke this script for each played game, it will add patterns
5 # to the database incrementally; each file in the database contains
6 # patterns played by one player.
8 # You must already have a spatial dictionary generated.
10 dbdir="$1"; shift
11 sgf="$1"; shift
12 # rest of parameters is passed to the patternscan engine
14 sgf_attr() {
15 # TODO
16 # Does not work correctly for attributes containing escaped ]
17 # e.g. "PB[Tony \[Killer\] Nowak]" gets cropped to
18 # "Tony \[Killer"
20 # Not a big problem, since usually the player name is just
21 # [a-zA-Z0-9]*
22 sed -n 's/^.*'$1'\[\([^]]*\)\].*$/\1/p' "$sgf"
25 black="$(sgf_attr PB)"
26 white="$(sgf_attr PW)"
27 handi="$(sgf_attr HA)"
29 if [ -n "$handi" ] && [ "$handi" -gt 0 ]; then
30 to_play=white
31 # Comment following out if you want to include handi games.
32 echo "$sgf: Skipping handicap game" >&2
33 exit 1
36 to_play=black
37 cat "$sgf" | tools/sgf2gtp.pl | ./pachi -d 0 -e patternscan "$@" |
38 sed -n -e 's/^= //p' | grep -v '^ *$' | # skip irrelevant replies
39 while read pattern; do
40 if [ "$to_play" = black ]; then
41 player="$black"
42 to_play=white
43 else
44 player="$white"
45 to_play=black
47 echo "$pattern" >>"$dbdir/$player"
48 done