| 96 | |
| 97 | |
| 98 | def get_data(split_sequences=False): |
| 99 | if not os.path.exists('chunking'): |
| 100 | print("Please create a folder in your local directory called 'chunking'") |
| 101 | print("train.txt and test.txt should be stored in there.") |
| 102 | print("Please check the comments to get the download link.") |
| 103 | exit() |
| 104 | elif not os.path.exists('chunking/train.txt'): |
| 105 | print("train.txt is not in chunking/train.txt") |
| 106 | print("Please check the comments to get the download link.") |
| 107 | exit() |
| 108 | elif not os.path.exists('chunking/test.txt'): |
| 109 | print("test.txt is not in chunking/test.txt") |
| 110 | print("Please check the comments to get the download link.") |
| 111 | exit() |
| 112 | |
| 113 | word2idx = {} |
| 114 | tag2idx = {} |
| 115 | word_idx = 0 |
| 116 | tag_idx = 0 |
| 117 | Xtrain = [] |
| 118 | Ytrain = [] |
| 119 | currentX = [] |
| 120 | currentY = [] |
| 121 | for line in open('chunking/train.txt'): |
| 122 | line = line.rstrip() |
| 123 | if line: |
| 124 | r = line.split() |
| 125 | word, tag, _ = r |
| 126 | if word not in word2idx: |
| 127 | word2idx[word] = word_idx |
| 128 | word_idx += 1 |
| 129 | currentX.append(word2idx[word]) |
| 130 | |
| 131 | if tag not in tag2idx: |
| 132 | tag2idx[tag] = tag_idx |
| 133 | tag_idx += 1 |
| 134 | currentY.append(tag2idx[tag]) |
| 135 | elif split_sequences: |
| 136 | Xtrain.append(currentX) |
| 137 | Ytrain.append(currentY) |
| 138 | currentX = [] |
| 139 | currentY = [] |
| 140 | |
| 141 | if not split_sequences: |
| 142 | Xtrain = currentX |
| 143 | Ytrain = currentY |
| 144 | |
| 145 | # load and score test data |
| 146 | Xtest = [] |
| 147 | Ytest = [] |
| 148 | currentX = [] |
| 149 | currentY = [] |
| 150 | for line in open('chunking/test.txt'): |
| 151 | line = line.rstrip() |
| 152 | if line: |
| 153 | r = line.split() |
| 154 | word, tag, _ = r |
| 155 | if word in word2idx: |