You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
8.2 KiB

  1. import ProjectService from '@/services/project.service'
  2. export const state = () => ({
  3. projects: [],
  4. selected: [],
  5. current: {},
  6. loading: false
  7. })
  8. export const getters = {
  9. isProjectSelected(state) {
  10. return state.selected.length > 0
  11. },
  12. currentProject(state) {
  13. return state.current
  14. },
  15. getCurrentUserRole(state) {
  16. return state.current.current_users_role || {}
  17. },
  18. getFilterOption(state) {
  19. if (state.current.project_type === 'DocumentClassification') {
  20. return 'doc_annotations__isnull'
  21. } else if (state.current.project_type === 'SequenceLabeling') {
  22. return 'seq_annotations__isnull'
  23. } else if (state.current.project_type === 'Seq2seq') {
  24. return 'seq2seq_annotations__isnull'
  25. } else {
  26. return ''
  27. }
  28. },
  29. getLink(state) {
  30. if (state.current.project_type === 'DocumentClassification') {
  31. return 'text-classification'
  32. } else if (state.current.project_type === 'SequenceLabeling') {
  33. return 'sequence-labeling'
  34. } else if (state.current.project_type === 'Seq2seq') {
  35. return 'sequence-to-sequence'
  36. } else {
  37. return ''
  38. }
  39. },
  40. getImportFormat(state) {
  41. const plain = {
  42. type: 'plain',
  43. text: 'Plain text',
  44. accept: '.txt',
  45. examples: [
  46. 'EU rejects German call to boycott British lamb.\n',
  47. 'Peter Blackburn\n',
  48. 'President Obama'
  49. ]
  50. }
  51. const csv = {
  52. type: 'csv',
  53. text: 'CSV',
  54. accept: '.csv'
  55. }
  56. const json = {
  57. type: 'json',
  58. text: 'JSON',
  59. accept: '.json,.jsonl'
  60. }
  61. const conll = {
  62. type: 'conll',
  63. text: 'CoNLL',
  64. accept: '.conll'
  65. }
  66. if (state.current.project_type === 'DocumentClassification') {
  67. json.examples = [
  68. '{"text": "Terrible customer service.", "labels": ["negative"]}\n',
  69. '{"text": "Really great transaction.", "labels": ["positive"]}\n',
  70. '{"text": "Great price.", "labels": ["positive"]}'
  71. ]
  72. csv.examples = [
  73. 'text,label\n',
  74. '"Terrible customer service.","negative"\n',
  75. '"Really great transaction.","positive"\n',
  76. '"Great price.","positive"'
  77. ]
  78. return [
  79. plain,
  80. csv,
  81. json
  82. ]
  83. } else if (state.current.project_type === 'SequenceLabeling') {
  84. json.examples = [
  85. '{"text": "EU rejects German call to boycott British lamb.", "labels": [ [0, 2, "ORG"], [11, 17, "MISC"], ... ]}\n',
  86. '{"text": "Peter Blackburn", "labels": [ [0, 15, "PERSON"] ]}\n',
  87. '{"text": "President Obama", "labels": [ [10, 15, "PERSON"] ]}'
  88. ]
  89. conll.examples = [
  90. 'EU\tB-ORG\n',
  91. 'rejects\tO\n',
  92. 'German\tB-MISC\n',
  93. 'call\tO\n',
  94. 'to\tO\n',
  95. 'boycott\tO\n',
  96. 'British\tB-MISC\n',
  97. 'lamb\tO\n',
  98. '.\tO\n\n',
  99. 'Peter\tB-PER\n',
  100. 'Blackburn\tI-PER'
  101. ]
  102. return [
  103. plain,
  104. json,
  105. conll
  106. ]
  107. } else if (state.current.project_type === 'Seq2seq') {
  108. json.examples = [
  109. '{"text": "Hello!", "labels": ["こんにちは!"]}\n',
  110. '{"text": "Good morning.", "labels": ["おはようございます。"]}\n',
  111. '{"text": "See you.", "labels": ["さようなら。"]}'
  112. ]
  113. csv.examples = [
  114. 'text,label\n',
  115. '"Hello!","こんにちは!"\n',
  116. '"Good morning.","おはようございます。"\n',
  117. '"See you.","さようなら。"'
  118. ]
  119. return [
  120. plain,
  121. csv,
  122. json
  123. ]
  124. } else {
  125. return []
  126. }
  127. },
  128. getExportFormat(state) {
  129. const csv = {
  130. type: 'csv',
  131. text: 'CSV'
  132. }
  133. const json = {
  134. type: 'json',
  135. text: 'JSON'
  136. }
  137. const jsonl = {
  138. type: 'json1',
  139. text: 'JSON(Text label)'
  140. }
  141. if (state.current.project_type === 'DocumentClassification') {
  142. json.examples = [
  143. '{"id": 1, "text": "Terrible customer service.", "annotations": [{"id": 1, "label": 1, "user": 1}]}\n',
  144. '{"id": 2, "text": "Really great transaction.", "annotations": [{"id": 2, "label": 2, "user": 1}]}\n',
  145. '{"id": 3, "text": "Great price.", "annotations": [{"id": 3, "label": 2, "user": 1}]}'
  146. ]
  147. csv.examples = [
  148. 'id,text,label,user\n',
  149. '1,"Terrible customer service.",1,1\n',
  150. '2,"Really great transaction.",2,1\n',
  151. '3,"Great price.",2,1'
  152. ]
  153. return [
  154. csv,
  155. json
  156. ]
  157. } else if (state.current.project_type === 'SequenceLabeling') {
  158. json.examples = [
  159. '{"id": 1, "text": "EU rejects ...", "annotations": [{"id": 1, "label": 2, "start_offset": 0, "end_offset": 2, "user": 1}]}\n',
  160. '{"id": 2, "text": "Peter Blackburn", "annotations": [{"id": 2, "label": 1, "start_offset": 0, "end_offset": 15, "user": 1}]}\n',
  161. '{"id": 3, "text": "President Obama", "annotations": [{"id": 3, "label": 1, "start_offset": 10, "end_offset": 15, "user": 1}]}'
  162. ]
  163. jsonl.examples = [
  164. '{"id": 1, "text": "EU rejects ...", "labels": [[0,2,"ORG"], [11,17, "MISC"], [34,41,"ORG"]]}\n',
  165. '{"id": 2, "text": "Peter Blackburn", "labels": [[0, 15, "PERSON"]]}\n',
  166. '{"id": 3, "text": "President Obama", "labels": [[10, 15, "PERSON"]]}\n'
  167. ]
  168. return [
  169. json,
  170. jsonl
  171. ]
  172. } else if (state.current.project_type === 'Seq2seq') {
  173. json.examples = [
  174. '{"id": 1, "text": "Hello!", "annotations": [{"id": 1, "label": "こんにちは!", "user": 1}]}\n',
  175. '{"id": 2, "text": "Good morning.", "annotations": [{"id": 2, "label": "おはようございます。", "user": 1}]}\n',
  176. '{"id": 3, "text": "See you.", "annotations": [{"id": 3, "label": "さようなら。", "user": 1}]}'
  177. ]
  178. csv.examples = [
  179. 'id,text,label,user\n',
  180. '1,"Hello!","こんにちは!",1\n',
  181. '2,"Good morning.","おはようございます。",1\n',
  182. '3,"See you.","さようなら。",1'
  183. ]
  184. return [
  185. csv,
  186. json
  187. ]
  188. } else {
  189. return []
  190. }
  191. }
  192. }
  193. export const mutations = {
  194. setProjectList(state, payload) {
  195. state.projects = payload
  196. },
  197. createProject(state, project) {
  198. state.projects.unshift(project)
  199. },
  200. updateProject(state, project) {
  201. const item = state.projects.find(item => item.id === project.id)
  202. Object.assign(item, project)
  203. },
  204. deleteProject(state, projectId) {
  205. state.projects = state.projects.filter(item => item.id !== projectId)
  206. },
  207. updateSelected(state, selected) {
  208. state.selected = selected
  209. },
  210. resetSelected(state) {
  211. state.selected = []
  212. },
  213. setLoading(state, payload) {
  214. state.loading = payload
  215. },
  216. setCurrent(state, payload) {
  217. state.current = payload
  218. }
  219. }
  220. export const actions = {
  221. getProjectList({ commit }, config) {
  222. commit('setLoading', true)
  223. ProjectService.getProjectList()
  224. .then((response) => {
  225. commit('setProjectList', response.data)
  226. })
  227. .catch((error) => {
  228. alert(error)
  229. })
  230. .finally(() => {
  231. commit('setLoading', false)
  232. })
  233. },
  234. createProject({ commit }, project) {
  235. ProjectService.createProject(project)
  236. .then((response) => {
  237. commit('createProject', response.data)
  238. })
  239. .catch((error) => {
  240. alert(error)
  241. })
  242. },
  243. updateProject({ commit }, data) {
  244. ProjectService.updateProject(data.projectId, data)
  245. .then((response) => {
  246. commit('updateProject', response.data)
  247. })
  248. .catch((error) => {
  249. alert(error)
  250. })
  251. },
  252. deleteProject({ commit, state }, config) {
  253. for (const project of state.selected) {
  254. ProjectService.deleteProject(project.id)
  255. .then((response) => {
  256. commit('deleteProject', project.id)
  257. })
  258. .catch((error) => {
  259. alert(error)
  260. })
  261. }
  262. commit('resetSelected')
  263. },
  264. setCurrentProject({ commit }, projectId) {
  265. return ProjectService.fetchProjectById(projectId)
  266. .then((response) => {
  267. commit('setCurrent', response.data)
  268. })
  269. .catch((error) => {
  270. alert(error)
  271. })
  272. },
  273. updateCurrentProject({ commit }, data) {
  274. ProjectService.updateProject(data.projectId, data)
  275. .then((response) => {
  276. commit('setCurrent', response.data)
  277. })
  278. .catch((error) => {
  279. alert(error)
  280. })
  281. }
  282. }