代码之家  ›  专栏  ›  技术社区  ›  forJ

如何搜索具有特定字符索引的特定单词索引

  •  0
  • forJ  · 技术社区  · 6 年前

    举例来说,我有下面的话

    THIS TEXT IS A SAMPLE TEXT
    

    我的性格指数是7。

    然后,当我将句子拆分成单词时,我必须返回索引1,该单词的索引包含字符索引而不是5,该索引与组成字符索引的单词完全匹配,但不是字符所在的正确索引。

    基本上,我试图用字符索引返回字符所在位置的正确单词索引(拆分为单词时)和字符索引(拆分为字符时)

    我想我可以用下面这样的词来重建这个词,以便在这个字符处找到这个词

    let curString = 'find a word from here';
    let initialPositin = 5
    let position = initialPositin
    
    let stringBuilder = '';
    
    while(position > -1 && curString.charAt(position) !== ' '){
      console.log('run 1')
      console.log(position);
    
      stringBuilder = curString.charAt(position) + stringBuilder;
    
      position --;
    }
    
    console.log(stringBuilder)
    
    position = initialPositin + 1;
    
    while(position < curString.length && curString.charAt(position) !== ' '){
      console.log('run 2')
    
      stringBuilder += curString.charAt(position);
    
      position ++;
    }
    
    console.log(stringBuilder);
    

    然后把句子分成几个词,然后找出包含我所构造的词的所有索引。然后检查所有找到的单词,并重建之前的单词,看看重建中目标字符的索引是否与给定的字符位置匹配。

    感觉效率不高。有人有更好的建议吗?

    我更喜欢javascript,但我可以自己翻译其他语言

    5 回复  |  直到 6 年前
        1
  •  3
  •   miradham    6 年前

    我想你可以在给定索引之前计算空格,比如

    let curString = 'find a word from here';
    let givenIndex = 9;
    
    let spaceIndex = 0;
    for (var i = 0; i < curString.length; i++) {
      if(curString.charAt(i) == ' ') {
          if (i < givenIndex) {
              spaceIndex++;
          } else {
              // found what we need
              console.log(spaceIndex);
          }
      }
    }
    
        2
  •  0
  •   Jacques Ung    6 年前

    也许您可以构建一个返回所有空间位置的函数。 然后可以看到字符索引在空间位置列表中的位置。

        3
  •  0
  •   Burak Buğrul    6 年前
    text = "THIS TEXT IS A SAMPLE TEXT"
    indexes = []
    current_word = 0
    
    for i in range(0, len(text)):
    
        if text[i] == ' ':
            current_word += 1  # After a ' ' character, we passed a word
        else:
            indexes.append(current_word)  # current character belongs to current word
    

    使用这段代码(用Python3编写)可以构建索引数组一次,然后可以对每个索引使用它。如果还想计算索引数组中的“”个字符,可以简单地将它们添加到for循环(in If语句)中。

        4
  •  0
  •   forJ    6 年前

    我最终使用了下面的代码

    let content = 'THIS IS A SAMPLE SENTENCE';
    let target = 13;
    
    let spaceCount = 0;
    let index = 0;
    
    while(index < target) {
    
        if (content.charAt(index) === ' ') {
            spaceCount++;
        }
    
        index++;
    }
    
    let splitContent = content.split(' ');
    splitContent[spaceCount] = '#' + value
    console.log(splitContent.join(' '))
    

    工作得很好

        5
  •  0
  •   Damiaan Dufaux    6 年前

    就像@miradham的答案一样,这个函数计算给定索引之前的空格,但是使用内置函数计算字符出现次数。

    function wordIndexOfCharacterIndexInString(index, string) {
      const stringUpToCharacter = string.slice(0, index)
      return (stringUpToCharacter.match(/ /g) || []).length
    }
    
    console.log(wordIndexOfCharacterIndexInString(7, "THIS TEXT IS A SAMPLE TEXT"))