如何在给定位置的文件中获取单词?

时间:2018-05-06 22:33:51

标签: crystal-lang

如何在给定位置的文件中获取单词

def get_word(file, position)
  File.each_line(file).with_index do |line, line_number|
    if (line_number + 1) == position.line_number
      # How to get a word at position.column_number ?
    end
  end
end

这应该是这样的:

档案:message.md

Dear people:

My name is [Ángeliño](#angelino).

Bye!

致电:get_word

record Position, line_number : Int32, column_number : Int32

get_word("message.md", Position.new(1, 9))  # => people
get_word("message.md", Position.new(3, 20)) # => Ángeliño
get_word("message.md", Position.new(5, 3))  # => Bye!

2 个答案:

答案 0 :(得分:2)

也许,这会给你一个提示。请注意,此实现从不将标点符号视为单词的一部分,因此最后一个示例返回Bye而不是Bye!

def get_word_of(line : String, at position : Int)
  chunks = line.split(/(\p{P}|\p{Z})/)

  edge = 0
  hashes = chunks.map do |chunk|
    next if chunk.empty?
    {chunk => (edge + 1)..(edge += chunk.size)}
  end.compact

  candidate = hashes.find { |hash| hash.first_value.covers?(position) }
                    .try &.first_key

  candidate unless (candidate =~ /\A(?:\p{P}|\p{Z})+\Z/)
end

p get_word_of("Dear people:", 9)                       # => people
p get_word_of("My name is [Ángeliño](#angelino).", 20) # => Ángeliño
p get_word_of("Bye!", 3)                               # => Bye

答案 1 :(得分:-1)

获取字符串一部分的数字位置的一种方便方法是使用像这样的正则表达式:

filename = "/path/to/file"
File.each_line(filename).each_with_index do |line, line_number|
  term = "search-term"
  column = line =~ %r{#{term}}
  p "Found #{term} at line #{line_number}, column #{column}." if column
end

输出:"Found search-term at line 38, column 6"