第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何拆分多個連接詞?

如何拆分多個連接詞?

守著一只汪 2019-10-05 15:26:22
我有大約1000個條目的數組,下面是示例:wickedweatherliquidweatherdriveourtrucksgocompactslimprojector我希望能夠將它們分為各自的詞,例如:wicked weatherliquid weatherdrive our trucksgo compactslim projector我希望我能做到一個正則表達式。但是,由于我沒有止境可言,因此我也沒有可能要大寫的任何大寫字母,因此可能需要某種對字典的引用?我想可以手工完成,但是為什么-什么時候可以用代碼完成!=)但是,這讓我感到難過。有任何想法嗎?
查看完整描述

3 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

最好的工具是遞歸,而不是正則表達式。基本思想是從字符串的開頭開始尋找一個單詞,然后從字符串的其余部分開始尋找另一個單詞,依此類推,直到到達字符串的末尾。遞歸解決方案是很自然的,因為當字符串的給定其余部分不能分解為一組單詞時,需要進行回溯。下面的解決方案使用詞典來確定什么是單詞,并在找到它們時打印出解決方案(一些字符串可以分解為多個可能的單詞組,例如wickedweather可以解析為“對我們不利”)。如果您只想要一組單詞,則需要確定選擇最佳單詞的規(guī)則,


#!/usr/bin/perl


use strict;


my $WORD_FILE = '/usr/share/dict/words'; #Change as needed

my %words; # Hash of words in dictionary


# Open dictionary, load words into hash

open(WORDS, $WORD_FILE) or die "Failed to open dictionary: $!\n";

while (<WORDS>) {

  chomp;

  $words{lc($_)} = 1;

}

close(WORDS);


# Read one line at a time from stdin, break into words

while (<>) {

  chomp;

  my @words;

  find_words(lc($_));

}


sub find_words {

  # Print every way $string can be parsed into whole words

  my $string = shift;

  my @words = @_;

  my $length = length $string;


  foreach my $i ( 1 .. $length ) {

    my $word = substr $string, 0, $i;

    my $remainder = substr $string, $i, $length - $i;

    # Some dictionaries contain each letter as a word

    next if ($i == 1 && ($word ne "a" && $word ne "i"));


    if (defined($words{$word})) {

      push @words, $word;

      if ($remainder eq "") {

        print join(' ', @words), "\n";

        return;

      } else {

        find_words($remainder, @words);

      }

      pop @words;

    }

  }


  return;

}


查看完整回答
反對 回復 2019-10-05
  • 3 回答
  • 0 關注
  • 643 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號