Python快速提取英文单词的方法总结

:soogor 2023-10-28 16:01:57 :52

我们在日常的数据处理当中,有时需要提取文中的单词,在这里我们总结了几个提取英文单词的方法。

方法一:

for迭代法

text = '''CHAPTER I   A SERMON ON INNS
The sea was a pale elfin green and the afternoon had already felt the fairy touch of evening as a young woman with dark hair, dressed in a crinkly copper-coloured sort of dress of the artistic order, was walking rather listlessly along the parade of Pebblewick-on-Sea, trailing a parasol and looking out upon the sea’s horizon. She had a reason for looking instinctively out at the sea-line; a reason that many young women have had in the history of the world. But there was no sail in sight.
On the beach below the parade were a succession of small crowds, surrounding the usual orators of the seaside; whether niggers or socialists, whether clowns or clergymen. Here would stand a man doing something or other with paper boxes; and the holiday makers would watch him for hours in the hope of some time knowing what it was that he was doing with them. Next to him would be a man in a top hat with a very big Bible and a very small wife, who stood silently beside him, while he fought with his clenched fist against the heresy of Milnian Sublapsarianism so wide-spread in fashionable watering-places. It was not easy to follow him, he was so very much excited; but every now and then the words “our Sublapsarian friends” would recur with a kind of wailing sneer. Next was a young man talking of nobody knew what (least of all himself), but apparently relying for public favour mainly on having a ring of carrots round his hat. He had more money lying in front of him than the others. Next were niggers. Next was a children’s service conducted by a man with a long neck who beat time with a little wooden spade. Farther along there was an atheist, in a towering rage, who pointed every now and then at the children’s service and spoke of Nature’s fairest things being corrupted with the secrets of the Spanish Inquisition—by the man with the little spade, of course. The atheist (who wore a red rosette) was very withering to his own audience as well. “Hypocrites!” he would say; and then they would throw him money. “Dupes and dastards!” and then they would throw him more money. But between the atheist and the children’s service was a little owlish man in a red fez, weakly waving a green gamp umbrella. His face was brown and wrinkled like a walnut, his nose was of the sort we associate with Judæa, his beard was the sort of black wedge we associate rather with Persia. The young woman had never seen him before; he was a new exhibit in the now familiar museum of cranks and quacks. The young woman was one of those people in whom a real sense of humour is always at issue with a certain temperamental tendency to boredom or melancholia; and she lingered a moment, and leaned on the rail to listen.
It was fully four minutes before she could understand a word the man was saying; he spoke English with so extraordinary an accent that she supposed at first that he was talking in his own oriental tongue. All the noises of that articulation were odd; the most marked was an extreme prolongation of the short “u” into “oo”; as in “poo-oot” for “put.” Gradually the girl got used to the dialect, and began to understand the words; though some time elapsed even then before she could form any conjecture of their subject matter. Eventually it appeared to her that he had some fad about English civilisation having been founded by the Turks; or, perhaps by the Saracens after their victory in the Crusades. He also seemed to think that Englishmen would soon return to this way of thinking; and seemed to be urging the spread of teetotalism as an evidence of it. The girl was the only person listening to him.
“Loo-ook,” he said, wagging a curled brown finger, “loo-ook at your own inns” (which he pronounced as “ince”). “Your inns of which you write in your boo-ooks! Those inns were not poo-oot up in the beginning to sell ze alcoholic Christian drink. They were put up to sell ze non-alcoholic Islamic drinks. You can see this in the names of your inns. They are eastern names, Asiatic names. You have a famous public house to which your omnibuses go on the pilgrimage. It is called the Elephant and Castle. That is not an English name. It is an Asiatic name. You will say there are castles in England, and I will agree with you. There is the Windsor Castle. But where,” he cried sternly, shaking his green umbrella at the girl in an angry oratorical triumph, “where is the Windsor Elephant? I have searched all Windsor Park. No elephants.”
The girl with the dark hair smiled, and began to think that this man was better than any of the others. In accordance with the strange system of concurrent religious endowment which prevails at watering-places, she dropped a two shilling piece into the round copper tray beside him. With honourable and disinterested eagerness, the old gentleman in the red fez took no notice of this, but went on warmly, if obscurely, with his argument.
“Then you have a place of drink in this town which you call The Bool!'''
sentence = text.replace('\n', '')
sentence = re.sub('[\u4e00-\u9fa5]', '', sentence)
sentence = re.sub('。', '', sentence)
sentence = re.sub(',', '', sentence)
sentence = re.sub('《', '', sentence)
sentence = re.sub('》', '', sentence)
sentence = re.sub('“', '', sentence)
sentence = re.sub('”', '', sentence)
sentence = re.sub('、', '', sentence)
sentence = re.sub('(', '', sentence)
sentence = re.sub(')', '', sentence)
sentence = re.sub('\(', '', sentence)
sentence = re.sub('\)', '', sentence)
sentence = re.sub('’', "'", sentence)
for i in sentence:
    if i == ' ' or i == ',' or i == '.' or i == '、' or i == '。' or i == ',' or i == '《' or i == '》' or i == '\(' or i == '\)' or i == '”' or i == '“' or i == '?' or i == '!' or i == '?' or i == '!':
        if x != 0:  #第二格单词前面存在空格需往后移一格,剔除空格
            q = sentence[x + 1:y:1]
            x = y  #记录位置,从x处继续提取
            print(q)
            result.append(q)
        elif x == 0:  #第一个单词前没有空格
            q = sentence[x:y:1]
            x = y
            print(q)
            result.append(q)
    if y == len(sentence) - 1:  #用于提取最后一个单词
        q = sentence[x + 1:y + 1:1]
        print(q)
        result.append(q)
    y = y + 1

for i in result:
    if i == '':
        result.remove('')

print(set(result))

方法二:

split()方法

import re

text = '''CHAPTER I   A SERMON ON INNS
The sea was a pale elfin green and the afternoon had already felt the fairy touch of evening as a young woman with dark hair, dressed in a crinkly copper-coloured sort of dress of the artistic order, was walking rather listlessly along the parade of Pebblewick-on-Sea, trailing a parasol and looking out upon the sea’s horizon. She had a reason for looking instinctively out at the sea-line; a reason that many young women have had in the history of the world. But there was no sail in sight.
On the beach below the parade were a succession of small crowds, surrounding the usual orators of the seaside; whether niggers or socialists, whether clowns or clergymen. Here would stand a man doing something or other with paper boxes; and the holiday makers would watch him for hours in the hope of some time knowing what it was that he was doing with them. Next to him would be a man in a top hat with a very big Bible and a very small wife, who stood silently beside him, while he fought with his clenched fist against the heresy of Milnian Sublapsarianism so wide-spread in fashionable watering-places. It was not easy to follow him, he was so very much excited; but every now and then the words “our Sublapsarian friends” would recur with a kind of wailing sneer. Next was a young man talking of nobody knew what (least of all himself), but apparently relying for public favour mainly on having a ring of carrots round his hat. He had more money lying in front of him than the others. Next were niggers. Next was a children’s service conducted by a man with a long neck who beat time with a little wooden spade. Farther along there was an atheist, in a towering rage, who pointed every now and then at the children’s service and spoke of Nature’s fairest things being corrupted with the secrets of the Spanish Inquisition—by the man with the little spade, of course. The atheist (who wore a red rosette) was very withering to his own audience as well. “Hypocrites!” he would say; and then they would throw him money. “Dupes and dastards!” and then they would throw him more money. But between the atheist and the children’s service was a little owlish man in a red fez, weakly waving a green gamp umbrella. His face was brown and wrinkled like a walnut, his nose was of the sort we associate with Judæa, his beard was the sort of black wedge we associate rather with Persia. The young woman had never seen him before; he was a new exhibit in the now familiar museum of cranks and quacks. The young woman was one of those people in whom a real sense of humour is always at issue with a certain temperamental tendency to boredom or melancholia; and she lingered a moment, and leaned on the rail to listen.
It was fully four minutes before she could understand a word the man was saying; he spoke English with so extraordinary an accent that she supposed at first that he was talking in his own oriental tongue. All the noises of that articulation were odd; the most marked was an extreme prolongation of the short “u” into “oo”; as in “poo-oot” for “put.” Gradually the girl got used to the dialect, and began to understand the words; though some time elapsed even then before she could form any conjecture of their subject matter. Eventually it appeared to her that he had some fad about English civilisation having been founded by the Turks; or, perhaps by the Saracens after their victory in the Crusades. He also seemed to think that Englishmen would soon return to this way of thinking; and seemed to be urging the spread of teetotalism as an evidence of it. The girl was the only person listening to him.
“Loo-ook,” he said, wagging a curled brown finger, “loo-ook at your own inns” (which he pronounced as “ince”). “Your inns of which you write in your boo-ooks! Those inns were not poo-oot up in the beginning to sell ze alcoholic Christian drink. They were put up to sell ze non-alcoholic Islamic drinks. You can see this in the names of your inns. They are eastern names, Asiatic names. You have a famous public house to which your omnibuses go on the pilgrimage. It is called the Elephant and Castle. That is not an English name. It is an Asiatic name. You will say there are castles in England, and I will agree with you. There is the Windsor Castle. But where,” he cried sternly, shaking his green umbrella at the girl in an angry oratorical triumph, “where is the Windsor Elephant? I have searched all Windsor Park. No elephants.”
The girl with the dark hair smiled, and began to think that this man was better than any of the others. In accordance with the strange system of concurrent religious endowment which prevails at watering-places, she dropped a two shilling piece into the round copper tray beside him. With honourable and disinterested eagerness, the old gentleman in the red fez took no notice of this, but went on warmly, if obscurely, with his argument.
“Then you have a place of drink in this town which you call The Bool!'''
print(text.split())
word_list = text.split()
word_list = list(set(word_list))
word_list2 = []
for item in word_list:
    item = re.sub(',|!|\)|\(', '', item)
    word_list2.append(item)
print(word_list2)

大致方法如此,细节之处大家可以根据需要自行进行修改

以上就是soogor软件总结的“python快速提取英文单词的方法总结”的具体内容

本文编辑:admin
Copyright © 2022 All Rights Reserved 威海上格软件有限公司 版权所有

鲁ICP备20007704号

Thanks for visiting my site.