<commit_before># Helper object
App.CwbMultiwordTerm = Em.Object.extend
  word:     null
  min:      null
  max:      null

  isFirst:  false
  isLast:   false

  isFirstDidChange: ->
    # The first term cannot be preceded by an interval
    if @get('isFirst')
      @set('min', null)
      @set('max', null)


App.CwbMultiwordComponent = Em.Component.extend

  # Private variable that holds the currently displayed query, which is
  # transferred to the public query property on reception of a focusOut event
  # or a search action
  _query: null

  displayedQuery: (->
    @_query = @get('query')
    query = @_splitQueryTerms()

    dq = []
    min = null
    max = null

    query.forEach (item) =>
      m = item.match(/\[\]\{(.+)\}/)
      if m
        [min, max] = @_handleIntervalSpecification(m)
      else
        dq.push App.CwbMultiwordTerm.create
          word: item
          min: min
          max: max

        min = null
        max = null

    # Ugly hack that is needed because the #each Handlebars helper
    # does not provide array indices to the templates in the loop :-/
    dq[0].isFirst = true
    dq[dq.length-1].isLast = true
    dq
  ).property('query')


  displayedQueryDidChange: (->
    displayedQuery = @get('displayedQuery')

    terms = for term in displayedQuery
      min  = term.get('min')
      max  = term.get('max')
      word = term.get('word')
      res  = []

      if min or max
        res.push("[]{#{min ? ''},#{max ? ''}}")  # interval
      res.push word.replace(/\S+/g, '"$&"')      # word
      res.join(' ')

    # Assign the query value to a private variable, which will not be
    # transferred to the query property until we recieve a focusOut event.
    # Otherwise, displayedQuery will be updated in turn when query is set,
    # which leads to the input field that we are editing losing focus.
    @_query = terms.join(' ')
  ).observes('displayedQuery.@each.word',
      'displayedQuery.@each.min',
      'displayedQuery.@each.max')


  _splitQueryTerms: ->
    query = @get('query')
    query = query.replace(/"(.*?)"/g, '$1')
    query.split(/\s+/)


  _handleIntervalSpecification: (m) ->
    intervalText = m[1]
    min = max = null

    m2 = intervalText.match(/(\d+),/)
    min = m2[1] if m2

    m2 = intervalText.match(/,(\d+)/)
    max = m2[1] if m2

    [min, max]


  addTerm: ->
    query = @get('query')
    return unless query

    query += ' ""'
    @set('query', query)

<commit_msg>Add terms to displayedQuery rather than query<commit_after># Helper object
App.CwbMultiwordTerm = Em.Object.extend
  word:     null
  min:      null
  max:      null

  isFirst:  false
  isLast:   false

  isFirstDidChange: ->
    # The first term cannot be preceded by an interval
    if @get('isFirst')
      @set('min', null)
      @set('max', null)


App.CwbMultiwordComponent = Em.Component.extend

  # Private variable that holds the currently displayed query, which is
  # transferred to the public query property on reception of a focusOut event
  # or a search action
  _query: null

  displayedQuery: (->
    @_query = @get('query')
    query = @_splitQueryTerms()

    dq = []
    min = null
    max = null

    query.forEach (item) =>
      m = item.match(/\[\]\{(.+)\}/)
      if m
        [min, max] = @_handleIntervalSpecification(m)
      else
        dq.push App.CwbMultiwordTerm.create
          word: item
          min: min
          max: max

        min = null
        max = null

    # Ugly hack that is needed because the #each Handlebars helper
    # does not provide array indices to the templates in the loop :-/
    dq[0].isFirst = true
    dq[dq.length-1].isLast = true
    dq
  ).property('query')


  displayedQueryDidChange: (->
    displayedQuery = @get('displayedQuery')

    terms = for term in displayedQuery
      min  = term.get('min')
      max  = term.get('max')
      word = term.get('word')
      res  = []

      if min or max
        res.push("[]{#{min ? ''},#{max ? ''}}")  # interval
      res.push word.replace(/\S+/g, '"$&"')      # word
      res.join(' ')

    # Assign the query value to a private variable, which will not be
    # transferred to the query property until we recieve a focusOut event.
    # Otherwise, displayedQuery will be updated in turn when query is set,
    # which leads to the input field that we are editing losing focus.
    @_query = terms.join(' ')
  ).observes('displayedQuery.@each.word',
      'displayedQuery.@each.min',
      'displayedQuery.@each.max')


  _splitQueryTerms: ->
    query = @get('query')
    query = query.replace(/"(.*?)"/g, '$1')
    query.split(/\s+/)


  _handleIntervalSpecification: (m) ->
    intervalText = m[1]
    min = max = null

    m2 = intervalText.match(/(\d+),/)
    min = m2[1] if m2

    m2 = intervalText.match(/,(\d+)/)
    max = m2[1] if m2

    [min, max]


  addTerm: ->
    newTerm = App.CwbMultiwordTerm.create
      word: ''
      min: null
      max: null
      isLast: true

    dq = @get('displayedQuery')
    dq[dq.length-1].set('isLast', false)
    dq.pushObject(newTerm)


{'fraction_non_alphanumeric': 0.1326490327674694, 'fraction_numerical': 0.00493485984998026, 'mean_word_length': 2.3379446640316206, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6887947', 'n_tokens_mistral': 1677, 'n_tokens_neox': 1613, 'n_words': 482}