首页 > 解决方案 > 使用 python 或 javascript 从文本中提取难懂的英语单词以进行词汇构建

问题描述

我想从在线英语文本中提取难词,例如从古腾堡使用 python 或 javascript 构建词汇。我不会得到简单的单词,而是独特的词汇,比如 regal , aocryphal ..etc。

如何确保在拆分文本时只得到唯一的词汇而不是简单的单词。

标签: javascriptpythontext

解决方案


我将“非常用词”定义为没有出现在前 10000 个最常用英语单词中的词。

10 k 最常见的词是任意边界,但正如github repo 中所述

根据牛津英语语料库的分析,最常见的 7000 个英语词元约占使用量的 90%,因此一个 10000 个单词的训练语料库对于实际的训练应用来说绰绰有余。

import requests

english_most_common_10k = 'https://raw.githubusercontent.com/first20hours/google-10000-english/master/google-10000-english-usa-no-swears.txt'

# Get the file of 10 k most common words from TXT file in a github repo
response = requests.get(english_most_common_10k)
data = response.text

set_of_common_words = {x for x in data.split('\n')}

# Once we have the set of common words, we can just check.
# The check is in average case O(1) operation,
# but you can use for example some sort of search three with O(log(n)) complexity
while True:
    word = input()
    if word in set_of_common_words:
        print(f'The word "{word}" is common')
    else:
        print(f'The word "{word}" is difficult')

推荐阅读