簡體   English   中英

刪除兩個雙精度空格之間的所有空格

[英]Remove all spaces between two double spaces

我有幾千個經過嚴重分析的文本文件,它們在長度的10%到30%之間顯示出一些有趣的行為。 不幸的是我沒有原始數據,所以我無法嘗試重新解析,但是幾乎每個文件都需要被(部分清除)。

輸入示例


text = 'The European  l a n g u a g es  ar e  members  of  the  same  fa m i l y 
. Their  sep a rate  e xi ste nce  is a myth .  F or  s c i e n c e , music, 
sport , etc, Europe uses the  s a m e  v oca bula ry. The languages  o n l y  d 
i f f e r  i n  t heir  grammar, their  pro nu n c iation  and their most common 
words. Everyone realizes why a new common language would be desirable: one could 
refuse to pay expensive translators.'


預期產量


'The European languages are members of the same family. Their separate existence 
 i s  a myth. For science, music, sport, etc, Europe uses the same vocabulary. The 
languages only differ in their grammar, their pronunciation and their most 
common words. Everyone realizes why a new common language would be desirable: 
one could refuse to pay expensive translators.'


從一種奇怪的格式到另一種格式似乎沒有太多規律性,也沒有明確的“原因”或觸發字詞或符號。 我注意到的一件事:奇怪格式的單詞由兩個空格分隔(有時在標點符號之前除外,但這是一個簡單的text.replace(' ,',',') )。

如何從字符串中刪除成對的雙空格之間的所有空格? 我假設有一個我沒想過的正則表達式...


更多信息

我不知道每個文檔中有多少個奇怪的部分/字母,我也不知道文檔的內容。 我唯一可以確定的其他事項是:

  • 最短的片段長度是1個字符(“成員”可能是“成員”),並且可能更長(例如“預期”中的字符)
  • 標點符號可能以單個空格開頭,但並非總是如此

我曾嘗試創建一個與re.sub()一起使用的正則表達式,但未到任何地方-沒有找到匹配項(最新嘗試是(?<= )[az]* (.* [az]*)(?= )但無效)或替代組。

謝謝!

如果沒有模式,請提出一些建議:

  1. 替換所有不是單個空格的空格。
  2. 然后對照字典檢查每個單詞。 myDictionary.exists(字)
  3. 奇數空格可能是文本格式的開始或結尾。 檢查空格字符的unicode。
  4. 嘗試再次獲得原件或與發送文本的作者聯系

在建議2中,檢查單詞是否為單詞。 如果不是,則添加下一個字符並再次檢查。 繼續這樣做,直到找到一個單詞。 並非所有單詞都適用,但是“ languag es”將變為“語言”,但“ la”和“ lan”除外。 因此,即使您發現一個單詞,也要繼續添加字符,直到它再次變成一個單詞,或者限制在16個字符左右。

用偽代碼:

替換所有空間多於一個空間
根據單個空格將字符串拆分為數組
遍歷每個單詞
檢查單詞是否存在英語
添加字符直到找到匹配項
移至下一個字
如果標點符號在字符的開頭或兩個空格之間,則用於標點符號,則刪除前一個空格字符。

如何使用Python檢查單詞是否為英語單詞?

我將分三個步驟(如果您遵循可選步驟則為五個):

  1. 第一個匹配的text.replace(' *','(@)') (星號前三個空格)。 將所有這些空間對(或兩個以上)轉換為某些令牌,可以確保不會像demo1所示在文本中出現(我以(@)為例)。 這是為了避免將兩個(或更多)空間序列視為單個空間的序列(如下所示,我們將刪除它們)
  2. 接下來是text.replace(' ','') 將所有單個空格轉換為空字符串,如demo2所示 這會在示例文本中加入許多用單個空格分隔的單詞,請小心。
  3. 最后, text.replace('\\(@\\)',' ') 將第一步中的所有標記轉換為單個空格,如demo3所示
  4. [可選] text.replace(' *([.!?]) *([AZ])','. $1') 如果還將所有點后都跟一個大寫字符轉換成一個點,再跟兩個空格以及匹配的大寫字符,那么您將獲得更漂亮的外觀。 就像在demo4中一樣
  5. [可選] text.match(' *([,;:]) *','$1 ')') 對其他標點符號執行相同操作,但只能使用一個空格。

您可以使用sed(1)來執行此操作,如下所示:

$ sed -e 's/   */#@#/g' \
      -e 's/ //g' \
      -e 's/#@#/ /g' \
      -e 's/ *\([.!?]\)  *\([A-Z]\)/\1  \2/g' \
      -e 's/ *\([,;:]\) */\1 /g' \
      <<EOF
The European  l a n g u a g es  ar e  members  of
the  same  fa m i l y . Their  sep a rate  e xi ste nce
is a myth .  F or  s c i e n c e , music, sport ,
etc, Europe uses the  s a m e  v oca bula ry. The
languages  o n l y  d i f f e r  i n  t heir
grammar, their  pro nu n c iation  and their most
common words. Everyone realizes why a new common
language would be desirable: one could 
refuse to pay expensive translators.
EOF
TheEuropean languages are members of
the same family.  Their separate existence
isamyth. For science, music, sport,
etc, Europeusesthe same vocabulary.  The
languages only differ in their
grammar, their pronunciation andtheirmost
commonwords. Everyonerealizeswhyanewcommon
languagewouldbedesirable: onecould
refusetopayexpensivetranslators.
$ _

最后一個示例還將[,;:]轉換為它們加上一個空格,並且句子分隔是否也用於? ! 分數。

如何從字符串中刪除成對的雙空格之間的所有空格?

不要考慮兩個之間的n個空格...這與兩個或多個相同,只是text.replace(' *',' ')*之前的三個空格),或將兩個或多個空格組成的字符串替換為一串只有兩個 使用text.replace(' +',' ')'+之前的兩個空格text.replace(' +',' ')'可以實現相同的效果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM