【问题标题】:Python - Group Sequential Array MembersPython - 分组顺序数组成员
【发布时间】:2016-04-18 07:50:01
【问题描述】:

我想像这样编辑我的文本:

arr = [] 
# arr is full of tokenized words from my text

例如:

"Abraham Lincoln Hotel is very beautiful place and i want to go there with
 Barbara Palvin. Also there are stores like Adidas ,Nike , Reebok."

编辑:基本上我想检测专有名称并通过在 for 语句中使用 istitle() 和 isAlpha() 对它们进行分组,例如:

for i in arr:
    if arr[i].istitle() and arr[i].isAlpha

在示例中,arr 一直附加到下一个单词的第一个字母不是大写。

arr[0] + arr[1] + arr[2] = arr[0]
#Abraham Lincoln Hotel

这就是我想要的新 arr:

['Abraham Lincoln Hotel'] is very beautiful place and i want to go there with['Barbara Palvin']. ['Also'] there are stores like ['Adidas'], ['Nike'],['Reebok'].

“也”对我来说不是问题,当我尝试匹配我的数据集时它会很有用。

【问题讨论】:

  • 我想要一个基本的 python 代码,它总是返回正确的名称而不将它们分组,但无论如何谢谢。
  • 你不能做一个基本的python代码来返回正确的名字。这并不容易,您需要使用NTLK 来归档它。
  • 使用istitle() 的问题在于,Also 的世界也是大写的。
  • 我将检查我的数据集的正确名称。这就是为什么我不想使用 nltk。我创建了自己的语言处理程序。

标签: python nlp nltk stanford-nlp opennlp


【解决方案1】:

你可以这样做:

sentence = "Abraham Lincoln Hotel is very beautiful place and i want to go there with Barbara Palvin. Also there are stores like Adidas, Nike, Reebok."
all_words = sentence.split()
last_word_index = -100
proper_nouns = []
for idx, word in enumerate(all_words):
    if(word.istitle() and word.isalpha()):
        if(last_word_index == idx-1):
            proper_nouns[-1] = proper_nouns[-1] + " " + word
        else:
            proper_nouns.append(word)
        last_word_index = idx
print(proper_nouns)

此代码将:

  • 将所有单词拆分成一个列表
  • 遍历所有单词和
    • 如果最后一个大写单词是前一个单词,它将附加到列表中的最后一个条目
    • 否则它会将单词作为新条目存储在列表中
    • 记录找到大写单词的最后一个索引

【讨论】:

  • 这输出['Abraham Lincoln Hotel', 'Barbara', 'Also'],而不是['Abraham', 'Lincoln', 'Hotel', 'Barbara', 'Palvin.', 'Adidas', 'Nike', 'Reebok.']
  • 像“Also”或“Because”这样的词对我来说不会有问题,因为它们不会与我以后充满组织、位置和人名的数据集匹配。所以任何解决方案,如; ['Abraham Lincoln Hotel'] , ['Barbara Palvin'] ,['Adidas'], ['Nike'], ['Reebok'] 会很有用。因为稍后我会将它们分组的单词作为输入发送给我的函数。
  • 您编写的代码符合我的要求,但仅适用于第一个字母。输出为:['Abraham Lincoln Hotel', 'Barbara', 'Also']
【解决方案2】:

这是你要问的吗?

sentence = "Abraham Lincoln Hotel is very beautiful place and i want to go there with Barbara Palvin. Also there are stores like Adidas ,Nike , Reebok."

chars = ".!?,"                                   # Characters you want to remove from the words in the array

table = chars.maketrans(chars, " " * len(chars)) # Create a table for replacing characters
sentence = sentence.translate(table)             # Replace characters with spaces

arr = sentence.split()                           # Split the string into an array whereever a space occurs

print(arr)

输出是:

['Abraham',
 'Lincoln',
 'Hotel',
 'is',
 'very',
 'beautiful',
 'place',
 'and',
 'i',
 'want',
 'to',
 'go',
 'there',
 'with',
 'Barbara',
 'Palvin',
 'Also',
 'there',
 'are',
 'stores',
 'like',
 'Adidas',
 'Nike',
 'Reebok']

关于此代码的注意事项:chars 变量中的任何字符都将从数组中的字符串中删除。解释在代码中。

要删除非名称,只需执行以下操作:

import string
new_arr = []

for i in arr:
    if i[0] in string.ascii_uppercase:
        new_arr.append(i)

此代码将包含所有以大写字母开头的单词。

要解决此问题,您需要将 chars 更改为:

chars = ","

并将上面的代码改为:

import string
new_arr = []
end = ".!?"    

b = 1
for i in arr:
    if i[0] in string.ascii_uppercase and arr[b-1][-1] not in end:
        new_arr.append(i)
    b += 1

这将输出:

['Abraham', 
'Lincoln', 
'Hotel', 
'Barbara', 
'Palvin.', 
'Adidas', 
'Nike',
'Reebok.']

【讨论】:

  • 这不是正确的方法。我的意思是,OP 不可能列出所有不是专有名称的单词。
  • 已编辑。 @ArdaNalbant 您应该找到更多符合或不符合您需要识别的名称的标准,以便程序更精确。
  • 输出是我需要的让我试试。干得好
  • 我声明了 utf-8,它会影响输出吗?编辑:不,它没有
  • 没有。它不会。 (至少在 Python 3 上不是)
猜你喜欢
  • 2021-07-29
  • 2016-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-17
  • 2022-01-04
  • 1970-01-01
相关资源
最近更新 更多