gpt4 book ai didi

regex - Powershell 中的高级模式匹配

转载 作者:行者123 更新时间:2023-12-02 22:16:02 25 4
gpt4 key购买 nike

希望你能帮我做点什么。感谢@mklement0,我得到了一个很棒的脚本,它按字母顺序匹配单词的最基本的初始模式。但是缺少的是全文搜索和选择。
当前脚本的示例,其中包含 Words.txt 文件中几个单词的小样本:

App
Apple
Apply
Sword
Swords
Word
Words

变成:

App
Sword
Word

这很棒,因为它确实缩小到每行的基本模式!然而,它一行一行的结果仍然有一个可以进一步缩小的模式,即“Word”(大写不重要),所以理想情况下,输出应该是:

App
Word

并且“Sword”被删除,因为它属于更基本的模式,前缀为“Word”。

您对如何实现这一目标有什么建议吗?请记住,这将是一个大约 25 万个单词的字典列表,所以我不会提前知道我在寻找什么

CODE(来自 related post ,仅处理前缀匹配):
$outFile = [IO.File]::CreateText("C:\Temp\Results.txt")   # Output File Location
$prefix = '' # initialize the prefix pattern

foreach ($line in [IO.File]::ReadLines('C:\Temp\Words.txt')) # Input File name.
{

if ($line -like $prefix)
{
continue # same prefix, skip
}

$line # Visual output of new unique prefix
$prefix = "$line*" # Saves new prefix pattern
$outFile.writeline($line) # Output file write to configured location
}

最佳答案

您可以尝试两步法:

  • 步骤 1:在按字母顺序排序的单词列表中找到唯一前缀列表。这是通过顺序读取行来完成的,因此只需要您将唯一前缀作为一个整体保存在内存中。
  • 第 2 步:按长度顺序对结果前缀进行排序并对其进行迭代,在每次迭代中检查手头的单词是否已经由结果列表中的子字符串表示。
  • 结果列表一开始是空的,只要手头的单词在结果列表中没有子字符串,它就会被附加到列表中。
  • 结果列表作为带有交替的正则表达式 ( | ) 实现,以便在单个操作中匹配所有已找到的唯一单词。

  • 你必须看看性能是否足够好;为了获得最佳性能,尽可能直接使用 .NET 类型。
    # Read the input file and build the list of unique prefixes, assuming
    # alphabetical sorting.
    $inFilePath = 'C:\Temp\Words.txt' # Be sure to use a full path.
    $uniquePrefixWords =
    foreach ($word in [IO.File]::ReadLines($inFilePath)) {
    if ($word -like $prefix) { continue }
    $word
    $prefix = "$word*"
    }

    # Sort the prefixes by length in ascending order (shorter ones first).
    # Note: This is a more time- and space-efficient alternative to:
    # $uniquePrefixWords = $uniquePrefixWords | Sort-Object -Property Length
    [Array]::Sort($uniquePrefixWords.ForEach('Length'), $uniquePrefixWords)

    # Build the result lists of unique shortest words with the help of a regex.
    # Skip later - and therefore longer - words, if they are already represented
    # in the result list of word by a substring.
    $regexUniqueWords = ''; $first = $true
    foreach ($word in $uniquePrefixWords) {
    if ($first) { # first word
    $regexUniqueWords = $word
    $first = $false
    } elseif ($word -notmatch $regexUniqueWords) {
    # New unique word found: add it to the regex as an alternation (|)
    $regexUniqueWords += '|' + $word
    }
    }

    # The regex now contains all unique words, separated by "|".
    # Split it into an array of individual words, sort the array again...
    $resultWords = $regexUniqueWords.Split('|')
    [Array]::Sort($resultWords)

    # ... and write it to the output file.
    $outFilePath = 'C:\Temp\Results.txt' # Be sure to use a full path.
    [IO.File]::WriteAllLines($outFilePath, $resultWords)

    关于regex - Powershell 中的高级模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61367263/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com