【问题标题】:i have two paragraph in txt. i have to find common word from both paragraph using python nltk我在txt中有两段。我必须使用 python nltk 从两个段落中找到常用词
【发布时间】:2017-08-04 03:50:24
【问题描述】:

第 1 段

电子商务,通常写作电子商务,是使用计算机网络(例如互联网或在线社交网络)进行商品或服务交易的交易或便利化。电子商务利用移动商务、电子资金转账、供应链管理、互联网营销、在线交易处理、电子数据交换 (EDI)、库存管理系统和自动数据收集系统等技术。

第 2 段

现代电子商务通常在交易生命周期的至少一部分中使用万维网,尽管它也可能使用其他技术,例如电子邮件。电子商务的优势包括访问速度、商品和服务的选择范围更广、可访问性和国际影响力。

我必须找到两个段落之间的共同词并打印出来

【问题讨论】:

  • 这听起来像是一个家庭作业问题,所以我不会提供答案,而是给你一个提示。 NLTK 自己无法做到这一点——这不是 NLTk 的用途。但是,您最有可能需要做的是使用 NLTK 的分词器将段落拆分为单词,然后将这些单词放入集合并进行比较(例如,通过建议答案中的建议)。
  • 第 1 步。nltk.word_tokenize,第 2 步:参见stackoverflow.com/questions/15173225/… 或尝试web.stanford.edu/class/linguist236/materials/… 中的任何方法
  • @BhatiManishKumar,更改您的用户 ID 以隐藏您的姓名也会敲响警钟。提出家庭作业问题并不违反本网站的规则,只要是好问题。你已经得到了足够的提示,现在去学习一些 Python。

标签: python machine-learning nlp artificial-intelligence nltk


【解决方案1】:

如果您不需要在语言处理方面做一些特殊的事情,则不需要 NLTK:

paragraph1 = paragraph1.lower().split()
paragraph2 = paragraph2.lower().split()

intersection = set(words1) & set(words2)

【讨论】:

  • @BhatiManishKumar 当你开始抛出不合理的约束来敲响警钟的那一刻——这很可能是家庭作业或项目,否则没有理由安装一个 50MB 的库来做一些你可以用简单的集合交集。你不可能在这里找到帮助。
  • @BhatiManishKumar,更改您的用户 ID 以隐藏您的姓名也会敲响警钟。提出家庭作业问题并不违反本网站的规则,只要是好问题。你已经得到了足够的提示,现在去学习一些 Python。
【解决方案2】:

您可以使用set.intersection

p1 = '''
Electronic commerce, commonly written as E-Commerce, is the trading or  
facilitation of trading in goods or services using computer networks, such 
as the Internet or online social networks. Electronic commerce draws on 
technologies such as mobile commerce, electronic funds transfer, supply 
chain management, Internet marketing, online transaction processing, 
electronic data interchange (EDI), inventory management systems, and 
automated data collection systems.
'''.split()

p2 = '''
Modern electronic commerce typically uses the World Wide Web for at least 
one part of the transaction's life cycle although it may also use other 
technologies such as e-mail. The benefits of e-commerce include it’s the 
speed of access, a wider selection of goods and services, accessibility, and 
international reach.
'''.split()

print(set(p1).intersection(p2))
{'and', 'the', 'technologies', 'of', 'electronic', 'such', 'commerce', 'as', 'goods'}

【讨论】:

  • 谢谢你的回复,但我想要这个在 nltk。但也谢谢你
  • @BhatiManishKumar 我不知道有任何 NLTK 方法可以达到相同的结果。如果您检查source,他们也经常使用 set.intersection() 来获得类似的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-18
  • 1970-01-01
  • 2017-08-28
  • 2019-05-05
相关资源
最近更新 更多