2016-06-09 47 views
-1

您好我有一個腳本能夠刪除子標題和段落,但我無法刪除非英文子標題和文字的段落。刪除非英文小標題和段落

例如,(原文)

=== Personal finance === 
Protection against unforeseen personal events, as well as events in the wider economies 
Transference of family wealth across generations (bequests and inheritance) 

=== Corporate finance === 
Corporate finance deals with the sources of funding and the capital structure of corporations and the actions that managers take to increase the value of the firm to the shareholders. 

== External links == 
Business acronyms and abbreviations 
Business acronyms 

== Kūrybinės Industrijos == 
Kūrybinės industrijos apima sritį ekonominių veiksnių, susitelkusių ties žinių ir informacijos generavimu arba tyrimu. 

(結果)我從我的代碼得到的是:

Protection against unforeseen personal events, as well as events in the wider economies 
Transference of family wealth across generations (bequests and inheritance) 

Corporate finance deals with the sources of funding and the capital structure of corporations and the actions that managers take to increase the value of the firm to the shareholders. 

Kūrybinės industrijos apima sritį ekonominių veiksnių, susitelkusių ties žinių ir informacijos generavimu arba tyrimu. 

這是我希望達到(期望的結果)

Protection against unforeseen personal events, as well as events in the wider economies 
Transference of family wealth across generations (bequests and inheritance) 

Corporate finance deals with the sources of funding and the capital structure of corporations and the actions that managers take to increase the value of the firm to the shareholders. 

的腳本如下:

import re 
from subprocess import call 

f1 = open('asd.text', 'r') # read file that contains the orginal text 
f2 = open('NoRef.text', 'w') # write to new file 

section_title_re = re.compile("^=+\s+.*\s+=+$") 

content = [] 
skip = False 
for l in f1.read().splitlines(): 
    line = l.strip() 

    if "== external links ==" in line.lower(): 
     skip = True 
     continue 

    if section_title_re.match(line): 
     skip = False 
     continue 
    if skip: 
     continue 
    content.append(line) 

content = '\n'.join(content) + '\n' 
f2.write(content+"\n") 
f2.close() 

問題: 到目前爲止,我的代碼是能夠用已知的名字,如「外部鏈接」的副標題刪除的段落。

但是,我刪除那些非英文的子標題和段落嗎?

謝謝。

+4

你試過Google檢測語言的圖書館嗎?粗略的搜索提出了這個:https://pypi.python.org/pypi/langdetect? –

+0

如果您事先知道所有可能遇到的(英文)標題,只需檢查標題是否在您的列表中(實際上最好使用'set'),如果不是,則跳過整個段落。 – Julien

+0

嗨Julien我不知道所有可能的英文標題,因此存在我的問題在哪裏。 – windboy

回答

1

如果您只想檢測一個字符串中包含非英文字符,那很簡單:只是嘗試將其作爲ascii解碼:如果失敗了,它包含字符以上127代碼:

try: 
    utxt = txt.decode('ascii') 
except: 
    # txt contains non "english" characters 
    ... 

如果您想要檢測它是否包含非英語單詞,這是一個更復雜的問題,你應該想知道你是否想接受寫得很差的英文單詞,如englich woerds badli writed。祝你好運,如果你想要這樣...

+1

英語拼字法允許在藉詞中使用變音符號,如*zöology*和*résumé*,所以這是最好的近似方法。在像維基百科這樣的資源中,即使初學者很可能在沒有變音符號的情況下最初輸入這些詞語,但這些詞語有可能被精心編輯的問題糾正爲適當的形式。 – tripleee

+0

我會試一試。非常感謝你。 – windboy

+0

由@ juanpa.arrivillaga提出的包['langdetect'](https://pypi.python.org/pypi/langdetect)對這些詞表現得非常好:'detect_langs(「englich woerds badli writed」)'returns'[[ EN:0.999994655212]'。所以作爲一個啓發式的工具,似乎做得很好:)(除了用貸款詞表達正確外)。 – MariusSiuram