Улучшить обнаружение таких слов, как «она» и «ее» из предложений, и в результате вернуть «женский» - PullRequest
0 голосов
/ 02 октября 2018

У меня есть переменная «bio_sentences», и, как следует из названия переменной, она содержит от четырех до пяти биологических предложений отдельных лиц (выделенных и разделенных на предложения из переменной «био»).Я пытаюсь определить, какого пола человек использует эту логику ...

Femalew <- c("She", "Her")
Check <- str_extract_all(bio,Femalew)
Check <- Check[Check != "character(0)"]
Gender <- vector("character")
if(length(Check) > 0){
  Gender[1] <- "Female"
}else{
  Gender[1] <- "Male"
}
for(i in 1:length(bio_sentences)){
  Gender[i] <- Gender[1]
} 

Я получаю хороший результат (большинство в моем наборе данных - мужчины), однако есть несколько промахов (некоторые женщины не 'не обнаружено), несмотря на то, что в предложениях есть «она» или «ее».Есть ли в любом случае, я могу улучшить точность логики или развернуть некоторые новые функции, такие как grepl?

РЕДАКТИРОВАТЬ:

    data1.Gender    A B C D E   data1.Description
1   Female  0   0   0   0   0   Ranjit Singh President of Boparan Holdings Limited Ranjit is President of Boparan Holdings Limited.
2   Female  0   0   0   NA  NA  He founded the business in 1993 and has more than 25 years’ experience in the food industry.
3   Female  0   0   0   NA  NA  Ranjit is particularly skilled at growing businesses, both organically and through acquisition.
4   Female  0   0   0   NA  NA  Notable acquisitions include Northern Foods and Brookes Avana in 2011.
5   Female  0   0   0   NA  NA  Ranjit and his wife Baljinder Boparan are the sole shareholders of Boparan Holdings, the holding company for 2 Sisters Food Group.
6   Female  0   0   0   NA  NA  s

Выше человек из данных, мое требование заключается в том, чтокод читает все строки в «data1.description» (в моем коде это цикл for, поэтому он читает все предложения для каждого человека), и, как вы можете видеть, человек - мужчина, и в нем явно присутствует «он».однако в одном из предложений я получаю его как «женский», применяя вышеупомянутую логику, которую я написал ранее.

Ответы [ 3 ]

0 голосов
/ 02 октября 2018

Как сказал @Merijn van Tilborg, вы должны очень четко помнить свои предложения, потому что, если существует более одного местоимения, ваша работа не сможет дать желаемых результатов.
Однако вы также можете управлять этими случаями.мы можем попробовать с пакетами dplyr и tidytext, но нам нужно немного очистить данные:

# explicit the genders
female <- c("She", "Her")
male <- c("He", "His")

# here your data, with several examples of cases
df <- data.frame(
line = c(1,2,3,4,5,6),
text = c("She is happy",            # female
         "Her dog is happy",        # female (if we look at the subject, it's not female..)
         "He is happy",             # male
         "His dog is happy",        # male
         "It is happy",             # ?
         "She and he are happy"),   # both!
         stringsAsFactors = FALSE ) # life saver

Теперь мы можем попробовать что-то вроде этого:

library(tidytext)
library(dplyr)

  df %>%
  unnest_tokens(word, text) %>%                                            # put words in rows
  mutate(gender = ifelse(word %in% tolower(female),'female',
                  ifelse(word %in% tolower(male), 'male','unknown'))) %>%  # detect male and female, remember tolower!
  filter(gender!='unknown') %>%                                            # remove the unknown
  right_join(df) %>%                                                       # join with the original sentences keeping all of them
  select(-word)                                                            # remove useless column

  line gender                 text
1    1 female         She is happy
2    2 female     Her dog is happy
3    3   male          He is happy
4    4   male     His dog is happy
5    5   <NA>          It is happy
6    6 female She and he are happy
7    6   male She and he are happy

И вы можете видеть, что 1,2,3,4 предложения соответствуют вашему стандарту, «оно» не определено, и если есть мужчины и женщины, мы удваиваем ряд, и вы понимаете, почему.

Наконец, вы можете свернуть в одну строку, добавив в цепочку dplyr это:

%>% group_by(text, line) %>% summarise(gender = paste(gender, collapse = ','))

# A tibble: 6 x 3
# Groups:   text [?]
  text                  line gender     
  <chr>                <dbl> <chr>      
1 He is happy              3 male       
2 Her dog is happy         2 female     
3 His dog is happy         4 male       
4 It is happy              5 NA         
5 She and he are happy     6 female,male
6 She is happy             1 female    

РЕДАКТИРОВАТЬ : Давайте попробуем с вашими данными:

data1 <- read.table(text="
    data1.Gender    A B C D E   data1.Description
1   Female  0   0   0   0   0   'Ranjit Singh President of Boparan Holdings Limited Ranjit is President of Boparan Holdings Limited.'
2   Female  0   0   0   NA  NA  'He founded the business in 1993 and has more than 25 years’ experience in the food industry.'
3   Female  0   0   0   NA  NA  'Ranjit is particularly skilled at growing businesses, both organically and through acquisition.'
4   Female  0   0   0   NA  NA  'Notable acquisitions include Northern Foods and Brookes Avana in 2011.'
5   Female  0   0   0   NA  NA  'Ranjit and his wife Baljinder Boparan are the sole shareholders of Boparan Holdings, the holding company for 2 Sisters Food Group.'
6   Female  0   0   0   NA  NA  's'",stringsAsFactors = FALSE)


# explicit the genders, in this case I've put also the names
female <- c("She", "Her","Baljinder")
male <- c("He", "His","Ranjit")

# clean the data
df <- data.frame(
line = rownames(data1),
text = data1$data1.Description,
stringsAsFactors = FALSE)

library(tidytext)
library(dplyr)

  df %>%
  unnest_tokens(word, text) %>%                                            # put words in rows
  mutate(gender = ifelse(word %in% tolower(female),'female',
                  ifelse(word %in% tolower(male), 'male','unknown'))) %>%  # detect male and female, remember tolower!
  filter(gender!='unknown') %>%                                            # remove the unknown
  right_join(df) %>%                                                       # join with the original sentences keeping all of them
  select(-word) %>% 
  group_by(text, line) %>%
  summarise(gender = paste(gender, collapse = ',')) 

В результате:

Joining, by = "line"
# A tibble: 6 x 3
# Groups:   text [?]
  text                                                            line  gender       
  <chr>                                                           <chr> <chr>        
1 He founded the business in 1993 and has more than 25 years’ ex~ 2     male         
2 Notable acquisitions include Northern Foods and Brookes Avana ~ 4     NA           
3 Ranjit and his wife Baljinder Boparan are the sole shareholder~ 5     male,male,fe~
4 Ranjit is particularly skilled at growing businesses, both org~ 3     male         
5 Ranjit Singh President of Boparan Holdings Limited Ranjit is P~ 1     male,male    
6 s                                                               6     NA  

Настоящая игра состоит в том, чтобы определить все слова, которые вы можете считать «мужскими» или «женскими».

0 голосов
/ 03 октября 2018

В дополнение к уже полученному ответу я также настоятельно рекомендую добавить в этот список наиболее распространенные женские имена.Например, их можно легко найти в Интернете в качестве 100 самых популярных женских имен в стране.Я уверен, что даже если вы добавите около 500 наиболее часто встречающихся имен в этот женский список, вы получите вполне приличное начало и сделаете то же самое для мужчин.

Кроме того, я приведу вам пример с небольшим количеством правил принятия решений,Насколько вероятно, что это будет женщина или мужчина?Одним из подходов может быть просто подсчет вхождений и вычисление соотношения.На основании соотношения вы можете принимать собственные решения.Мой выбор - просто произвольный пример и обозначается одной строкой для каждого решения (может быть закодировано гораздо более эффективно).

library(data.table) ## just my personal preference above dplyr
library(stringr) ## just my personal favorite when I deal with strings

df = data.table(text = c("Because Sandra is a female name and we talk a few times about her, she is most likely a female he says.",
       "Sandra is mentioned and the only references are about how she did everything to achieve her goals.", 
       "Nothing is mentioned that reveals a gender.",
       "She talks about him and he talks about her.",
       "Sandra says: he is nice and she is nice too.",
       "Adam is a male and we only talk about him")))

f.indicators = c("she", "her", "susan", "sandra")
m.indicators = c("he", "him", "his", "steve", "adam")

df[, f.count := sum(str_split(str_to_lower(text), "[[:space:]]|[[:punct:]]")[[1]] %in% f.indicators, na.rm = TRUE), by = text]
df[, m.count := sum(str_split(str_to_lower(text), "[[:space:]]|[[:punct:]]")[[1]] %in% m.indicators, na.rm = TRUE), by = text]
df[f.count != 0 | m.count != 0, gender_ratio_female := f.count / (f.count + m.count)]
df[, decision := "Unknown"]
df[gender_ratio_female == 1, decision := "Female, no male indications"]
df[gender_ratio_female == 0, decision := "Male, no female indicators"]
df[gender_ratio_female > 0.4 & gender_ratio_female < 0.6, decision := "Gender should be checked"]
df[gender_ratio_female > 0.6 & gender_ratio_female < 1, decision := "Probably a Female"]
df[gender_ratio_female > 0 & gender_ratio_female < 0.4, decision := "Probably a Male"]

PS Извините, я изо всех сил пытаюсь отформатировать выходную таблицу здесь, я новичок здесь

                                                                       text f.count m.count   gender_ratio_female                    decision
1: Because Sandra is a female name and we talk a few times about her, she is most likely a female he says.       3       1              0.7500           Probably a Female
2:      Sandra is mentioned and the only references are about how she did everything to achieve her goals.       3       0              1.0000 Female, no male indications
3:                                                             Nothing is mentioned that reveals a gender.       0       0                  NA                     Unknown
4:                                                             She talks about him and he talks about her.       2       2              0.5000    Gender should be checked
5:                                                            Sandra says: he is nice and she is nice too.       2       1              0.6667           Probably a Female
6:                                                               Adam is a male and we only talk about him       0       2              0.0000  Male, no female indicators
0 голосов
/ 02 октября 2018

Это намного сложнее, так как контекст является ключевым здесь.Взгляните на три фразы ниже ...

У Сьюзен был великий профессор, и ЕГО звали Адам.Он учил ЕГО любимого ученика всему, что нужно знать ... (Сьюзен определяется не как женщина, а как мужчина)

У Сьюзан был замечательный профессор, а ЕГО звали Адам.ОН учил ЕГО всему, что нужно знать ... (Хорошо, у нас есть ОНА, но и ОНА)

У Сьюзан был замечательный профессор по имени Адам.Адам учил ЕЕ все, что нужно знать ... (ОК, у нас ОНА)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...