簡體   English   中英

使用 python 將段落中的每個新行上的句子分開

[英]Separate sentences on each new line from paragraph using python

我有一段為:

輸入 : -

“然而,通常缺乏足夠可靠的地方、區域和國家土地利用和土地覆蓋數據以及時間和地理細節來提供對景觀變化的准確估計。美國地質調查局的 EROS 數據中心和景觀生態學分部美國環境保護署正在合作開展一項為期四年的研究項目,以記錄過去 30 年來美國本土土地覆蓋變化的類型、分布、速率、驅動因素和后果。該項目使用生態區框架作為地理分層器。”

想在新行上分隔每個句子。 我正在解析“。” 替換每個“.” 作為“\\n”(換行符)。 它適用於普通句子,但是當“美國地質...”這樣的事情出現時,我的腳本會生成兩個我不想要的單獨句子。 請提出任何可能的建議

預期輸出:--(連續編號的 3 個句子)

1) 然而,通常缺乏足夠可靠的地方、區域和國家土地利用和土地覆蓋數據以及時間和地理細節來提供對景觀變化的准確估計。

2) 美國地質調查局的 EROS 數據中心和美國環境保護署的景觀生態學分部正在合作進行一項為期四年的研究項目,以記錄美國本土土地覆蓋變化的類型、分布、速率、驅動因素和后果。過去 30 年的國家。

3) 該項目使用生態區框架作為地理分層器。

目前得到:(7 句話)

1) 然而,通常缺乏足夠可靠的地方、區域和國家土地利用和土地覆蓋數據以及時間和地理細節來提供對景觀變化的准確估計。

2)美國。

3)S。

4)地質調查局EROS數據中心和美國景觀生態分會。

5)S。

6) 環境保護署正在合作開展一項為期四年的研究項目,以記錄過去 30 年美國本土土地覆蓋變化的類型、分布、比率、驅動因素和后果。

7) 該項目使用生態區框架作為地理分層器。

使用nltk絕對是一個好方法。 這些句子可以列舉如下:

import nltk

text = "However, there is generally a lack of local, regional, and national land use and land cover data of sufficient reliability and temporal and geographic detail for providing accurate estimates of landscape change. The U.S. Geological Survey's EROS Data Center and the Landscape Ecology Branch of the U.S. Environmental Protection Agency are collaborating on a four-year research project to document the types, distributions, rates, drivers, and consequences of land cover change for the conterminous United States over the past 30 years. The project is using an ecoregion framework as a geographic stratifier."                
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')

for index, sentence in enumerate(tokenizer.tokenize(text), start=1):
    print "{}) {}\n".format(index, sentence)

這將顯示以下輸出:

1) However, there is generally a lack of local, regional, and national land use and land cover data of sufficient reliability and temporal and geographic detail for providing accurate estimates of landscape change.

2) The U.S. Geological Survey's EROS Data Center and the Landscape Ecology Branch of the U.S. Environmental Protection Agency are collaborating on a four-year research project to document the types, distributions, rates, drivers, and consequences of land cover change for the conterminous United States over the past 30 years.

3) The project is using an ecoregion framework as a geographic stratifier.

暫無
暫無

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

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