import re


def wordcount(text, unique=False):
  """Counts words in a string, optionally limiting to unique words."""
  words = re.findall(r'\w+', text.lower())
  if unique:
    words = set(words)
  return len(words)