File size: 4,638 Bytes
d3cee44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import nltk
import spacy
from word2number import w2n
import inflect
from num2words import num2words
p = inflect.engine()
import numpy as np
import random

nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
nlp = spacy.load('en_core_web_sm')

# object names with two words
SPECIAL_WORDS = ['baseball bat',
        'baseball glove',
        'cell phone',
        'dining table',
        'fire hydrant',
        'french fries',
        'hair drier',
        'hot dog',
        'parking meter',
        'potted plant',
        'soccer ball',
        'soccer player',
        'sports ball',
        'stop sign',
        'teddy bear',
        'tennis racket',
        'toy figure',
        'traffic light',
        'wine glass']

def _get_nouns(lines):
    # function to test if something is a noun
    present_words = []
    for s in SPECIAL_WORDS:
        if s in lines:
            present_words.append(s)
    
    for w in present_words:
        lines = lines.replace(w, "")
    
    is_noun = lambda pos: pos[:2] == 'NN' or pos[:2] == 'NNP'
    # do the nlp stuff
    tokenized = nltk.word_tokenize(lines)
    nouns = [word for (word, pos) in nltk.pos_tag(tokenized) if is_noun(pos)]
    noun_dict = {}
    if "objects" in nouns:
        nouns.remove("objects")
    if "image" in nouns:
        nouns.remove("image")

    for n in nouns:
        if n not in noun_dict.keys():
            noun_dict[n] = 1
        else:
            noun_dict[n] += 1
    nouns = {}
    for k, v in noun_dict.items():
        if not (k == "bus" or k == "skis"):
            if v == 1:
                if p.singular_noun(k):
                    k = p.singular_noun(k)
            else:
                if not p.singular_noun(k):
                    k = p.plural(k)
        try:
            w2n.word_to_num(k)
        except:
            if len(k) >= 3:
                if k == "ski":
                    k = "skis"
                elif k == "gras":
                        k = "grass"
                nouns[k] = v
    for w in present_words:
        nouns[w] = 1
    return nouns

def _get_num_nouns(lines):
    lines = lines.replace(":", "").replace(".", "")
    doc = nlp(lines)
    num_nouns = [chunk.text for chunk in doc.noun_chunks if any(token.pos_ == 'NUM' for token in chunk)]

    num_noun_dict = {}
    for n in num_nouns:
        nums = n.split(", ")
        for n in nums:
            try:
                w = " ".join(n.split(' ')[1:])
                if w == "ski":
                    w = "skis"
                num_noun_dict[w] = w2n.word_to_num(n.split(' ')[0])
            except:
                pass

    return num_noun_dict


def _obtain_nouns(gt):
    gt = gt.replace("hair dryer", "hair drier").lower()
    nouns_gt = _get_nouns(gt)

    num_nouns_gt = _get_num_nouns(gt)

    com_keys = []
    for k in nouns_gt.keys():
        if p.plural(k) in num_nouns_gt.keys():
            com_keys.append(k)
    for k in com_keys:
        del nouns_gt[k]

    num_nouns_gt = {**num_nouns_gt, **nouns_gt}
    
    return num_nouns_gt

def generate_qa_pairs(text):
    num_nouns = _obtain_nouns(text)
    qa_pairs = []

    for obj, count in num_nouns.items():
        # Count question
        if count == 1:
            plural_obj = p.plural(obj)
        else:
            plural_obj = obj
        count_question = f"How many {plural_obj} are there in the image?"
        count_answer = f"There {'is' if count == 1 else 'are'} {num2words(count)} {obj} in the image."
        qa_pairs.append((count_question, count_answer))

        prob_positive = np.random.uniform(0,1.)

        if prob_positive > 0.7 or count == 1:
            numeric_presence_question = f"{'Is' if count == 1 else 'Are'} there {num2words(count)} {obj} in the image?"
            numeric_presence_answer = "Yes."
        elif count > 1:
            numbers = [i for i in range(2, count + 6) if i != count]
            # Select a random number from the range
            cnt = random.choice(numbers)
            numeric_presence_question = f"{'Is' if cnt == 1 else 'Are'} there {num2words(cnt)} {obj} in the image?"
            numeric_presence_answer = "No."
        
        qa_pairs.append((numeric_presence_question, numeric_presence_answer))
        random.shuffle(qa_pairs)

    return random.sample(qa_pairs, min(len(qa_pairs), random.choice([1, 2, 3, 4, 5, 6])))

if __name__ == "__main__":

    text = "The objects present in the image are: wall, ceiling, shelf, cabinet, counter, dining table, two people, eighteen bottles, two wine glasses, refrigerator, tv, bowl"
    
    qa = generate_qa_pairs(text)
    from icecream import ic
    ic(qa)